Skip to content
refs
History
19 lines · 1.3 KBRaw
1import type { CiRunState, CiRunSummary, CommitCheck } from "@refs/backend/contract";
2
3const ENDED: ReadonlySet<CiRunState> = new Set(["succeeded", "failed", "cancelled", "timed_out"]);
4
5/**
6 * Sums up the runs of one commit, newest first. A newer run replaces an older one of the same kind: the branch, or
7 * one pull request. So a run started again by hand clears an earlier failure. Cancelled runs do not count.
8 */
9export function commitCheck(repo: string, runs: readonly CiRunSummary[]): CommitCheck | null {
10 const newest = new Map<number | null, CiRunSummary>();
11 for (const run of runs) if (run.state !== "cancelled" && !newest.has(run.pull)) newest.set(run.pull, run);
12 const counted = [...newest.values()];
13 if (!counted.length) return null;
14 const failed = counted.find((run) => run.state === "failed" || run.state === "timed_out");
15 if (failed) return { state: "failed", label: "A CI run failed", href: `/${repo}/runs/${failed.id}` };
16 const running = counted.find((run) => !ENDED.has(run.state));
17 if (running) return { state: "running", label: running.state === "waiting_approval" ? "A CI run waits for approval" : "CI is running", href: `/${repo}/runs/${running.id}` };
18 return { state: "succeeded", label: "CI passed", href: `/${repo}/runs/${counted[0].id}` };
19}