|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +import * as yaml from "yaml"; |
| 5 | + |
| 6 | +import { REPO_ROOT } from "./config"; |
| 7 | + |
| 8 | +/** The path of the workflow that reruns failed jobs in other workflows. */ |
| 9 | +const DEFLAKE_WORKFLOW_FILE = path.join( |
| 10 | + REPO_ROOT, |
| 11 | + ".github", |
| 12 | + "workflows", |
| 13 | + "deflake.yml", |
| 14 | +); |
| 15 | + |
| 16 | +/** |
| 17 | + * The names of workflows that `sync.ts` doesn't generate, but whose failed jobs the Deflake |
| 18 | + * workflow should rerun. |
| 19 | + * |
| 20 | + * Exclude workflows that have significant side effects, like publishing releases. It's OK to retry |
| 21 | + * CodeQL analysis. |
| 22 | + */ |
| 23 | +const NON_GENERATED_WORKFLOWS_TO_RERUN = [ |
| 24 | + "Check Expected Release Files", |
| 25 | + "Code-Scanning config CLI tests", |
| 26 | + "CodeQL action", |
| 27 | + "PR Check - Debug artifact upload", |
| 28 | + "PR Check - Debug artifacts after failure", |
| 29 | + "PR Checks", |
| 30 | + "Query filters tests", |
| 31 | + "Test that the workaround for python 3.12 on windows works", |
| 32 | +]; |
| 33 | + |
| 34 | +/** |
| 35 | + * Escapes the characters in a workflow name that `on.workflow_run.workflows` would otherwise treat |
| 36 | + * as glob syntax. |
| 37 | + * |
| 38 | + * @returns A pattern that matches only the workflow called `name`. |
| 39 | + */ |
| 40 | +export function escapeWorkflowName(name: string): string { |
| 41 | + // These are the characters that can be escaped in GitHub Actions filter patterns. `]` is only |
| 42 | + // special after an unescaped `[`, so it doesn't need escaping. |
| 43 | + return name.replace(/[\\[?*+!]/g, "\\$&"); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Replaces the items in the `on.workflow_run.workflows` list of a workflow, and any comments on |
| 48 | + * their lines or between them, with patterns that match the workflows called `workflowNames`, |
| 49 | + * sorted by name. The list is written as a block sequence. |
| 50 | + * |
| 51 | + * The whole workflow is rewritten with the `yaml` library, so any other parts that aren't |
| 52 | + * formatted the way that library formats them are reformatted, for example by joining lines of |
| 53 | + * folded (`>`) block scalars. Prettier, which checks the formatting of YAML files in this |
| 54 | + * repository, formats non-empty flow mappings, and flow sequences that don't fit within 80 |
| 55 | + * columns, differently from that library, so the workflow is expected not to contain any. |
| 56 | + * |
| 57 | + * @param workflow The contents of the workflow file. |
| 58 | + * @param workflowNames The names of the workflows that should trigger the workflow. |
| 59 | + * @returns The updated contents of the workflow file. |
| 60 | + * @throws If the workflow isn't valid YAML, or `on.workflow_run.workflows` isn't a sequence. |
| 61 | + */ |
| 62 | +export function setWorkflowRunWorkflows( |
| 63 | + workflow: string, |
| 64 | + workflowNames: string[], |
| 65 | +): string { |
| 66 | + const document = yaml.parseDocument(workflow); |
| 67 | + if (document.errors.length > 0) { |
| 68 | + throw new Error( |
| 69 | + `Failed to parse the workflow: ${document.errors[0].message}`, |
| 70 | + ); |
| 71 | + } |
| 72 | + const workflows = document.getIn(["on", "workflow_run", "workflows"], true); |
| 73 | + if (!yaml.isSeq(workflows)) { |
| 74 | + throw new Error("Expected `on.workflow_run.workflows` to be a sequence."); |
| 75 | + } |
| 76 | + |
| 77 | + workflows.flow = false; |
| 78 | + workflows.items = workflowNames |
| 79 | + .toSorted((a, b) => a.localeCompare(b, "en")) |
| 80 | + .map((name) => { |
| 81 | + const pattern = escapeWorkflowName(name); |
| 82 | + const item = new yaml.Scalar(pattern); |
| 83 | + // Quote the pattern in the same way as Prettier, which uses single quotes for strings that |
| 84 | + // contain `"` but no `\`, and double quotes otherwise. |
| 85 | + item.type = |
| 86 | + pattern.includes('"') && !pattern.includes("\\") |
| 87 | + ? yaml.Scalar.QUOTE_SINGLE |
| 88 | + : yaml.Scalar.QUOTE_DOUBLE; |
| 89 | + return item; |
| 90 | + }); |
| 91 | + // These options match how Prettier formats short flow sequences, such as `[completed]`, and long |
| 92 | + // strings. |
| 93 | + return document.toString({ flowCollectionPadding: false, lineWidth: 0 }); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Updates the Deflake workflow so that it reruns failed jobs in the workflows called |
| 98 | + * `generatedWorkflowNames` and in {@link NON_GENERATED_WORKFLOWS_TO_RERUN}. |
| 99 | + */ |
| 100 | +export function updateDeflakeWorkflow(generatedWorkflowNames: string[]): void { |
| 101 | + console.log(`Updating the list of workflows in '${DEFLAKE_WORKFLOW_FILE}'.`); |
| 102 | + const workflow = fs.readFileSync(DEFLAKE_WORKFLOW_FILE, "utf8"); |
| 103 | + fs.writeFileSync( |
| 104 | + DEFLAKE_WORKFLOW_FILE, |
| 105 | + setWorkflowRunWorkflows(workflow, [ |
| 106 | + ...generatedWorkflowNames, |
| 107 | + ...NON_GENERATED_WORKFLOWS_TO_RERUN, |
| 108 | + ]), |
| 109 | + "utf8", |
| 110 | + ); |
| 111 | +} |
0 commit comments