From 26122a420806b9c4e973e26be24e0727da6c50d7 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 18 Nov 2026 16:18:46 +0100 Subject: [PATCH] ci(test): mirror the dev push run on the release pull request Every push to dev started test.yml twice for the same commit: the push run on dev and the pull_request run of the open release PR into main. Together about 72 long jobs exceeded the 60 organisation slots, so jobs queued and ci-ok landed minutes later. On a pull request from dev into main (head in this repository) the changes job now sets mirror and turns every area off (scripts/ci/changed-areas.mjs), so no gate, test, coverage or scan job runs. ci-ok keeps its needs verdict and then runs scripts/ci/mirror-push-run.mjs, which waits for the ci-ok of the push run of test.yml on dev for exactly the PR head commit and passes or fails with it. A missing push run, one that ended without a verdict, an API that keeps failing or a wait past 17 minutes fails with a re-run hint. ci-ok reads with actions, checks and contents only, and its timeout rises to 20 minutes. A push to dev now lists its changes against the merge base with main instead of the previous push, so the run the mirror copies covers every area the release pull request brings in, also when an earlier push's run was cancelled or red. The release workflows keep reading the newest ci-ok on a commit. A green mirror implies a green push ci-ok before it; a mirror that fails on its own blocks a release until it is re-run. --- .github/workflows/test.yml | 104 ++++++++++++-- CLAUDE.md | 1 + scripts/ci/changed-areas.mjs | 61 +++++++- scripts/ci/changed-areas.test.mjs | 86 ++++++++++- scripts/ci/mirror-push-run.mjs | 214 +++++++++++++++++++++++++++ scripts/ci/mirror-push-run.test.mjs | 216 ++++++++++++++++++++++++++++ scripts/ci/workflow-jobs.test.mjs | 207 ++++++++++++++++++++++++++ 7 files changed, 871 insertions(+), 18 deletions(-) create mode 100644 scripts/ci/mirror-push-run.mjs create mode 100644 scripts/ci/mirror-push-run.test.mjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dd97aed4a..51ef16858 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -90,12 +90,15 @@ permissions: # the six Postgres slices, the two e2e slices and the Docker job) plus two # short ones (Shared tests, Server S3), so two PR runs fit side by side # and a third waits a minute or two for slots. The target is ci-ok within -# ten minutes of the push. A new long job, slice or shard needs a reason -# that outweighs the slot it takes; a job that waits for a slot adds its -# wait to the run when a coverage job waits for it. +# ten minutes of the push. The release pull request starts none of them: +# it mirrors the push run on dev (below). A new long job, slice or shard +# needs a reason that outweighs the slot it takes; a job that waits for a +# slot adds its wait to the run when a coverage job waits for it. # - Timeouts: 15 minutes for the gates, the slices, Postgres, e2e and the -# Docker job, 10 for the merges, Shared tests, Server S3 and changes, 5 -# for ci-ok, so a job that hangs frees its slot in a quarter of an hour. +# Docker job, so a job that hangs frees its slot in a quarter of an hour; +# 10 for the merges, Shared tests, Server S3 and changes; 20 for ci-ok, +# which only polls while its mirror waits for the push run on the release +# pull request (below). # - Scan `needs:` the gates and the coverage jobs, Shared tests among them. # A coverage job failing must stop the scan: a missing lcov file would # make every line of that package report as uncovered, and the dashboard @@ -106,8 +109,11 @@ permissions: # passed. Everything else runs on those PRs so outside contributors and # dependency bumps still get the full test signal. # - changes runs first and decides which of the jobs below a change needs -# (scripts/ci/changed-areas.mjs holds the rules). A docs-only push or PR -# runs nothing else. The filter lives in a job, not in `on.paths`, because +# (scripts/ci/changed-areas.mjs holds the rules). A pull request lists +# what it changes against its base, a push to main what the push added, +# and a push to dev everything dev holds that main does not (the release +# pull request mirrors that run, below). A docs-only change runs nothing +# else. The filter lives in a job, not in `on.paths`, because # a workflow skipped by its path filter never reports a check, and a # required check that never reports blocks the PR for good. # - docker builds the Dockerfile and boots the result when the image area @@ -120,6 +126,26 @@ permissions: # or on a fork PR without secrets). The scan is not in it: the quality # gate reports on the PR by itself, and a SonarCloud outage should not # block a merge. +# - The release pull request (dev into main, from this repository) carries +# the head of dev, which the push run on dev tests anyway, and that run +# lists its changes against the merge base with main, so it covers every +# area the pull request brings in, an earlier push's changes included +# when that push's run was cancelled or red. Testing it a +# second time doubled the jobs past the 60 slots, so there changes sets +# mirror and turns every area off (scripts/ci/changed-areas.mjs): every +# job skips, the scan included (the push run's scan covers dev), and ci-ok +# runs scripts/ci/mirror-push-run.mjs, which waits for the ci-ok of the +# push run on dev for exactly the PR's head commit and passes or fails +# with it. No push run for that commit, one that ended without a verdict, +# or a wait past 17 minutes fails, with a re-run hint. A newer push to dev +# cancels the old push run and, through the synchronize it causes, the +# old PR run too (the concurrency group is per ref), so a mirror never +# waits on a superseded commit for long. The release workflows read the +# newest ci-ok on a commit (scripts/ci/ci-ok-verdict.mjs). A green mirror +# implies a green push ci-ok before it, so a mirror never lets a release +# through that the push run failed. A mirror that fails on its own (the +# API keeps failing, or the wait runs out) is the newest ci-ok on that +# commit and blocks the release workflows until it is re-run. # # Automatic Analysis must stay disabled on sonarcloud.io — it is mutually # exclusive with CI-based analysis, and it cannot import the lcov coverage @@ -151,6 +177,7 @@ jobs: code: ${{ steps.areas.outputs.code }} image: ${{ steps.areas.outputs.image }} deploy: ${{ steps.areas.outputs.deploy }} + mirror: ${{ steps.areas.outputs.mirror }} steps: - uses: actions/checkout@v6 with: @@ -179,6 +206,7 @@ jobs: id: list env: EVENT: ${{ github.event_name }} + REF: ${{ github.ref }} BEFORE: ${{ github.event.before }} AFTER: ${{ github.sha }} run: | @@ -192,9 +220,22 @@ jobs: changed_between HEAD^1 HEAD ;; push) + # A push to dev lists everything dev holds that main does not, + # not only what this push added. The release pull request + # mirrors this run (below), so it has to test what that pull + # request brings into main, and a cancelled or red earlier push + # must not leave its changes untested under a later docs-only + # one. The fetch is blobless: the diff reads trees only. + if [ "$REF" = refs/heads/dev ]; then + if git fetch --no-tags --filter=blob:none --unshallow origin +refs/heads/main:refs/remotes/origin/main && base="$(git merge-base origin/main "$AFTER")"; then + changed_between "$base" "$AFTER" + else + echo "No merge base with main, so everything runs." + list="" + fi # A new branch has no previous commit, and a force push can leave # it unreachable. Both run everything. - if [ -n "$BEFORE" ] && [ "$BEFORE" != "0000000000000000000000000000000000000000" ] && git fetch --no-tags --depth=1 origin "$BEFORE"; then + elif [ -n "$BEFORE" ] && [ "$BEFORE" != "0000000000000000000000000000000000000000" ] && git fetch --no-tags --depth=1 origin "$BEFORE"; then changed_between "$BEFORE" "$AFTER" else echo "No previous commit to compare with, so everything runs." @@ -208,10 +249,18 @@ jobs: echo "list=$list" >> "$GITHUB_OUTPUT" [ -z "$list" ] || cat "$list" + # On the release pull request (dev into main, from this repository) every + # area is off and mirror is on: ci-ok then mirrors the push run on dev + # for the same commit instead of testing it a second time. - name: Decide what runs id: areas env: LIST: ${{ steps.list.outputs.list }} + CI_EVENT: ${{ github.event_name }} + CI_BASE_REF: ${{ github.base_ref }} + CI_HEAD_REF: ${{ github.head_ref }} + CI_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + CI_REPO: ${{ github.repository }} run: | if [ -n "$LIST" ]; then node scripts/ci/changed-areas.mjs --files "$LIST" @@ -1342,8 +1391,10 @@ jobs: runs-on: ubuntu-24.04 timeout-minutes: 20 # Fork PRs and Dependabot runs don't receive SONAR_TOKEN, so skip the scan - # rather than fail it. + # rather than fail it. The release pull request mirrors the push run, whose + # scan already covers dev, so it skips the scan too. if: >- + needs.changes.outputs.mirror != 'true' && github.actor != 'dependabot[bot]' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) # Coverage jobs failing must stop the scan: a missing lcov file would make @@ -1353,6 +1404,7 @@ jobs: # scripts/ci/workflow-jobs.test.mjs fails while a job that uploads a # sonar-*-coverage artifact is missing here. needs: + - changes - shared - server-types - server-lint @@ -1437,7 +1489,14 @@ jobs: # ruleset reads a skipped required check as passed. if: always() runs-on: ubuntu-24.04 - timeout-minutes: 5 + # 20 rather than 5: on the release pull request the mirror step waits for + # the push run's ci-ok, and gives up two minutes before this kills it. + timeout-minutes: 20 + # The mirror reads the push run's jobs; nothing here writes. + permissions: + actions: read + checks: read + contents: read steps: - name: Every needed job passed or was skipped env: @@ -1450,3 +1509,28 @@ jobs: echo "::error::Not passed: $failed" exit 1 fi + + # On the release pull request every job above skipped, so the step above + # only proves that changes passed. The verdict is the push run's: wait + # for the ci-ok of the push run of this workflow on dev for exactly the + # pull request's head commit, and pass or fail with it. No push run, a + # cancelled one or a timeout fails, with what to do in the log. + - name: Check out the mirror script + if: needs.changes.outputs.mirror == 'true' + uses: actions/checkout@v6 + with: + persist-credentials: false + sparse-checkout: scripts/ci + + - uses: actions/setup-node@v6 + if: needs.changes.outputs.mirror == 'true' + with: + node-version-file: .nvmrc + + - name: Mirror the ci-ok of the push run on dev + if: needs.changes.outputs.mirror == 'true' + timeout-minutes: 19 + env: + GITHUB_TOKEN: ${{ github.token }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: node scripts/ci/mirror-push-run.mjs diff --git a/CLAUDE.md b/CLAUDE.md index aaaf1c92d..acfd440d3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -143,6 +143,7 @@ These principles come out of a full-repo audit and shape all new code: - **What the folded jobs hold.** `Shared tests` is the plugin SDK's typecheck and coverage, run before the root install so the SDK resolves only its own dependencies, then i18n parity and shared's coverage. `Client types` is the typecheck and the strict ratchet; `Client checks` the source ratchets, then the production build with `check:gl-split`. `Docker and Helm` builds and boots the image when the image area changed and checks the chart and compose files when the deploy area changed. Inside a folded job each group of steps runs after a failure in another, so one red check shows every gate that failed; the step names say which. - **Tests run beside the gates, in shards.** The client and server suites run in ten slices each on parallel runners (`client-tests`, `server-tests`, through each package's `vitest.shard.config.ts`), the Postgres lane in three per major, and the end-to-end flows in two, and `client-coverage`/`server-coverage` merge their blob reports and check the thresholds against the whole. A red slice is usually a failing test, a red merge job usually a threshold. A sliced job states its slice count in its matrix's shard axis, the `i/n` in its name and its `--shard=i/n`; `scripts/ci/workflow-jobs.test.mjs` fails until all three agree, and on any job naming `matrix.shard` or `--shard` whose axis is not the one-line `shard: [1, ..., n]` it reads. - **`ci-ok` is the required check.** The branch ruleset requires `ci-ok` (plus `lint` and `check-target` from their own workflows), and `ci-ok` needs every job above except the scan. It fails if any of them failed or was cancelled and passes if they passed or were skipped. A new gate job counts only once it is listed in `ci-ok`'s `needs:`, and `scripts/ci/workflow-jobs.test.mjs` (in `test:ci-scripts`) fails while a job other than the scan is missing there. +- **The release pull request mirrors the push run.** On the pull request from `dev` into `main` (head branch in this repository), `changes` sets `mirror` and turns every area off (`scripts/ci/changed-areas.mjs`), so no test, gate or scan job runs, and `ci-ok` runs `scripts/ci/mirror-push-run.mjs`: it waits for the `ci-ok` of the push run of `test.yml` on `dev` for exactly the PR's head commit and passes or fails with it. No such push run, one that ended without a verdict, or a wait past 17 minutes fails with a re-run hint, never passes. A push to `dev` lists its changes against the merge base with `main`, not against the previous push, so the run it mirrors covers every area the pull request brings in, even when an earlier push's run was cancelled or red. Testing that commit twice doubled the jobs past the 60 slots. - **Guides name paths that exist**: the `changes` job runs `scripts/ci/doc-paths.mjs`, which resolves every backtick path in the CLAUDE.md files, `README.md`, `server/src/nest/README.md` and the wiki against the tracked files; the missing ones are listed in `scripts/ci/doc-paths-baseline.json`, which only shrinks (`--update` after a fix). - **What runs is decided by `changes`, not by `on.paths`**: `scripts/ci/changed-areas.mjs` maps the changed files to the `code`, `image` and `deploy` areas (tests: `npm run test:ci-scripts`). A path filter on the workflow would leave `ci-ok` unreported on a docs-only PR and block it. Add a new top-level file the build reads to the right area there. A change to `test.yml`, to `scripts/ci/` or to a local action under `.github/actions/` runs everything. - **Setup goes through one local action.** Every job that installs dependencies uses `.github/actions/setup/action.yml` (`scope` full, client or shared; `build-shared`) instead of its own `npm ci`; `workflow-jobs.test.mjs` fails on a job that installs on its own (plugin-sdk's own install in its directory excepted). The action restores node_modules on an exact key (runner image, scope, Node ABI, `.nvmrc`, the lockfile and every workspace `package.json`, so npm ci still refuses a `package.json` out of step with the lockfile; no restore-keys) and shared/dist on a key over shared's sources and build config, and runs `npm ci` or the shared build on a miss. Only pushes to dev and main save, the npm download cache included; pull requests only read. diff --git a/scripts/ci/changed-areas.mjs b/scripts/ci/changed-areas.mjs index 6f84b4d37..8a40026f1 100644 --- a/scripts/ci/changed-areas.mjs +++ b/scripts/ci/changed-areas.mjs @@ -8,12 +8,23 @@ // through this script, and every other job asks the answer whether to run. // The aggregate `ci-ok` job then treats a skipped job as passed. // +// It also decides whether the run mirrors the push run instead of testing. +// The release pull request (dev into main, from this repository) always +// carries the head of dev, and every push to dev already runs this workflow +// on exactly that commit, listing its changes against the merge base with +// main, so that run covers every area the pull request brings in. Running +// it twice for one commit doubled the jobs past the organisation's slots, so +// on that pull request every area is off and ci-ok mirrors the push run's +// verdict (scripts/ci/mirror-push-run.mjs). +// The run's context comes from the environment: CI_EVENT, CI_BASE_REF, +// CI_HEAD_REF, CI_HEAD_REPO and CI_REPO. +// // Usage: // node scripts/ci/changed-areas.mjs --files one changed path per line // node scripts/ci/changed-areas.mjs --all no usable list, run everything // -// Prints `=true|false` per area and appends the same lines to -// $GITHUB_OUTPUT when it is set. +// Prints `=true|false` per area and `mirror=true|false`, and appends +// the same lines to $GITHUB_OUTPUT when it is set. // // It fails closed. A list it cannot read is an error, and anything it cannot // classify with confidence (an empty list, a path git had to quote) turns every @@ -108,6 +119,50 @@ export function classify(files) { return result; } +// The release pull request: base main, head dev, and the head branch in this +// repository. A fork's branch named dev is not it: its commits never ran here. +export const RELEASE_PR = { base: 'main', head: 'dev' }; + +/** + * @param {{ event?: string, baseRef?: string, headRef?: string, headRepo?: string, repo?: string }} context + * @returns {boolean} true when the run mirrors the push run on dev instead of testing + */ +export function mirrorsPushRun({ event, baseRef, headRef, headRepo, repo }) { + return ( + event === 'pull_request' && + baseRef === RELEASE_PR.base && + headRef === RELEASE_PR.head && + Boolean(repo) && + headRepo === repo + ); +} + +/** + * What the run does: the areas to test, and whether ci-ok mirrors the push + * run. In mirror mode every area is off, so every job skips by its area + * condition and only changes and ci-ok run. + * + * @param {string[] | null} files changed paths, or null when there is no usable list + * @param {Parameters[0]} context + * @returns {Record} + */ +export function decide(files, context) { + if (mirrorsPushRun(context)) { + return { ...Object.fromEntries(Object.keys(AREAS).map((area) => [area, false])), mirror: true }; + } + return { ...classify(files), mirror: false }; +} + +function contextFromEnv(env) { + return { + event: env.CI_EVENT, + baseRef: env.CI_BASE_REF, + headRef: env.CI_HEAD_REF, + headRepo: env.CI_HEAD_REPO, + repo: env.CI_REPO, + }; +} + function parseArgs(argv) { if (argv.length === 1 && argv[0] === '--all') return { all: true }; if (argv.length === 2 && argv[0] === '--files' && argv[1]) return { all: false, list: argv[1] }; @@ -118,7 +173,7 @@ function main() { const args = parseArgs(process.argv.slice(2)); // An unreadable list throws and fails the job instead of running on a guess. const files = args.all ? null : readFileSync(args.list, 'utf8').split('\n'); - const result = classify(files); + const result = decide(files, contextFromEnv(process.env)); const lines = Object.entries(result).map(([area, on]) => `${area}=${on}`); console.log(lines.join('\n')); if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `${lines.join('\n')}\n`); diff --git a/scripts/ci/changed-areas.test.mjs b/scripts/ci/changed-areas.test.mjs index d0d371ca0..518ad3fea 100644 --- a/scripts/ci/changed-areas.test.mjs +++ b/scripts/ci/changed-areas.test.mjs @@ -5,7 +5,7 @@ import { tmpdir } from 'node:os'; import { basename, dirname, join } from 'node:path'; import { after, describe, it } from 'node:test'; import { fileURLToPath } from 'node:url'; -import { AREAS, classify } from './changed-areas.mjs'; +import { AREAS, classify, decide, mirrorsPushRun } from './changed-areas.mjs'; const script = fileURLToPath(new URL('./changed-areas.mjs', import.meta.url)); const workflow = fileURLToPath(new URL('../../.github/workflows/test.yml', import.meta.url)); @@ -15,10 +15,21 @@ after(() => rmSync(scratch, { recursive: true, force: true })); const none = Object.fromEntries(Object.keys(AREAS).map((area) => [area, false])); const every = Object.fromEntries(Object.keys(AREAS).map((area) => [area, true])); +// The run's context, cleared so a variable set around the test run cannot +// switch the script into mirror mode. +const NO_CONTEXT = { CI_EVENT: '', CI_BASE_REF: '', CI_HEAD_REF: '', CI_HEAD_REPO: '', CI_REPO: '' }; +const RELEASE_PR_ENV = { + CI_EVENT: 'pull_request', + CI_BASE_REF: 'main', + CI_HEAD_REF: 'dev', + CI_HEAD_REPO: 'liketrek/TREK', + CI_REPO: 'liketrek/TREK', +}; + function run(args, env = {}) { return spawnSync(process.execPath, [script, ...args], { encoding: 'utf8', - env: { ...process.env, GITHUB_OUTPUT: '', ...env }, + env: { ...process.env, GITHUB_OUTPUT: '', ...NO_CONTEXT, ...env }, }); } @@ -118,6 +129,55 @@ describe('classify', () => { }); }); +describe('the mirror decision', () => { + const releasePr = { + event: 'pull_request', + baseRef: 'main', + headRef: 'dev', + headRepo: 'liketrek/TREK', + repo: 'liketrek/TREK', + }; + + it('mirrors the push run on the release pull request from dev into main', () => { + assert.equal(mirrorsPushRun(releasePr), true); + }); + + it('tests every other run itself', () => { + for (const [what, context] of [ + ['a push to dev', { ...releasePr, event: 'push', baseRef: '', headRef: '' }], + ['a push to main', { event: 'push', repo: 'liketrek/TREK' }], + ['a manual dispatch', { ...releasePr, event: 'workflow_dispatch' }], + ['a feature PR into dev', { ...releasePr, baseRef: 'dev', headRef: 'feat/x' }], + ['a hotfix PR into main', { ...releasePr, headRef: 'hotfix/x' }], + ['a PR from main into dev', { ...releasePr, baseRef: 'dev', headRef: 'main' }], + ['a fork whose branch is named dev', { ...releasePr, headRepo: 'someone/TREK' }], + ['a PR without a head repository', { ...releasePr, headRepo: '' }], + ['a run without a repository', { ...releasePr, headRepo: '', repo: '' }], + ['no context at all', {}], + ]) { + assert.equal(mirrorsPushRun(context), false, what); + } + }); + + it('turns every area off in mirror mode, whatever changed', () => { + for (const files of [null, [], ['server/src/index.ts'], ['.github/workflows/test.yml']]) { + assert.deepEqual(decide(files, releasePr), { ...none, mirror: true }, JSON.stringify(files)); + } + }); + + it('classifies the files as before outside mirror mode', () => { + const feature = { ...releasePr, baseRef: 'dev', headRef: 'feat/x' }; + assert.deepEqual(decide(['README.md'], feature), { ...none, mirror: false }); + assert.deepEqual(decide(null, { event: 'push' }), { ...every, mirror: false }); + assert.deepEqual(decide(['charts/trek/values.yaml'], feature), { + code: true, + image: false, + deploy: true, + mirror: false, + }); + }); +}); + describe('the command line', () => { it('writes the areas to stdout and to GITHUB_OUTPUT', () => { const list = join(scratch, 'list.txt'); @@ -126,14 +186,30 @@ describe('the command line', () => { writeFileSync(output, ''); const result = run(['--files', list], { GITHUB_OUTPUT: output }); assert.equal(result.status, 0, result.stderr); - assert.equal(result.stdout, 'code=true\nimage=false\ndeploy=true\n'); - assert.equal(readFileSync(output, 'utf8'), 'code=true\nimage=false\ndeploy=true\n'); + assert.equal(result.stdout, 'code=true\nimage=false\ndeploy=true\nmirror=false\n'); + assert.equal(readFileSync(output, 'utf8'), 'code=true\nimage=false\ndeploy=true\nmirror=false\n'); }); it('turns everything on with --all', () => { const result = run(['--all']); assert.equal(result.status, 0, result.stderr); - assert.equal(result.stdout, 'code=true\nimage=true\ndeploy=true\n'); + assert.equal(result.stdout, 'code=true\nimage=true\ndeploy=true\nmirror=false\n'); + }); + + it('turns every area off and mirror on for the release pull request', () => { + const list = join(scratch, 'release-list.txt'); + writeFileSync(list, 'server/src/index.ts\n'); + for (const args of [['--files', list], ['--all']]) { + const result = run(args, RELEASE_PR_ENV); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, 'code=false\nimage=false\ndeploy=false\nmirror=true\n', JSON.stringify(args)); + } + }); + + it('still fails on an unreadable list in mirror mode', () => { + const result = run(['--files', join(scratch, 'missing.txt')], RELEASE_PR_ENV); + assert.equal(result.status, 1); + assert.equal(result.stdout, ''); }); it('fails on a list it cannot read', () => { diff --git a/scripts/ci/mirror-push-run.mjs b/scripts/ci/mirror-push-run.mjs new file mode 100644 index 000000000..c05993f28 --- /dev/null +++ b/scripts/ci/mirror-push-run.mjs @@ -0,0 +1,214 @@ +#!/usr/bin/env node +// Mirrors the push run's ci-ok onto the release pull request. +// +// The release pull request (dev into main) always carries the head of dev, and +// every push to dev runs test.yml on exactly that commit. Running the whole +// workflow a second time for the pull request doubled the jobs past the +// organisation's slots, so on that pull request changes turns every area off +// (scripts/ci/changed-areas.mjs) and ci-ok runs this script instead: it waits +// for the ci-ok of the push run of test.yml on dev for the pull request's head +// commit and passes or fails with it. +// +// A green push ci-ok means every area the pull request brings in was tested: +// a push to dev lists its changes against the merge base with main (the +// "List the changed files" step in test.yml), not against the previous push, +// so a cancelled or red earlier push cannot hide under a later docs-only one. +// +// It never passes without a real test of that exact commit. No push run for +// it, a push run that ended without a ci-ok verdict, a ci-ok that is not +// success, an API that keeps failing, or a wait past the timeout all fail, +// each with what to do next. +// +// Usage (in ci-ok, with GITHUB_TOKEN, GITHUB_REPOSITORY and GITHUB_API_URL set): +// HEAD_SHA= node scripts/ci/mirror-push-run.mjs + +import { fileURLToPath } from 'node:url'; + +export const WORKFLOW_FILE = 'test.yml'; +export const BRANCH = 'dev'; +export const CHECK = 'ci-ok'; + +// The push run starts about 20 s before the pull request run, so it is there +// on the first look. A short grace covers GitHub creating them the other way +// round. +export const GRACE_MS = 3 * 60_000; +export const INTERVAL_MS = 20_000; +// Below ci-ok's timeout-minutes in test.yml (workflow-jobs.test.mjs holds the +// margin), so the script always ends with its own message, not a kill. +export const TIMEOUT_MS = 17 * 60_000; +export const MAX_API_ERRORS = 3; + +const minutes = (ms) => `${Math.round(ms / 60_000)} min`; + +class ApiError extends Error {} + +function api({ fetch, apiUrl, token }) { + return async (path) => { + let response; + try { + response = await fetch(`${apiUrl}${path}`, { + headers: { + Accept: 'application/vnd.github+json', + Authorization: `Bearer ${token}`, + 'X-GitHub-Api-Version': '2022-11-28', + }, + }); + } catch (err) { + throw new ApiError(`GET ${path} failed: ${err instanceof Error ? err.message : err}`); + } + if (!response.ok) throw new ApiError(`GET ${path} answered ${response.status}`); + return response.json(); + }; +} + +/** The newest push run of test.yml on dev for the commit, or null. */ +async function findPushRun(get, repo, sha) { + const query = new URLSearchParams({ head_sha: sha, event: 'push', branch: BRANCH, per_page: '100' }); + const body = await get(`/repos/${repo}/actions/workflows/${WORKFLOW_FILE}/runs?${query}`); + if (!Array.isArray(body?.workflow_runs)) throw new ApiError('the runs answer has no workflow_runs array'); + const runs = body.workflow_runs.filter( + (run) => run.head_sha === sha && run.event === 'push' && run.head_branch === BRANCH, + ); + return runs.reduce((newest, run) => (newest === null || run.id > newest.id ? run : newest), null); +} + +/** The ci-ok job of the run's latest attempt, or null while it has none. */ +async function findCheck(get, repo, runId) { + const jobs = []; + for (let page = 1; ; page += 1) { + const body = await get(`/repos/${repo}/actions/runs/${runId}/jobs?filter=latest&per_page=100&page=${page}`); + if (!Array.isArray(body?.jobs)) throw new ApiError('the jobs answer has no jobs array'); + jobs.push(...body.jobs); + if (body.jobs.length === 0 || jobs.length >= (body.total_count ?? 0)) break; + } + const named = jobs.filter((job) => job.name === CHECK); + return named.reduce((newest, job) => (newest === null || job.id > newest.id ? job : newest), null); +} + +/** + * One look at the push run. Returns { done: false, note } to keep waiting, or + * { done: true, ok, message } once the verdict is known. + */ +export function assess({ run, check, sha, elapsed, grace = GRACE_MS }) { + if (!run) { + if (elapsed < grace) return { done: false, note: `No push run of ${WORKFLOW_FILE} on ${BRANCH} for ${sha} yet.` }; + return { + done: true, + ok: false, + message: `No push run of ${WORKFLOW_FILE} on ${BRANCH} exists for ${sha}, so nothing tested this commit. Push it to ${BRANCH} (or run CI on ${BRANCH} again), then re-run this job.`, + }; + } + const where = run.html_url ?? `run ${run.id}`; + if (check?.status === 'completed') { + if (check.conclusion === 'success') { + return { done: true, ok: true, message: `${CHECK} of the push run passed on ${sha}: ${check.html_url ?? where}` }; + } + return { + done: true, + ok: false, + message: `${CHECK} of the push run is ${check.conclusion} on ${sha}: ${check.html_url ?? where}. Fix it on ${BRANCH}, or re-run that push run, then re-run this job.`, + }; + } + if (run.status === 'completed') { + return { + done: true, + ok: false, + message: `The push run ${where} ended ${run.conclusion} without a ${CHECK} verdict on ${sha}. Re-run it, then re-run this job.`, + }; + } + return { + done: false, + note: `Waiting for the push run ${where}: run ${run.status}, ${CHECK} ${check ? check.status : 'not started'}.`, + }; +} + +/** + * Waits for the push run's ci-ok and returns { ok, message }. + * + * @param {object} options + * @param {string} options.sha the pull request's head commit + * @param {string} options.repo owner/name + * @param {string} options.token a token with actions: read + * @param {string} [options.apiUrl] + * @param {typeof fetch} options.fetch + * @param {{ now(): number, sleep(ms: number): Promise }} options.clock + * @param {(line: string) => void} [options.log] + */ +export async function mirror({ + sha, + repo, + token, + apiUrl = 'https://api.github.com', + fetch, + clock, + log = () => {}, + interval = INTERVAL_MS, + timeout = TIMEOUT_MS, + grace = GRACE_MS, + maxApiErrors = MAX_API_ERRORS, +}) { + const get = api({ fetch, apiUrl, token }); + const start = clock.now(); + let errors = 0; + let lastNote = ''; + let lastRun = null; + for (;;) { + try { + const run = await findPushRun(get, repo, sha); + const check = run ? await findCheck(get, repo, run.id) : null; + errors = 0; + lastRun = run; + const result = assess({ run, check, sha, elapsed: clock.now() - start, grace }); + if (result.done) return { ok: result.ok, message: result.message }; + if (result.note !== lastNote) log(result.note); + lastNote = result.note; + } catch (err) { + if (!(err instanceof ApiError)) throw err; + errors += 1; + log(`The API failed (${errors} of ${maxApiErrors}): ${err.message}`); + if (errors >= maxApiErrors) { + return { + ok: false, + message: `The GitHub API failed ${maxApiErrors} times in a row (${err.message}), so the push run's ${CHECK} is unknown. Re-run this job.`, + }; + } + } + if (clock.now() - start + interval > timeout) break; + await clock.sleep(interval); + } + const where = lastRun?.html_url ?? `the push run of ${WORKFLOW_FILE} on ${BRANCH}`; + return { + ok: false, + message: `Gave up after ${minutes(timeout)}: ${CHECK} of ${where} has not finished on ${sha}. Once it has, re-run this job; it then reads the verdict at once.`, + }; +} + +async function main() { + const sha = process.env.HEAD_SHA ?? ''; + const repo = process.env.GITHUB_REPOSITORY ?? ''; + const token = process.env.GITHUB_TOKEN ?? ''; + if (!/^[0-9a-f]{40}$/.test(sha)) throw new Error(`HEAD_SHA is not a commit: "${sha}"`); + if (!/^[\w.-]+\/[\w.-]+$/.test(repo)) throw new Error(`GITHUB_REPOSITORY is not owner/name: "${repo}"`); + if (!token) throw new Error('GITHUB_TOKEN is empty'); + const result = await mirror({ + sha, + repo, + token, + apiUrl: process.env.GITHUB_API_URL || 'https://api.github.com', + fetch: globalThis.fetch, + clock: { now: () => Date.now(), sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)) }, + log: (line) => console.log(line), + }); + if (!result.ok) { + console.log(`::error::${result.message}`); + process.exit(1); + } + console.log(result.message); +} + +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { + main().catch((err) => { + console.log(`::error::${err instanceof Error ? err.message : err}`); + process.exit(1); + }); +} diff --git a/scripts/ci/mirror-push-run.test.mjs b/scripts/ci/mirror-push-run.test.mjs new file mode 100644 index 000000000..6bc45172f --- /dev/null +++ b/scripts/ci/mirror-push-run.test.mjs @@ -0,0 +1,216 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { assess, GRACE_MS, INTERVAL_MS, mirror, TIMEOUT_MS } from './mirror-push-run.mjs'; + +const SHA = 'a'.repeat(40); +const REPO = 'liketrek/TREK'; +const RUN_URL = 'https://github.com/liketrek/TREK/actions/runs/7'; + +const pushRun = (fields = {}) => ({ + id: 7, + head_sha: SHA, + event: 'push', + head_branch: 'dev', + status: 'in_progress', + conclusion: null, + html_url: RUN_URL, + ...fields, +}); +const ciOk = (status, conclusion = null) => ({ id: 70, name: 'ci-ok', status, conclusion, html_url: `${RUN_URL}/job/70` }); +const otherJob = { id: 71, name: 'Server tests 1/10', status: 'completed', conclusion: 'success' }; + +/** + * A fake GitHub API. `looks` holds one answer per look: { runs, jobs } or + * { error } (a status code, or 'network'). The last look repeats. + */ +function fakeApi(looks) { + const calls = []; + let look = -1; + const fetch = async (url, init) => { + calls.push({ url, init }); + const path = url.replace('https://api.github.test', ''); + if (path.includes('/actions/workflows/')) look += 1; + const answer = looks[Math.min(look, looks.length - 1)]; + if (answer.error === 'network') throw new Error('socket hang up'); + if (answer.error) return { ok: false, status: answer.error, json: async () => ({}) }; + if (path.includes('/actions/workflows/')) { + return { ok: true, status: 200, json: async () => ({ workflow_runs: answer.runs ?? [] }) }; + } + const jobs = answer.jobs ?? []; + return { ok: true, status: 200, json: async () => ({ total_count: jobs.length, jobs }) }; + }; + return { fetch, calls }; +} + +function fakeClock() { + let now = 0; + return { now: () => now, sleep: async (ms) => void (now += ms), elapsed: () => now }; +} + +async function mirrorWith(looks, options = {}) { + const { fetch, calls } = fakeApi(looks); + const clock = fakeClock(); + const lines = []; + const result = await mirror({ + sha: SHA, + repo: REPO, + token: 'secret', + apiUrl: 'https://api.github.test', + fetch, + clock, + log: (line) => lines.push(line), + ...options, + }); + return { ...result, calls, lines, elapsed: clock.elapsed() }; +} + +describe('mirror', () => { + it('passes when the push run ci-ok passed', async () => { + const result = await mirrorWith([{ runs: [pushRun()], jobs: [otherJob, ciOk('completed', 'success')] }]); + assert.equal(result.ok, true, result.message); + assert.match(result.message, /passed/); + assert.equal(result.elapsed, 0); + }); + + it('asks for exactly the push runs of test.yml on dev for the commit, with the token', async () => { + const { calls } = await mirrorWith([{ runs: [pushRun()], jobs: [ciOk('completed', 'success')] }]); + const runs = new URL(calls[0].url); + assert.equal(runs.pathname, `/repos/${REPO}/actions/workflows/test.yml/runs`); + assert.equal(runs.searchParams.get('head_sha'), SHA); + assert.equal(runs.searchParams.get('event'), 'push'); + assert.equal(runs.searchParams.get('branch'), 'dev'); + assert.equal(calls[0].init.headers.Authorization, 'Bearer secret'); + const jobs = new URL(calls[1].url); + assert.equal(jobs.pathname, `/repos/${REPO}/actions/runs/7/jobs`); + assert.equal(jobs.searchParams.get('filter'), 'latest'); + }); + + it('fails when the push run ci-ok failed', async () => { + const result = await mirrorWith([ + { runs: [pushRun({ status: 'completed', conclusion: 'failure' })], jobs: [ciOk('completed', 'failure')] }, + ]); + assert.equal(result.ok, false); + assert.match(result.message, /ci-ok of the push run is failure/); + }); + + it('fails when the push run ci-ok was cancelled', async () => { + const result = await mirrorWith([ + { runs: [pushRun({ status: 'completed', conclusion: 'cancelled' })], jobs: [ciOk('completed', 'cancelled')] }, + ]); + assert.equal(result.ok, false); + assert.match(result.message, /is cancelled/); + }); + + it('fails when the push run ended before ci-ok ran', async () => { + const result = await mirrorWith([{ runs: [pushRun({ status: 'completed', conclusion: 'cancelled' })], jobs: [] }]); + assert.equal(result.ok, false); + assert.match(result.message, /ended cancelled without a ci-ok verdict/); + }); + + it('fails closed after the grace when no push run exists', async () => { + const result = await mirrorWith([{ runs: [] }]); + assert.equal(result.ok, false); + assert.match(result.message, /No push run of test\.yml on dev exists/); + assert.match(result.message, /re-run this job/); + assert.ok(result.elapsed >= GRACE_MS && result.elapsed < GRACE_MS + 2 * INTERVAL_MS, String(result.elapsed)); + }); + + it('counts no run of another event, branch or commit', async () => { + const result = await mirrorWith([ + { + runs: [ + pushRun({ event: 'workflow_dispatch' }), + pushRun({ event: 'pull_request' }), + pushRun({ head_branch: 'main' }), + pushRun({ head_sha: 'b'.repeat(40) }), + ], + jobs: [ciOk('completed', 'success')], + }, + ]); + assert.equal(result.ok, false); + assert.match(result.message, /No push run/); + }); + + it('reads the newest push run when there are several', async () => { + const { fetch, calls } = fakeApi([{ runs: [pushRun({ id: 7 }), pushRun({ id: 9 })], jobs: [ciOk('completed', 'success')] }]); + const result = await mirror({ sha: SHA, repo: REPO, token: 't', apiUrl: 'https://api.github.test', fetch, clock: fakeClock() }); + assert.equal(result.ok, true); + assert.match(calls[1].url, /\/actions\/runs\/9\/jobs/); + }); + + it('waits while the push run is still going, then passes', async () => { + const result = await mirrorWith([ + { runs: [] }, + { runs: [pushRun({ status: 'queued' })], jobs: [] }, + { runs: [pushRun()], jobs: [ciOk('queued')] }, + { runs: [pushRun()], jobs: [ciOk('in_progress')] }, + { runs: [pushRun({ status: 'completed', conclusion: 'success' })], jobs: [ciOk('completed', 'success')] }, + ]); + assert.equal(result.ok, true, result.message); + assert.equal(result.elapsed, 4 * INTERVAL_MS); + assert.equal(result.lines.length, 4, result.lines.join('\n')); + }); + + it('logs a waiting state once, not on every look', async () => { + const looks = Array.from({ length: 5 }, () => ({ runs: [pushRun()], jobs: [ciOk('queued')] })); + looks.push({ runs: [pushRun()], jobs: [ciOk('completed', 'success')] }); + const result = await mirrorWith(looks); + assert.equal(result.ok, true); + assert.equal(result.lines.length, 1); + }); + + it('gives up with a re-run hint at the timeout, before ci-ok would be killed', async () => { + const result = await mirrorWith([{ runs: [pushRun()], jobs: [ciOk('in_progress')] }]); + assert.equal(result.ok, false); + assert.match(result.message, /Gave up after 17 min/); + assert.match(result.message, /re-run this job/); + assert.ok(result.message.includes(RUN_URL)); + assert.ok(result.elapsed <= TIMEOUT_MS, String(result.elapsed)); + assert.ok(result.elapsed > TIMEOUT_MS - 2 * INTERVAL_MS, String(result.elapsed)); + }); + + it('rides out a passing API error', async () => { + const result = await mirrorWith([ + { error: 502 }, + { error: 'network' }, + { runs: [pushRun()], jobs: [ciOk('completed', 'success')] }, + ]); + assert.equal(result.ok, true, result.message); + }); + + it('fails after the API failed three times in a row', async () => { + const result = await mirrorWith([{ error: 403 }]); + assert.equal(result.ok, false); + assert.match(result.message, /failed 3 times in a row/); + }); + + it('reads every page of jobs', async () => { + const page1 = Array.from({ length: 100 }, (_, i) => ({ id: i + 1, name: `job ${i}`, status: 'completed', conclusion: 'success' })); + let jobCalls = 0; + const fetch = async (url) => { + if (url.includes('/actions/workflows/')) return { ok: true, json: async () => ({ workflow_runs: [pushRun()] }) }; + jobCalls += 1; + const jobs = new URL(url).searchParams.get('page') === '1' ? page1 : [ciOk('completed', 'success')]; + return { ok: true, json: async () => ({ total_count: 101, jobs }) }; + }; + const result = await mirror({ sha: SHA, repo: REPO, token: 't', fetch, clock: fakeClock() }); + assert.equal(result.ok, true, result.message); + assert.equal(jobCalls, 2); + }); +}); + +describe('assess', () => { + it('waits for a missing run only within the grace', () => { + assert.equal(assess({ run: null, check: null, sha: SHA, elapsed: 0 }).done, false); + assert.equal(assess({ run: null, check: null, sha: SHA, elapsed: GRACE_MS }).done, true); + }); + + it('passes only a completed ci-ok whose conclusion is success', () => { + for (const conclusion of ['failure', 'cancelled', 'skipped', 'timed_out', 'neutral', 'action_required']) { + const result = assess({ run: pushRun(), check: ciOk('completed', conclusion), sha: SHA, elapsed: 0 }); + assert.deepEqual([result.done, result.ok], [true, false], conclusion); + } + const green = assess({ run: pushRun(), check: ciOk('completed', 'success'), sha: SHA, elapsed: 0 }); + assert.deepEqual([green.done, green.ok], [true, true]); + }); +}); diff --git a/scripts/ci/workflow-jobs.test.mjs b/scripts/ci/workflow-jobs.test.mjs index 49e0138a2..0e45448ed 100644 --- a/scripts/ci/workflow-jobs.test.mjs +++ b/scripts/ci/workflow-jobs.test.mjs @@ -245,6 +245,213 @@ describe('test.yml', () => { } }); +/** A job's if: condition on one line, without its comments; null when it has none. */ +function conditionOf(text) { + const match = /^ {4}if: (.*)\n((?: {6}\S.*\n)*)/m.exec(withoutComments(text)); + if (!match) return null; + const head = match[1].trim(); + return (/^[>|]-?$/.test(head) ? match[2] : head).replace(/\s+/g, ' ').trim(); +} + +/** The steps of a job by their name: line, each with its text. */ +const namedSteps = (text) => + stepsOf(text).map((step) => ({ name: /^ {6}- name: (.+)$/m.exec(step)?.[1]?.trim() ?? null, text: step })); + +const MIRROR_ON = "needs.changes.outputs.mirror == 'true'"; +const STATUS_FUNCTION = /\b(always|cancelled|failure)\(\)/; +// The terms that are false in mirror mode, where every area is off. +const AREA_TERM = + /^(needs\.changes\.outputs\.(code|image|deploy) == 'true'|needs\.changes\.outputs\.mirror != 'true')$/; + +/** Splits an expression at a top-level operator, outside parentheses and quotes. */ +function splitTop(expr, op) { + const parts = []; + let depth = 0; + let quoted = false; + let start = 0; + for (let i = 0; i < expr.length; i += 1) { + const c = expr[i]; + if (c === "'") quoted = !quoted; + else if (quoted) continue; + else if (c === '(') depth += 1; + else if (c === ')') depth -= 1; + else if (depth === 0 && expr.startsWith(op, i)) { + parts.push(expr.slice(start, i)); + start = i + op.length; + i += op.length - 1; + } + } + parts.push(expr.slice(start)); + return parts.map((part) => part.trim()); +} + +/** True when the whole expression sits in one pair of parentheses. */ +function wrapped(expr) { + if (!expr.startsWith('(')) return false; + let depth = 0; + for (let i = 0; i < expr.length; i += 1) { + if (expr[i] === '(') depth += 1; + else if (expr[i] === ')') { + depth -= 1; + if (depth === 0) return i === expr.length - 1; + } + } + return false; +} + +/** + * True only when the condition is false in mirror mode: every top-level || + * alternative has an area term in its top-level && chain. Anything it cannot + * read that way (a negation, an || with another term) counts as running. + */ +function falseInMirror(condition) { + let expr = condition.trim(); + const braced = /^\$\{\{([\s\S]*)\}\}$/.exec(expr); + if (braced) expr = braced[1].trim(); + while (wrapped(expr)) expr = expr.slice(1, -1).trim(); + const alternatives = splitTop(expr, '||'); + if (alternatives.length > 1) return alternatives.every(falseInMirror); + const conjuncts = splitTop(expr, '&&'); + if (conjuncts.length > 1) return conjuncts.some(falseInMirror); + return AREA_TERM.test(expr); +} + +// On the release pull request changes turns every area off and mirror on, and +// ci-ok mirrors the push run on dev instead of the jobs below testing the same +// commit again (scripts/ci/changed-areas.mjs decides, its tests hold that every +// area is off in mirror mode). +describe('the release pull request mirror', () => { + const byId = new Map(jobs().map((job) => [job.id, job])); + const ciOk = byId.get('ci-ok'); + + /** Why a job would still run in mirror mode; null when it skips. */ + function runsInMirrorMode(id, seen = new Set()) { + const job = byId.get(id); + if (!job) return `${id}: no such job`; + const condition = conditionOf(job.text); + if (condition && STATUS_FUNCTION.test(condition)) return `${id}: its if: (${condition}) runs past skipped needs`; + if (condition && falseInMirror(condition)) return null; + const needs = needsOf(job.text).concat(/^ {4}needs: ([a-z0-9-]+)\s*$/m.exec(job.text)?.[1] ?? []); + // A job without an area condition skips only when every job it needs skips. + const upstream = needs.filter((need) => need !== 'changes'); + if (upstream.length === 0) return `${id}: give it an area condition on needs.changes.outputs`; + if (seen.has(id)) return null; + seen.add(id); + const reasons = upstream.map((need) => runsInMirrorMode(need, seen)).filter(Boolean); + return reasons.length === upstream.length ? `${id}: ${reasons.join('; ')}` : null; + } + + it('reads a condition as skipping only when every alternative is off', () => { + for (const condition of [ + "needs.changes.outputs.code == 'true'", + "needs.changes.outputs.image == 'true' || needs.changes.outputs.deploy == 'true'", + "needs.changes.outputs.code == 'true' && github.actor != 'dependabot[bot]' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)", + "${{ needs.changes.outputs.mirror != 'true' && github.ref == 'refs/heads/dev' }}", + "(needs.changes.outputs.code == 'true' || needs.changes.outputs.image == 'true') && github.event_name == 'push'", + ]) { + assert.equal(falseInMirror(condition), true, condition); + } + for (const condition of [ + "needs.changes.outputs.code == 'true' || github.event_name == 'push'", + "github.repository == 'x/y' || needs.changes.outputs.code == 'true'", + "!(needs.changes.outputs.code == 'true')", + "needs.changes.outputs.code == 'false'", + "needs.changes.outputs.mirror == 'true'", + "(needs.changes.outputs.code == 'true' || github.event_name == 'push') && github.actor != 'x'", + "contains('needs.changes.outputs.code == ''true''', 'x')", + ]) { + assert.equal(falseInMirror(condition), false, condition); + } + }); + + // The push run on dev is what the mirror copies, so it has to cover every + // area the release pull request brings into main: it lists its changes + // against the merge base with main, not against the push before it. + it('has a push to dev list its changes against the merge base with main', () => { + const list = namedSteps(byId.get('changes').text).find((step) => step.name === 'List the changed files'); + assert.ok(list, 'changes must keep its "List the changed files" step'); + const text = withoutComments(list.text); + assert.ok(text.includes('REF: ${{ github.ref }}'), 'the list step must read github.ref'); + assert.match(text, /if \[ "\$REF" = refs\/heads\/dev \]; then\n/); + assert.match( + text, + /git fetch --no-tags --filter=blob:none --unshallow origin \+refs\/heads\/main:refs\/remotes\/origin\/main\s+&& base="\$\(git merge-base origin\/main "\$AFTER"\)"; then\n\s+changed_between "\$base" "\$AFTER"\n/, + ); + // The dev branch is checked first, so the diff against BEFORE only serves main. + assert.ok(text.indexOf('refs/heads/dev') < text.indexOf('changed_between "$BEFORE" "$AFTER"')); + }); + + it('has changes publish the mirror decision', () => { + const changes = byId.get('changes'); + assert.match(changes.text, /^ {6}mirror: \$\{\{ steps\.areas\.outputs\.mirror \}\}$/m); + const decide = namedSteps(changes.text).find((step) => step.name === 'Decide what runs'); + assert.ok(decide, 'changes must keep its "Decide what runs" step'); + for (const [name, value] of [ + ['CI_EVENT', 'github.event_name'], + ['CI_BASE_REF', 'github.base_ref'], + ['CI_HEAD_REF', 'github.head_ref'], + ['CI_HEAD_REPO', 'github.event.pull_request.head.repo.full_name'], + ['CI_REPO', 'github.repository'], + ]) { + assert.ok(decide.text.includes(`${name}: \${{ ${value} }}`), `the decide step must set ${name} from ${value}`); + } + assert.match(decide.text, /node scripts\/ci\/changed-areas\.mjs/); + }); + + it('skips every job but changes and ci-ok', () => { + const problems = jobs() + .filter(({ id }) => id !== 'changes' && id !== 'ci-ok') + .map(({ id }) => runsInMirrorMode(id)) + .filter(Boolean); + assert.deepEqual(problems, []); + }); + + it('skips the scan by its own condition, not only through its needs', () => { + const scan = byId.get('scan'); + assert.ok(needsOf(scan.text).includes('changes'), 'scan must need changes to read its mirror output'); + assert.match(conditionOf(scan.text), /^needs\.changes\.outputs\.mirror != 'true' && /); + }); + + it('runs the mirror in ci-ok only in mirror mode, after the needs verdict', () => { + const steps = namedSteps(ciOk.text); + const verdict = steps.findIndex((step) => step.name === 'Every needed job passed or was skipped'); + const mirrorStep = steps.findIndex((step) => /node scripts\/ci\/mirror-push-run\.mjs/.test(step.text)); + assert.ok(verdict >= 0, 'ci-ok must keep the needs verdict step'); + assert.ok(mirrorStep > verdict, 'ci-ok must run the mirror after the needs verdict'); + assert.doesNotMatch(steps[verdict].text, /^ {8}if:/m, 'the needs verdict must run in both modes'); + for (const step of steps.slice(verdict + 1)) { + assert.match(step.text, new RegExp(`^ {8}if: ${MIRROR_ON.replace(/[.()]/g, '\\$&')}$`, 'm'), step.text); + } + assert.match(steps[mirrorStep].text, /HEAD_SHA: \$\{\{ github\.event\.pull_request\.head\.sha \}\}/); + }); + + it('gives ci-ok read access to actions, checks and contents only', () => { + const block = /^ {4}permissions:\n((?: {6}.+\n)+)/m.exec(withoutComments(ciOk.text)); + assert.ok(block, 'ci-ok must set its own permissions'); + const granted = block[1].trim().split('\n').map((line) => line.trim()).sort(); + assert.deepEqual(granted, ['actions: read', 'checks: read', 'contents: read']); + }); + + it('lets the mirror end on its own before ci-ok is killed', async () => { + const { TIMEOUT_MS } = await import('./mirror-push-run.mjs'); + const job = Number(/^ {4}timeout-minutes: (\d+)\s*$/m.exec(ciOk.text)[1]); + const step = Number( + /^ {8}timeout-minutes: (\d+)\s*$/m.exec(namedSteps(ciOk.text).find((s) => /mirror-push-run/.test(s.text)).text)[1], + ); + assert.ok(TIMEOUT_MS / 60_000 + 2 <= step, `the mirror gives up after ${TIMEOUT_MS / 60_000} min, its step allows ${step}`); + assert.ok(step < job, `the mirror step allows ${step} min, the job ${job}`); + }); + + // A newer push to dev must cancel the mirror of the older commit too, so + // both runs share a group per ref and cancel in progress. + it('cancels a superseded run per ref', () => { + assert.match( + workflow, + /\nconcurrency:\n {2}group: \$\{\{ github\.workflow \}\}-\$\{\{ github\.ref \}\}\n {2}cancel-in-progress: true\n/, + ); + }); +}); + describe('the setup action', () => { const steps = setupAction.split(/\n(?= {4}- )/).slice(1);