diff --git a/scripts/ci/test-history.mjs b/scripts/ci/test-history.mjs index e95a69bcc4..8bd615487e 100644 --- a/scripts/ci/test-history.mjs +++ b/scripts/ci/test-history.mjs @@ -11,6 +11,7 @@ const TIER_PROFILES = { }; const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; const MAX_CANDIDATES = 5; +const COMMIT_SHA = /^[0-9a-f]{40}$/u; const EMPTY_SELECTION = { runId: "", artifactName: "" }; const positiveId = (value) => (typeof value === "string" || typeof value === "number") && @@ -76,6 +77,8 @@ function findTimingRun({ positiveId(lineage?.head_repository_id) && lineage.repository_id === lineage.head_repository_id && lineage.repository_id === Number(repositoryId) && + typeof lineage.head_sha === "string" && + COMMIT_SHA.test(lineage.head_sha) && lineage.head_branch === branch ); }) @@ -92,6 +95,7 @@ function findTimingRun({ age >= 0 && age < MAX_AGE_MS && run.id === artifact.workflow_run.id && + run.head_sha === artifact.workflow_run.head_sha && run.status === "completed" && run.conclusion === "success" && run.workflowPath?.file === ".github/workflows/ci.yml" && diff --git a/tests/boundary/filesystem/ciTestHistory.test.ts b/tests/boundary/filesystem/ciTestHistory.test.ts index 78fa6f4dfc..51ed527dc3 100644 --- a/tests/boundary/filesystem/ciTestHistory.test.ts +++ b/tests/boundary/filesystem/ciTestHistory.test.ts @@ -13,6 +13,7 @@ const script = fileURLToPath( const repository = "example/rea"; const repositoryId = 1; const now = Date.now(); +const headSha = "a".repeat(40); function artifact(id: number, overrides: Record = {}) { return { @@ -25,6 +26,7 @@ function artifact(id: number, overrides: Record = {}) { repository_id: repositoryId, head_repository_id: repositoryId, head_branch: "main", + head_sha: headSha, }, ...overrides, }; @@ -40,6 +42,7 @@ function run(id: number, overrides: Record = {}) { created_at: new Date(now - 120_000).toISOString(), head_branch: "main", head_repository: { full_name: repository }, + head_sha: headSha, ...overrides, }; } @@ -385,3 +388,36 @@ it("finds an older successful timing artifact beyond five unsuccessful runs", as expect(calls).toContain("branch=main"); expect(calls).toContain("status=success"); }); + +it.each([ + ["the commit SHAs differ", headSha, "b".repeat(40)], + ["both commit SHAs are omitted", undefined, undefined], + ["both commit SHAs are malformed", "invalid-sha", "invalid-sha"], +])("skips newer timing history when %s", async (_, artifactSha, runSha) => { + const unverifiedArtifact = artifact(51, { + created_at: new Date(now - 30_000).toISOString(), + workflow_run: { ...artifact(51).workflow_run, head_sha: artifactSha }, + }); + const unverifiedRun = run(51, { head_sha: runSha }); + const [fallback, empty] = await Promise.all([ + invoke( + [unverifiedArtifact, artifact(50)], + { 51: unverifiedRun, 50: run(50) }, + { + tier: "full", + }, + ), + invoke([unverifiedArtifact], { 51: unverifiedRun }, { tier: "full" }), + ]); + + expect(JSON.parse(fallback.result.stdout)).toEqual({ + run_id: "50", + artifact_name: "test-timings-full", + }); + expect(fallback.output).toBe("run-id=50\nartifact-name=test-timings-full\n"); + expect(JSON.parse(empty.result.stdout)).toEqual({ + run_id: "", + artifact_name: "", + }); + expect(empty.output).toBe("run-id=\nartifact-name=\n"); +});