fix(ci): bind timing history to the verified workflow source (#12555)

* fix(ci): bind timing history to the verified workflow source

* test(ci): reject timing history without valid commit lineage

---------

Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-19 22:56:52 +00:00
1 parent 3e40979b42
commit a035813e55
2 files changed
+40

No files matched your search

+4
View File
@@ -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" &&
@@ -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<string, unknown> = {}) {
return {
@@ -25,6 +26,7 @@ function artifact(id: number, overrides: Record<string, unknown> = {}) {
repository_id: repositoryId,
head_repository_id: repositoryId,
head_branch: "main",
head_sha: headSha,
},
...overrides,
};
@@ -40,6 +42,7 @@ function run(id: number, overrides: Record<string, unknown> = {}) {
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");
});