mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-24 09:01:28 +01:00
Trunk flaky-test upload runs after Free Tests, off the pull request's checks (GST-3303) (#4579)
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
This commit is contained in:
1 parent
92c57ca4ce
commit
faae7f725a
12 files changed
+214
-119
No files matched your search
@@ -19,8 +19,8 @@ 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', 'trunk-flaky-tests', 'docs-scope', 'docs-lane', 'free-plan'], observer: 'trunk-dynamic-ci', uploader: 'trunk-flaky-tests', 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, uploader: null, manual: true, reuseIf: MAIN_PUSH, pullRequest: false, docsScope: false, skip: "steps.route.outputs.reused != 'true'" },
|
||||
'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 }>;
|
||||
|
||||
@@ -47,10 +47,9 @@ for (const [file, spec] of Object.entries(FILES)) {
|
||||
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 && n !== spec.uploader)) expect([name, jobs[name].permissions]).toEqual([name, undefined]);
|
||||
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]);
|
||||
if (spec.uploader) expect([jobs[spec.uploader].permissions, jobs[spec.uploader].needs, jobs[spec.uploader].steps.some((s: any) => s.uses?.startsWith('actions/checkout@'))]).toEqual([{}, 'free-suite', false]);
|
||||
expect(source.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi) ?? []).toEqual(spec.observer ? ['secrets.TRUNK_API_TOKEN', 'secrets.TRUNK_ORG_API_TOKEN'] : []);
|
||||
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 }}' });
|
||||
|
||||
@@ -403,7 +403,7 @@ describe('CSO runtime staging gates', () => {
|
||||
const gate = job.steps.find((step: any) => step.run?.trim() === 'bun test --max-concurrency 1 test/cso-docker-integration.test.ts');
|
||||
expect(gate.env.GSTACK_CSO_DOCKER_TESTS).toBe('1');
|
||||
expect(JSON.stringify(job)).not.toContain('secrets.');
|
||||
expect(raw.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi)).toEqual(['secrets.TRUNK_API_TOKEN', 'secrets.TRUNK_ORG_API_TOKEN']);
|
||||
expect(raw.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi)).toEqual(['secrets.TRUNK_API_TOKEN']);
|
||||
expect(job.steps.map((s: any) => s.run ?? '').join('\n')).toContain('docker --host unix:///var/run/docker.sock info');
|
||||
expect(job['continue-on-error']).not.toBe(true);
|
||||
expect(gate['continue-on-error']).not.toBe(true);
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
* `bun test <dirs>` glob — the runner owns TEST_ROOTS and strict-output
|
||||
* classification, so a truncated run can't report green.
|
||||
* 2. It is SECRETLESS where code runs: free tests make no API calls, and
|
||||
* keeping keys out means fork PRs get real signal here. The two allowed
|
||||
* `secrets.` references are TRUNK_API_TOKEN in the learning-only
|
||||
* trunk-dynamic-ci observer and TRUNK_ORG_API_TOKEN in the detection-only
|
||||
* trunk-flaky-tests uploader. Neither checks out or runs anything of the
|
||||
* PR's and no job depends on either; any other reference is a regression.
|
||||
* keeping keys out means fork PRs get real signal here. The one allowed
|
||||
* `secrets.` reference is TRUNK_API_TOKEN in the learning-only
|
||||
* trunk-dynamic-ci observer, which checks out and runs nothing of the
|
||||
* PR's and no job depends on; any other reference is a regression. The
|
||||
* detection-only Trunk uploader runs after the run, in trunk-flaky-tests.yml.
|
||||
* 3. It triggers on `pull_request` (never `pull_request_target`, which
|
||||
* would hand a fork PR the base repo's context).
|
||||
*
|
||||
@@ -39,9 +39,8 @@ describe('free-tests workflow wiring', () => {
|
||||
|
||||
test('secretless: no secrets reach the free lane', () => {
|
||||
const jobs = (Bun.YAML.parse(source) as any).jobs;
|
||||
expect(source.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi)).toEqual(['secrets.TRUNK_API_TOKEN', 'secrets.TRUNK_ORG_API_TOKEN']);
|
||||
expect(source.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi)).toEqual(['secrets.TRUNK_API_TOKEN']);
|
||||
expect(jobs['trunk-dynamic-ci'].env).toEqual({ TRUNK_API_TOKEN: '${{ secrets.TRUNK_API_TOKEN }}' });
|
||||
expect(jobs['trunk-flaky-tests'].env).toEqual({ TRUNK_ORG_API_TOKEN: '${{ secrets.TRUNK_ORG_API_TOKEN }}' });
|
||||
expect(source).not.toContain('ANTHROPIC_API_KEY');
|
||||
expect(source).not.toContain('OPENAI_API_KEY');
|
||||
});
|
||||
@@ -68,13 +67,12 @@ describe('free-tests workflow wiring', () => {
|
||||
for (const key of step.with['job-keys'].split(',')) expect(jobs[key], key).toBeDefined();
|
||||
for (const name of Object.keys(jobs).filter(n => n !== 'trunk-dynamic-ci')) {
|
||||
expect([name, [jobs[name].needs ?? []].flat()]).not.toEqual([name, expect.arrayContaining(['trunk-dynamic-ci'])]);
|
||||
if (name === 'trunk-flaky-tests') continue;
|
||||
expect([name, JSON.stringify(jobs[name])]).not.toEqual([name, expect.stringMatching(/trunk|dynamic-ci/i)]);
|
||||
}
|
||||
});
|
||||
|
||||
// Value: protects=GST-1859's detection-only contract: Trunk Flaky Tests receives every free-suite shard's JUnit, failures included, and can never quarantine, skip, gate or fail anything, while the token never shares a job with pull-request code; fails_when=the suite stops writing or uploading JUnit, the uploader job checks out or runs code, gains token scopes, is needed or read by any job, loses fail-open or its token skip, turns quarantine on, runs on forks, or either action is unpinned; seam=.github/workflows/free-tests.yml; no_claim=what Trunk flags
|
||||
test('trunk-flaky-tests is a detection-only uploader: pinned, scope-less, fail-open, quarantine off, read by no job', () => {
|
||||
test('trunk-flaky-tests.yml is a detection-only uploader after the run: pinned, read-only token, fail-open, quarantine off, never on forks', () => {
|
||||
const jobs = (Bun.YAML.parse(source) as any).jobs;
|
||||
const suite = jobs['free-suite'];
|
||||
expect(suite.steps.find((step: any) => step.name === 'Run free suite').run).toContain('--junit-dir "$RUNNER_TEMP/free-junit"');
|
||||
@@ -85,32 +83,51 @@ describe('free-tests workflow wiring', () => {
|
||||
const quick = jobs['quick-bun-default'].steps as any[];
|
||||
const quickRun = quick.findIndex(step => step.name === 'Run the quick free tier on the install-default Bun');
|
||||
expect(quick[quickRun].env).toEqual({ GSTACK_FREE_RETRY_FLAKY: '1', GSTACK_FLAKE_LEDGER: '${{ runner.temp }}/flake-ledger.jsonl' });
|
||||
expect(quick[quickRun].run).toBe('xvfb-run -a bun run test:quick --junit-dir "$RUNNER_TEMP/quick-junit"');
|
||||
const quickLedger = quick.find(step => step.with?.name === 'flake-ledger-quick');
|
||||
expect(quick.indexOf(quickLedger)).toBeGreaterThan(quickRun);
|
||||
expect(quickLedger.if).toBe('always()');
|
||||
expect(quickLedger.uses).toMatch(/^actions\/upload-artifact@[0-9a-f]{40}$/);
|
||||
expect(quickLedger.with).toEqual({ name: 'flake-ledger-quick', path: '${{ runner.temp }}/flake-ledger.jsonl', 'if-no-files-found': 'ignore', 'retention-days': 90 });
|
||||
const flaky = jobs['trunk-flaky-tests'];
|
||||
expect(flaky.needs).toBe('free-suite');
|
||||
expect(flaky.if).toBe("always() && needs.free-suite.result != 'skipped' && ((github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository))");
|
||||
const quickJunit = quick.find(step => step.with?.name === 'quick-junit');
|
||||
expect(quick.indexOf(quickJunit)).toBeGreaterThan(quickRun);
|
||||
expect(quickJunit.if).toBe('always()');
|
||||
expect(quickJunit.uses).toMatch(/^actions\/upload-artifact@[0-9a-f]{40}$/);
|
||||
expect(quickJunit.with).toEqual({ name: 'quick-junit', path: '${{ runner.temp }}/quick-junit/*.xml', 'if-no-files-found': 'ignore', 'retention-days': 7 });
|
||||
for (const name of Object.keys(jobs)) expect([name, /TRUNK_ORG_API_TOKEN|analytics-uploader|download-artifact[^"]*"[^}]*free-junit-\*/.test(JSON.stringify(jobs[name]))]).toEqual([name, false]);
|
||||
|
||||
const upSource = fs.readFileSync(path.resolve(import.meta.dir, '..', '.github', 'workflows', 'trunk-flaky-tests.yml'), 'utf-8');
|
||||
const up = Bun.YAML.parse(upSource) as any;
|
||||
expect(up.on).toEqual({ workflow_run: { workflows: ['Free Tests'], types: ['completed'] } });
|
||||
expect(up.permissions).toEqual({});
|
||||
expect(Object.keys(up.jobs)).toEqual(['trunk-flaky-tests']);
|
||||
expect(upSource.match(/secrets\s*(?:\.|\[)\s*['"]?[A-Za-z0-9_-]*|toJSON\(\s*secrets\b/gi)).toEqual(['secrets.TRUNK_ORG_API_TOKEN']);
|
||||
expect(upSource).not.toContain('pull_request_target');
|
||||
const flaky = up.jobs['trunk-flaky-tests'];
|
||||
expect(flaky.if).toBe("github.event.workflow_run.conclusion != 'skipped' && ((github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'main') || github.event.workflow_run.event == 'workflow_dispatch' || (github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.head_repository.full_name == github.repository))");
|
||||
expect(flaky['runs-on']).toBe('ubuntu-24.04');
|
||||
expect(flaky.permissions).toEqual({});
|
||||
expect(flaky.permissions).toEqual({ actions: 'read' });
|
||||
expect(flaky.env).toEqual({ TRUNK_ORG_API_TOKEN: '${{ secrets.TRUNK_ORG_API_TOKEN }}' });
|
||||
expect(flaky['continue-on-error']).toBe(true);
|
||||
expect(flaky['timeout-minutes']).toBe(10);
|
||||
expect(flaky.outputs).toBeUndefined();
|
||||
expect(flaky.steps).toHaveLength(2);
|
||||
const [download, upload] = flaky.steps;
|
||||
expect(flaky.needs).toBeUndefined();
|
||||
expect(flaky.steps).toHaveLength(4);
|
||||
const [downloadShards, downloadQuick, uploadShards, uploadQuick] = flaky.steps;
|
||||
for (const step of flaky.steps) {
|
||||
expect(step.run).toBeUndefined();
|
||||
expect(step.if).toBe("env.TRUNK_ORG_API_TOKEN != ''");
|
||||
expect(step.uses.startsWith('actions/checkout@')).toBe(false);
|
||||
expect(step['continue-on-error']).toBe(true);
|
||||
}
|
||||
expect(download.uses).toMatch(/^actions\/download-artifact@[0-9a-f]{40}$/);
|
||||
expect(download.with).toEqual({ pattern: 'free-junit-*', path: '${{ runner.temp }}/free-junit' });
|
||||
expect(upload.uses).toBe('trunk-io/analytics-uploader@9886de74a788c450c793109577a4d97bfb3e799f');
|
||||
expect(upload['timeout-minutes']).toBe(5);
|
||||
expect(upload.with).toEqual({
|
||||
'junit-paths': '${{ runner.temp }}/free-junit/**/*.xml',
|
||||
const fromRun = { 'run-id': '${{ github.event.workflow_run.id }}', 'github-token': '${{ github.token }}' };
|
||||
for (const download of [downloadShards, downloadQuick]) {
|
||||
expect(download.if).toBe("env.TRUNK_ORG_API_TOKEN != ''");
|
||||
expect(download.uses).toMatch(/^actions\/download-artifact@[0-9a-f]{40}$/);
|
||||
}
|
||||
expect(downloadShards.with).toEqual({ pattern: 'free-junit-*', path: '${{ github.workspace }}/free-junit', ...fromRun });
|
||||
expect(downloadQuick.with).toEqual({ name: 'quick-junit', path: '${{ github.workspace }}/quick-junit', ...fromRun });
|
||||
const uploaded = (dir: string) => ({
|
||||
'junit-paths': `\${{ github.workspace }}/${dir}/**/*.xml`,
|
||||
'org-slug': 'garry-tan',
|
||||
token: '${{ env.TRUNK_ORG_API_TOKEN }}',
|
||||
'test-collection-id': '7yDVXXE3',
|
||||
@@ -120,13 +137,16 @@ describe('free-tests workflow wiring', () => {
|
||||
'use-uncloned-repo': 'true',
|
||||
'github-token': '',
|
||||
'gh-repo-url': '${{ github.server_url }}/${{ github.repository }}',
|
||||
'gh-repo-head-sha': '${{ github.event.pull_request.head.sha || github.sha }}',
|
||||
'gh-repo-head-branch': '${{ github.event.pull_request.head.ref || github.ref_name }}',
|
||||
'gh-repo-head-sha': '${{ github.event.workflow_run.head_sha }}',
|
||||
'gh-repo-head-branch': '${{ github.event.workflow_run.head_branch }}',
|
||||
});
|
||||
for (const name of Object.keys(jobs).filter(n => n !== 'trunk-flaky-tests')) {
|
||||
expect([name, [jobs[name].needs ?? []].flat().includes('trunk-flaky-tests')]).toEqual([name, false]);
|
||||
expect([name, /TRUNK_ORG_API_TOKEN|analytics-uploader|free-junit-\*/.test(JSON.stringify(jobs[name]))]).toEqual([name, false]);
|
||||
for (const [upload, dir] of [[uploadShards, 'free-junit'], [uploadQuick, 'quick-junit']] as const) {
|
||||
expect(upload.if).toBe(`env.TRUNK_ORG_API_TOKEN != '' && hashFiles('${dir}/**/*.xml') != ''`);
|
||||
expect(upload.uses).toBe('trunk-io/analytics-uploader@9886de74a788c450c793109577a4d97bfb3e799f');
|
||||
expect(upload['timeout-minutes']).toBe(5);
|
||||
}
|
||||
expect(uploadShards.with).toEqual(uploaded('free-junit'));
|
||||
expect(uploadQuick.with).toEqual({ ...uploaded('quick-junit'), variant: 'bun-default' });
|
||||
});
|
||||
|
||||
test('pull_request trigger, never pull_request_target', () => {
|
||||
@@ -247,7 +267,7 @@ describe('free-tests workflow wiring', () => {
|
||||
}
|
||||
expect(steps[install]['continue-on-error'], name).toBe(true);
|
||||
const offline = steps.find((step: any) => step.id === 'apt_offline');
|
||||
const failOpen = name === 'free-suite' ? [offline, steps[install], steps.find((step: any) => step.name === 'Upload shard JUnit reports')] : [offline, steps[install]];
|
||||
const failOpen = [offline, steps[install], steps.find((step: any) => step.name === (name === 'free-suite' ? 'Upload shard JUnit reports' : "Upload the quick tier's JUnit reports"))];
|
||||
expect(steps.filter((step: any) => step['continue-on-error']), name).toEqual(failOpen);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -480,7 +480,8 @@ describe('per-file test and expect() counts from bun JUnit', () => {
|
||||
const base = ['--ci-run', 'plan.json', '--shard', '1', '--result', 'r.json'];
|
||||
expect(parseCliOptions([...base, '--junit-dir', 'junit']).junitDir).toBe('junit');
|
||||
expect('junitDir' in parseCliOptions(base)).toBe(false);
|
||||
expect(() => parseCliOptions(['--junit-dir', 'junit'])).toThrow('--junit-dir applies only to --ci-run');
|
||||
expect(() => parseCliOptions(['--junit-dir', 'junit'])).toThrow('--junit-dir applies only to --ci-run or --quick');
|
||||
expect(parseCliOptions(['--quick', '--junit-dir', 'junit']).junitDir).toBe('junit');
|
||||
expect(() => parseCliOptions([...base, '--junit-dir'])).toThrow('Missing path for --junit-dir');
|
||||
} finally { errors.mockRestore(); fs.rmSync(root, { recursive: true, force: true }); }
|
||||
}, 60_000);
|
||||
|
||||
@@ -52,7 +52,7 @@ describe('Trunk Dynamic CI observers', () => {
|
||||
for (const [name, job] of Object.entries(wf.jobs as Record<string, any>).filter(([n]) => n !== 'trunk-dynamic-ci')) {
|
||||
expect([name, [job.needs ?? []].flat().includes('trunk-dynamic-ci')]).toEqual([name, false]);
|
||||
const text = JSON.stringify(job);
|
||||
const mentions = name === 'trunk-flaky-tests' && file === 'free-tests.yml' ? /dynamic-ci|TRUNK_API_TOKEN/i.test(text) : /trunk|dynamic-ci|TRUNK_API_TOKEN/i.test(text);
|
||||
const mentions = /trunk|dynamic-ci|TRUNK_API_TOKEN/i.test(text);
|
||||
expect([name, mentions]).toEqual([name, false]);
|
||||
}
|
||||
const trunkRefs = (JSON.stringify(wf.jobs).match(SECRET_REF) ?? []).filter(ref => ref === 'secrets.TRUNK_API_TOKEN');
|
||||
|
||||
Reference in new issue
Block a user