mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
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.
This commit is contained in:
@@ -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 <list> one changed path per line
|
||||
// node scripts/ci/changed-areas.mjs --all no usable list, run everything
|
||||
//
|
||||
// Prints `<area>=true|false` per area and appends the same lines to
|
||||
// $GITHUB_OUTPUT when it is set.
|
||||
// Prints `<area>=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<typeof mirrorsPushRun>[0]} context
|
||||
* @returns {Record<string, boolean>}
|
||||
*/
|
||||
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`);
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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=<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<void> }} 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);
|
||||
});
|
||||
}
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user