From bc19c8d21fbe26be280db55faae92bac2c12c20e Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Mon, 23 Nov 2026 22:20:55 +0800 Subject: [PATCH] fix(ci): bind timing evidence to its workflow attempt (#14080) Co-authored-by: ShuaixinHuang --- scripts/ci/job-timings.mjs | 11 ++-- .../boundary/filesystem/ciJobTimings.test.ts | 54 +++++++++++++++++-- .../filesystem/ciJobTimingsBatch.test.ts | 10 ++-- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/scripts/ci/job-timings.mjs b/scripts/ci/job-timings.mjs index c38f69c6b5..15cc33c9b5 100644 --- a/scripts/ci/job-timings.mjs +++ b/scripts/ci/job-timings.mjs @@ -46,19 +46,20 @@ export async function collectJobTimings({ }) { const identity = validateIdentity({ repository, runId, runAttempt }); const reportPath = join(outputDirectory, "report.json"); + const rawPath = join(outputDirectory, "jobs.json"); await rm(reportPath, { force: true }); + await rm(rawPath, { force: true }); const endpoint = `repos/${identity.repository}/actions/runs/${identity.runId}/attempts/${identity.runAttempt}/jobs?per_page=100`; const { stdout } = await execFileAsync( "gh", ["api", "--paginate", "--slurp", endpoint], { env, encoding: "utf8", maxBuffer: MAX_BUFFER, timeout: timeoutMs }, ); - const rawPath = join(outputDirectory, "jobs.json"); await mkdir(outputDirectory, { recursive: true }); await writeFile(rawPath, stdout, "utf8"); const pages = parsePages(stdout); const jobs = pages.flatMap(({ jobs: pageJobs }, index) => - validateJobs(pageJobs, index), + validateJobs(pageJobs, index, identity), ); if ( pages.some( @@ -125,7 +126,7 @@ function parsePages(stdout) { return pages; } -function validateJobs(jobs, pageIndex) { +function validateJobs(jobs, pageIndex, { runId, runAttempt }) { return jobs.map((job, jobIndex) => { if ( typeof job !== "object" || @@ -138,6 +139,10 @@ function validateJobs(jobs, pageIndex) { throw new Error( `GitHub jobs API page ${String(pageIndex + 1)} job ${String(jobIndex + 1)} is malformed`, ); + if (job.run_id !== runId || job.run_attempt !== runAttempt) + throw new Error( + `GitHub jobs API page ${String(pageIndex + 1)} job ${String(jobIndex + 1)} belongs to a different workflow run or attempt`, + ); return job; }); } diff --git a/tests/boundary/filesystem/ciJobTimings.test.ts b/tests/boundary/filesystem/ciJobTimings.test.ts index e2e3279395..ea886947e6 100644 --- a/tests/boundary/filesystem/ciJobTimings.test.ts +++ b/tests/boundary/filesystem/ciJobTimings.test.ts @@ -16,12 +16,15 @@ const reporter = fileURLToPath( new URL("../../../scripts/ci/job-timings.mjs", import.meta.url), ); +const attempt = { run_id: 42, run_attempt: 2 }; + it("merges paginated jobs, reports elapsed metrics, and preserves raw responses", async () => { const fixture = await createFixture([ { total_count: 4, jobs: [ { + ...attempt, name: "Build package", workflow_name: "CI", status: "completed", @@ -48,6 +51,7 @@ it("merges paginated jobs, reports elapsed metrics, and preserves raw responses" total_count: 4, jobs: [ { + ...attempt, name: "Queued lane", status: "queued", created_at: "2026-10-13T00:00:00Z", @@ -56,6 +60,7 @@ it("merges paginated jobs, reports elapsed metrics, and preserves raw responses" steps: [], }, { + ...attempt, name: "Running lane", status: "in_progress", created_at: "2026-10-13T00:00:00Z", @@ -70,6 +75,7 @@ it("merges paginated jobs, reports elapsed metrics, and preserves raw responses" ], }, { + ...attempt, name: "Skipped lane", status: "completed", conclusion: "skipped", @@ -188,6 +194,7 @@ it.each(["ci.yml", "release.yml"])( { jobs: [ { + ...attempt, name: filename, status: "completed", steps: [ @@ -226,6 +233,7 @@ it("excludes skipped artifact steps even when GitHub supplies timestamps", async { jobs: [ { + ...attempt, name: "Portable tests", status: "completed", conclusion: "failure", @@ -268,6 +276,7 @@ it("marks invalid and out-of-order timestamps unknown instead of emitting negati { jobs: [ { + ...attempt, name: "Invalid dates", status: "completed", created_at: "not-a-date", @@ -306,21 +315,60 @@ it("fails on malformed pages or a failed GitHub API request", async () => { ).resolves.toBe(malformed.raw); const failedApi = await createFixture([], { exitCode: 1 }); + await mkdir(failedApi.outputDirectory, { recursive: true }); + const oldRaw = join(failedApi.outputDirectory, "jobs.json"); + await writeFile(oldRaw, '{"prior":true}'); await expect(runReporter(failedApi)).rejects.toThrow(); + await expect(readFile(oldRaw, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); }); it.each([ + [ + "jobs from a different run", + "belongs to a different workflow run or attempt", + [ + { + jobs: [ + { run_id: 43, run_attempt: 2, name: "test", status: "completed" }, + ], + }, + ], + ], + [ + "jobs from a different attempt", + "belongs to a different workflow run or attempt", + [ + { + jobs: [ + { run_id: 42, run_attempt: 1, name: "test", status: "completed" }, + ], + }, + ], + ], [ "incomplete jobs", "total_count does not match", - [{ total_count: 2, jobs: [{ id: 1, name: "test", status: "completed" }] }], + [ + { + total_count: 2, + jobs: [{ ...attempt, id: 1, name: "test", status: "completed" }], + }, + ], ], [ "duplicate jobs", "duplicate job IDs", [ - { total_count: 2, jobs: [{ id: 1, name: "test", status: "completed" }] }, - { total_count: 2, jobs: [{ id: 1, name: "test", status: "completed" }] }, + { + total_count: 2, + jobs: [{ ...attempt, id: 1, name: "test", status: "completed" }], + }, + { + total_count: 2, + jobs: [{ ...attempt, id: 1, name: "test", status: "completed" }], + }, ], ], ])("keeps raw timing evidence and refuses %s", async (_label, error, pages) => { diff --git a/tests/boundary/filesystem/ciJobTimingsBatch.test.ts b/tests/boundary/filesystem/ciJobTimingsBatch.test.ts index c3ea01bb55..91fde9055b 100644 --- a/tests/boundary/filesystem/ciJobTimingsBatch.test.ts +++ b/tests/boundary/filesystem/ciJobTimingsBatch.test.ts @@ -45,9 +45,9 @@ it("collects the latest attempt for recent completed CI and Release runs into a }, ], }, - completedJobs("Release lane", "success", recent, 50), - completedJobs("Failed lane", "failure", recent, 55), - completedJobs("Cancelled lane", "cancelled", recent, 30), + completedJobs(43, 2, "Release lane", "success", recent, 50), + completedJobs(42, 3, "Failed lane", "failure", recent, 55), + completedJobs(41, 1, "Cancelled lane", "cancelled", recent, 30), ]); const result = await runBatch(fixture); const manifest = JSON.parse( @@ -212,6 +212,8 @@ it("retains a partial manifest when its collection budget is exhausted", async ( }); function completedJobs( + runId: number, + attempt: number, name: string, conclusion: string, startedAt: string, @@ -221,6 +223,8 @@ function completedJobs( { jobs: [ { + run_id: runId, + run_attempt: attempt, name, status: "completed", conclusion,