import type { CiRunState, CiRunSummary, CommitCheck } from "@refs/backend/contract"; const ENDED: ReadonlySet = new Set(["succeeded", "failed", "cancelled", "timed_out"]); /** * Sums up the runs of one commit, newest first. A newer run replaces an older one of the same kind: the branch, or * one pull request. So a run started again by hand clears an earlier failure. Cancelled runs do not count. */ export function commitCheck(repo: string, runs: readonly CiRunSummary[]): CommitCheck | null { const newest = new Map(); for (const run of runs) if (run.state !== "cancelled" && !newest.has(run.pull)) newest.set(run.pull, run); const counted = [...newest.values()]; if (!counted.length) return null; const failed = counted.find((run) => run.state === "failed" || run.state === "timed_out"); if (failed) return { state: "failed", label: "A CI run failed", href: `/${repo}/runs/${failed.id}` }; const running = counted.find((run) => !ENDED.has(run.state)); if (running) return { state: "running", label: running.state === "waiting_approval" ? "A CI run waits for approval" : "CI is running", href: `/${repo}/runs/${running.id}` }; return { state: "succeeded", label: "CI passed", href: `/${repo}/runs/${counted[0].id}` }; }