mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-24 09:01:28 +01:00
Pull requests no longer wait on the upload that sends Free Tests results to Trunk. It now runs on its own after Free Tests finishes. The quick test tier's flaky passes now reach Trunk too, so a test that fails once and passes on its retry shows up there wherever it ran. Nothing to do: no required check changes, and the upload still never affects whether a check passes. Refs: GST-3303 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/red-team#2 reader=red-team
206 lines
17 KiB
TypeScript
206 lines
17 KiB
TypeScript
/**
|
|
* CI reuse wiring (GSTA-156 P3b) for free-tests.yml and windows-free-tests.yml. Pins the properties that make reuse
|
|
* safe: nothing a pull request controls certifies it (a push to main consumes; free-tests.yml's pull_request runs consume
|
|
* only through find --pull-request run from main's checkout, whose sources and guarded files test/ci-tree-reuse.test.ts
|
|
* pins; a sizing-experiment dispatch records sizing_experiment and is never evidence; dispatch runs never consume), the reuse job alone reads Actions with a job-level actions: read,
|
|
* a skipped or failed reuse job runs every job exactly as before (GitHub skips a job whose needed job was skipped
|
|
* unless its condition uses a status function, so every dependent condition starts with !cancelled()), the
|
|
* aggregate's reuse branch runs before any download and the strict path is otherwise unchanged, tested-tree is
|
|
* uploaded only after strict verification, and the run profiles in .gstack/pregate.json name real jobs.
|
|
*/
|
|
import { describe, expect, test } from 'bun:test';
|
|
import { spawnSync } from 'node:child_process';
|
|
import * as fs from 'node:fs';
|
|
import * as os from 'node:os';
|
|
import * as path from 'node:path';
|
|
import { parseWorkflow } from '../lib/pregate/workflows';
|
|
|
|
const ROOT = path.resolve(import.meta.dir, '..');
|
|
const read = (rel: string) => fs.readFileSync(path.join(ROOT, rel), 'utf8');
|
|
const MAIN_PUSH = "github.event_name == 'push' && github.ref == 'refs/heads/main'";
|
|
const FILES = {
|
|
'free-tests.yml': { aggregate: 'free-tests', profile: 'ci-reuse/free-tests', plan: 'free-plan', exempt: ['trunk-dynamic-ci', 'docs-scope', 'docs-lane', 'free-plan'], observer: 'trunk-dynamic-ci', manual: false, reuseIf: `(${MAIN_PUSH}) || github.event_name == 'pull_request'`, pullRequest: true, docsScope: true, skip: "steps.route.outputs.skip != 'true'" },
|
|
'windows-free-tests.yml': { aggregate: 'windows-free-tests', profile: 'ci-reuse/windows-free-tests', plan: 'windows-plan', exempt: ['windows-record-durations'], observer: null, manual: true, reuseIf: MAIN_PUSH, pullRequest: false, docsScope: false, skip: "steps.route.outputs.reused != 'true'" },
|
|
};
|
|
const PROFILES = JSON.parse(read('.gstack/pregate.json')).run_profiles as Record<string, { workflow: string; jobs: string[]; artifact: string; events: string[]; os: string }>;
|
|
|
|
/** Evaluate a job condition the way the runner would for one reuse outcome (status functions true: nothing cancelled). */
|
|
function runs(cond: string, s: { result: string; reused: string; plan?: string; docsScope?: string; docsOnly?: string }): boolean {
|
|
const expr = cond.replace(/^\$\{\{\s*|\s*\}\}$/g, '')
|
|
.replace(/!cancelled\(\)|always\(\)/g, 'true')
|
|
.replace(/needs\.reuse\.result/g, JSON.stringify(s.result)).replace(/needs\.reuse\.outputs\.reused/g, JSON.stringify(s.reused))
|
|
.replace(/needs\.(free|windows)-plan\.result/g, JSON.stringify(s.plan ?? 'success')).replace(/inputs\.record_durations/g, 'false')
|
|
.replace(/needs\.docs-scope\.result/g, JSON.stringify(s.docsScope ?? 'skipped')).replace(/needs\.docs-scope\.outputs\.docs_only/g, JSON.stringify(s.docsOnly ?? ''));
|
|
expect(expr).not.toMatch(/needs\.|inputs\.|github\.|steps\./);
|
|
return new Function(`return (${expr});`)() as boolean;
|
|
}
|
|
|
|
for (const [file, spec] of Object.entries(FILES)) {
|
|
describe(`${file} main-push reuse wiring`, () => {
|
|
const source = read(`.github/workflows/${file}`);
|
|
const wf = Bun.YAML.parse(source) as any;
|
|
const jobs = wf.jobs as Record<string, any>;
|
|
const needs = (name: string): string[] => [jobs[name].needs ?? []].flat();
|
|
const suite = Object.keys(jobs).filter(n => n !== 'reuse' && n !== spec.aggregate && !spec.exempt.includes(n));
|
|
|
|
test(spec.pullRequest ? 'a push to main consumes, and a pull request only through find --pull-request (never a run it controls), with job-level actions: read on the reuse job alone and no secrets' : 'only a push to main consumes, with job-level actions: read on the reuse job alone and no secrets', () => {
|
|
expect(jobs.reuse.if).toBe(spec.reuseIf);
|
|
expect(jobs.reuse.permissions).toEqual({ contents: 'read', actions: 'read' });
|
|
expect(wf.permissions).toEqual({ contents: 'read' });
|
|
for (const name of Object.keys(jobs).filter(n => n !== 'reuse' && n !== spec.observer)) expect([name, jobs[name].permissions]).toEqual([name, undefined]);
|
|
if (spec.observer) expect([jobs[spec.observer].permissions, jobs[spec.observer].needs, jobs[spec.observer].steps.some((s: any) => s.uses?.startsWith('actions/checkout@'))]).toEqual([{}, undefined, false]);
|
|
expect(source.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi) ?? []).toEqual(spec.observer ? ['secrets.TRUNK_API_TOKEN'] : []);
|
|
expect(source).not.toContain('pull_request_target');
|
|
const find = jobs.reuse.steps.find((s: any) => s.id === 'find');
|
|
expect(find.env).toEqual({ GH_TOKEN: '${{ github.token }}' });
|
|
expect(find.run).toBe(spec.pullRequest ? `bun --no-env-file --config=/dev/null run scripts/ci-tree-reuse.ts find --workflow ${file} --profile ${spec.profile} --pull-request` : `bun run scripts/ci-tree-reuse.ts find --workflow ${file} --profile ${spec.profile}`);
|
|
expect(jobs.reuse.steps[0].with['persist-credentials']).toBe(false);
|
|
if (spec.pullRequest) expect(jobs.reuse.steps[0].with.ref).toBe('${{ github.event.pull_request.base.sha || github.sha }}');
|
|
expect(Object.keys(jobs.reuse.outputs).sort()).toEqual(['reused', 'run_id', 'run_url', 'tree']);
|
|
});
|
|
|
|
test('every suite job needs reuse and runs unless reuse succeeded with reused=true; a skipped or failed reuse runs them all', () => {
|
|
expect(suite.length).toBeGreaterThan(1);
|
|
for (const name of suite) {
|
|
expect([name, needs(name)]).toEqual([name, expect.arrayContaining(['reuse'])]);
|
|
const cond = jobs[name].if as string;
|
|
expect([name, cond]).toEqual([name, expect.stringMatching(/^\$\{\{ !cancelled\(\) && /)]);
|
|
expect([name, runs(cond, { result: 'skipped', reused: '' })]).toEqual([name, true]);
|
|
expect([name, runs(cond, { result: 'success', reused: 'false' })]).toEqual([name, true]);
|
|
expect([name, runs(cond, { result: 'failure', reused: '' })]).toEqual([name, true]);
|
|
expect([name, runs(cond, { result: 'failure', reused: 'true' })]).toEqual([name, true]);
|
|
expect([name, runs(cond, { result: 'success', reused: 'true' })]).toEqual([name, false]);
|
|
}
|
|
for (const name of suite.filter(n => needs(n).includes(spec.plan))) expect([name, runs(jobs[name].if, { result: 'skipped', reused: '', plan: 'failure' })]).toEqual([name, false]);
|
|
const docsGated = suite.filter(n => needs(n).includes('docs-scope'));
|
|
expect(docsGated.length > 0).toBe(spec.docsScope);
|
|
for (const name of docsGated) {
|
|
expect([name, runs(jobs[name].if, { result: 'skipped', reused: '', docsScope: 'success', docsOnly: 'true' })]).toEqual([name, false]);
|
|
expect([name, runs(jobs[name].if, { result: 'skipped', reused: '', docsScope: 'failure', docsOnly: 'true' })]).toEqual([name, true]);
|
|
expect([name, runs(jobs[name].if, { result: 'skipped', reused: '', docsScope: 'success', docsOnly: 'false' })]).toEqual([name, true]);
|
|
}
|
|
expect(needs(spec.aggregate)).toContain('reuse');
|
|
expect(runs(jobs[spec.aggregate].if, { result: 'success', reused: 'true' })).toBe(true);
|
|
});
|
|
|
|
test('every job downstream of reuse uses a status function, so a skipped reuse never skips it', () => {
|
|
const downstream = (name: string): boolean => needs(name).some(n => n === 'reuse' || downstream(n));
|
|
for (const name of Object.keys(jobs).filter(downstream)) expect([name, jobs[name].if]).toEqual([name, expect.stringMatching(/!cancelled\(\)|always\(\)/)]);
|
|
});
|
|
|
|
test(spec.manual ? 'pregate reads every test lane as manual: the nightly Windows suite is never a PR obligation (windows-smoke.yml is)' : 'pregate still sees every test lane as required (no condition reads inputs or workflow_dispatch)', () => {
|
|
const lanes = parseWorkflow(file, source, 'linux').filter(l => l.runsTests && !spec.exempt.includes(l.job));
|
|
expect(lanes.length).toBeGreaterThan(0);
|
|
for (const lane of lanes) expect([lane.id, lane.manual]).toEqual([lane.id, spec.manual]);
|
|
});
|
|
|
|
test('the aggregate routes first, before any download; the strict path follows unchanged and alone uploads tested-tree after verification', () => {
|
|
const steps = jobs[spec.aggregate].steps as any[];
|
|
expect(steps[0].id).toBe('route');
|
|
expect(steps[0].if).toBeUndefined();
|
|
for (const step of steps.slice(1)) expect([step.name ?? step.uses, step.if]).toEqual([step.name ?? step.uses, spec.skip]);
|
|
const verify = steps.findIndex(s => s.run?.includes('--ci-verify'));
|
|
const record = steps.findIndex(s => s.run?.includes('scripts/ci-tree-reuse.ts record'));
|
|
const upload = steps.findIndex(s => s.with?.name === 'tested-tree');
|
|
expect(steps.findIndex(s => s.uses?.startsWith('actions/download-artifact@'))).toBeGreaterThan(0);
|
|
expect(verify).toBeGreaterThan(0);
|
|
expect(record).toBe(verify + 1);
|
|
expect(upload).toBe(record + 1);
|
|
expect(steps.length).toBe(upload + 1);
|
|
expect(steps[upload].uses).toStartWith('actions/upload-artifact@');
|
|
expect(steps[upload].with['if-no-files-found']).toBe('error');
|
|
expect(steps[record].env).toEqual({ PR_HEAD_SHA: '${{ github.event.pull_request.head.sha }}', ...(spec.pullRequest ? { SIZING_OVERRIDE: '${{ inputs.free_runner }}${{ inputs.free_shards }}${{ inputs.free_jobs }}' } : {}) });
|
|
expect(steps[1].run).toMatch(/set -eu\n(test "\$[A-Z_]+_RESULT" = success\n)+$/);
|
|
});
|
|
|
|
test.skipIf(!Bun.which('bash'))(`the route step takes the reuse branch only for a well-formed reuse on a main push${spec.pullRequest ? ' or a pull request' : ''}, and fails rather than passes on anything else claiming reuse`, () => {
|
|
const route = jobs[spec.aggregate].steps[0];
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'reuse route '));
|
|
const shell = (p: string) => p.split(path.sep).join('/');
|
|
try {
|
|
const go = (env: Record<string, string>) => {
|
|
const out = path.join(dir, 'out'); const summary = path.join(dir, 'summary');
|
|
fs.writeFileSync(out, ''); fs.writeFileSync(summary, '');
|
|
const r = spawnSync('bash', ['-e', '-c', route.run], { env: { PATH: process.env.PATH ?? '', GITHUB_OUTPUT: shell(out), GITHUB_STEP_SUMMARY: shell(summary), GITHUB_EVENT_NAME: 'push', GITHUB_REF: 'refs/heads/main', GITHUB_SERVER_URL: 'https://github.com', GITHUB_REPOSITORY: 'garrytan/gstack', REUSE_RESULT: '', REUSED: '', REUSE_RUN_ID: '', REUSE_RUN_URL: '', REUSE_TREE: '', DOCS_SCOPE_RESULT: '', DOCS_ONLY: '', DOCS_LANE_RESULT: '', ...env }, encoding: 'utf8', timeout: 5000 });
|
|
return { status: r.status, out: fs.readFileSync(out, 'utf8'), summary: fs.readFileSync(summary, 'utf8') };
|
|
};
|
|
const good = { REUSE_RESULT: 'success', REUSED: 'true', REUSE_RUN_ID: '123', REUSE_RUN_URL: 'https://github.com/garrytan/gstack/actions/runs/123', REUSE_TREE: 'a'.repeat(40) };
|
|
const routed = (reused: boolean) => `reused=${reused}\n${spec.docsScope ? `skip=${reused}\n` : ''}`;
|
|
expect(go({ REUSE_RESULT: 'skipped', GITHUB_EVENT_NAME: 'pull_request' })).toEqual({ status: 0, out: routed(false), summary: '' });
|
|
expect(go({ REUSE_RESULT: 'success', REUSED: 'false' })).toEqual({ status: 0, out: routed(false), summary: '' });
|
|
expect(go({ ...good, REUSE_RESULT: 'failure' })).toEqual({ status: 0, out: routed(false), summary: '' });
|
|
const reused = go(good);
|
|
expect([reused.status, reused.out]).toEqual([0, routed(true)]);
|
|
expect(reused.summary).toContain('Reused https://github.com/garrytan/gstack/actions/runs/123');
|
|
const prRun = { GITHUB_EVENT_NAME: 'pull_request', GITHUB_REF: 'refs/pull/7/merge' };
|
|
if (spec.pullRequest) expect(go({ ...good, ...prRun }).out).toBe(routed(true));
|
|
if (spec.docsScope) {
|
|
const docs = { ...prRun, DOCS_SCOPE_RESULT: 'success', DOCS_ONLY: 'true', DOCS_LANE_RESULT: 'success' };
|
|
const passed = go(docs);
|
|
expect([passed.status, passed.out]).toEqual([0, 'reused=false\nskip=true\n']);
|
|
expect(passed.summary).toContain('Docs-only pull request');
|
|
for (const bad of <Record<string, string>[]>[{ DOCS_LANE_RESULT: 'failure' }, { DOCS_LANE_RESULT: 'skipped' }, { GITHUB_EVENT_NAME: 'push', GITHUB_REF: 'refs/heads/main' }]) {
|
|
const r = go({ ...docs, ...bad });
|
|
expect([JSON.stringify(bad), r.status === 0, r.out]).toEqual([JSON.stringify(bad), false, '']);
|
|
}
|
|
expect(go({ ...docs, DOCS_SCOPE_RESULT: 'failure', REUSE_RESULT: 'skipped' }).out).toBe(routed(false));
|
|
}
|
|
for (const bad of <Record<string, string>[]>[{ REUSE_RUN_ID: '' }, { REUSE_RUN_ID: '12x' }, { REUSE_RUN_URL: 'https://github.com/other/repo/actions/runs/123' }, { REUSE_TREE: '' }, { GITHUB_EVENT_NAME: 'pull_request' }, { GITHUB_REF: 'refs/heads/feature' }, { GITHUB_EVENT_NAME: 'workflow_dispatch' }, ...(spec.pullRequest ? [] : [prRun])]) {
|
|
const r = go({ ...good, ...bad });
|
|
expect([JSON.stringify(bad), r.status === 0, r.out]).toEqual([JSON.stringify(bad), false, '']);
|
|
}
|
|
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test('its run profile names this workflow, its real non-matrix jobs and the uploaded artifact, and is never named after a lane (lane clears keep their URL path until GST-555)', () => {
|
|
const profiles = Object.entries(PROFILES).filter(([, p]) => p.workflow === file);
|
|
expect(profiles.map(([n]) => n)).toContain(spec.profile);
|
|
for (const [name, p] of profiles) {
|
|
expect([name, p.events.sort()]).toEqual([name, ['pull_request', 'workflow_dispatch']]);
|
|
expect([name, p.artifact]).toEqual([name, 'tested-tree']);
|
|
for (const job of p.jobs) expect([name, job, !!jobs[job] && !jobs[job].strategy]).toEqual([name, job, true]);
|
|
expect(p.jobs).toContain(spec.aggregate);
|
|
expect([name, /\.ya?ml\//.test(name)]).toEqual([name, false]);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
test('free-tests.yml plans beside reuse and docs-scope, and every job that reads the plan still waits for both and skips on a reuse or a docs-only pull request (GST-3303)', () => {
|
|
const jobs = (Bun.YAML.parse(read('.github/workflows/free-tests.yml')) as any).jobs as Record<string, any>;
|
|
expect([jobs['free-plan'].needs, jobs['free-plan'].if]).toEqual([undefined, undefined]);
|
|
const readers = Object.keys(jobs).filter(n => n !== 'free-tests' && JSON.stringify(jobs[n]).includes('needs.free-plan'));
|
|
expect(readers).toEqual(['free-suite']);
|
|
for (const name of readers) {
|
|
expect([name, [jobs[name].needs].flat()]).toEqual([name, expect.arrayContaining(['free-plan', 'reuse', 'docs-scope'])]);
|
|
expect(runs(jobs[name].if, { result: 'success', reused: 'true' })).toBe(false);
|
|
expect(runs(jobs[name].if, { result: 'skipped', reused: '', docsScope: 'success', docsOnly: 'true' })).toBe(false);
|
|
expect(runs(jobs[name].if, { result: 'skipped', reused: '', plan: 'failure' })).toBe(false);
|
|
expect(runs(jobs[name].if, { result: 'success', reused: 'false', docsScope: 'success', docsOnly: 'false' })).toBe(true);
|
|
}
|
|
});
|
|
|
|
test('every skippable job in the ci-reuse/free-tests profile skips on a docs-only pull request, so a docs-only run still carries as not owed (GST-3303)', () => {
|
|
const jobs = (Bun.YAML.parse(read('.github/workflows/free-tests.yml')) as any).jobs as Record<string, any>;
|
|
const skippable = (JSON.parse(read('.gstack/pregate.json')).run_profiles['ci-reuse/free-tests'].skippable.jobs) as string[];
|
|
for (const name of skippable) {
|
|
expect([name, [jobs[name].needs].flat()]).toEqual([name, expect.arrayContaining(['docs-scope'])]);
|
|
expect([name, String(jobs[name].if ?? '')]).toEqual([name, expect.stringContaining("needs.docs-scope.outputs.docs_only == 'true'")]);
|
|
}
|
|
});
|
|
|
|
test('every workflow that calls one of these grants the calling job actions: read, which the reuse job needs even when it is skipped', () => {
|
|
const dir = path.join(ROOT, '.github/workflows');
|
|
const callers: string[] = [];
|
|
for (const name of fs.readdirSync(dir).filter(f => /\.ya?ml$/.test(f))) {
|
|
const jobs = (Bun.YAML.parse(read(`.github/workflows/${name}`)) as any)?.jobs ?? {};
|
|
for (const [id, job] of Object.entries(jobs) as Array<[string, any]>) {
|
|
const called = /^\.\/\.github\/workflows\/(.+)$/.exec(job?.uses ?? '')?.[1];
|
|
if (!called || !(called in FILES)) continue;
|
|
callers.push(`${name}/${id}`);
|
|
expect([`${name}/${id}`, job.permissions?.actions]).toEqual([`${name}/${id}`, expect.stringMatching(/^(read|write)$/)]);
|
|
}
|
|
}
|
|
expect(callers).toContain('free-durations-record.yml/windows');
|
|
});
|