PromucFlow_constructor/.github/workflows/scripts/write-cypress-status.js
Shrikant Sharat Kandula 32147ddfdb
ci: Write Cypress status with the GitHub Script (#34559)
Replacing more of `nefrob/pr-description@v1.1.2` with our GitHub Script.
This is moving towards Javascript CI, and hopefully an unit-testable CI.




<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Chores**
- Replaced `nefrob/pr-description` action with `actions/github-script`
for handling test responses in PR automation.
- Improved guidance on modifying PR body for correct tag usage and
linked to relevant documentation.
- Enhanced script content to handle warnings and failures more
effectively in PR workflow automation.

- **New Features**
- Updated the `write-cypress-status` script to support different alert
types and prefixes, ensuring better validation and formatting of alerts.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-06-27 23:32:30 +05:30

56 lines
1.4 KiB
JavaScript

const HEADER = '<!-- This is an auto-generated comment: Cypress test results -->';
const FOOTER = '<!-- end of auto-generated comment: Cypress test results -->';
const PATTERN = new RegExp(HEADER + ".*?" + FOOTER, "ims");
// Ref: https://github.com/orgs/community/discussions/16925
const VALID_ALERT_TYPES = ["note", "tip", "important", "warning", "caution"]
const ALERT_PREFIXES = {
important: "🟣 🟣 🟣 ",
}
module.exports = async function({core, context, github}, alertType, note) {
if (!VALID_ALERT_TYPES.includes(alertType)) {
core.setFailed("Invalid alert type: '" + alertType + "'. Allowed: " + VALID_ALERT_TYPES.join(", ") + ".");
}
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.setFailed(
`No open pull request found for ${context.eventName}, ${context.sha}`,
);
return;
}
const response = await github.rest.pulls.get({
...context.repo,
pull_number: prNumber,
});
let body = response?.data?.body;
if (!body) {
core.setFailed(JSON.stringify(response, null, 2));
return;
}
note = [
HEADER,
`> [!${alertType.toUpperCase()}]`,
((ALERT_PREFIXES[alertType] ?? "") + note.trim()).replaceAll(/^/gm, "> "),
FOOTER,
].join("\n");
if (body.match(PATTERN)) {
body = body.replace(PATTERN, note);
} else {
body += "\n\n" + note + "\n";
}
await github.rest.pulls.update({
...context.repo,
pull_number: prNumber,
body,
});
}