Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestChecks.ts

39 lines1.7 KB

1import type { PullRequestCheck } from "@t3tools/contracts";
2​
3function isAtLeastAsNew(candidate: string | null, kept: string | null): boolean {
4 if (candidate === null) return kept === null;
5 return kept === null || candidate >= kept;
6}
7​
8/**
9 * A host's rollup is a list of runs, not a list of checks: while a workflow is being re-run — or
10 * while a second run of it is already live — the same check arrives twice, and both copies reach
11 * the reader as what looks like a duplicate. Nothing in a check carries an id, so the name is what
12 * identifies it, qualified by the workflow it belongs to since two workflows are free to name a
13 * job the same thing.
14 */
15export function dedupeChecks(
16 entries: ReadonlyArray<{
17 readonly check: PullRequestCheck;
18 readonly workflowName: string | null;
19 readonly at: string | null;
20 }>,
21): ReadonlyArray<PullRequestCheck> {
22 const newestByCheck = new Map<string, (typeof entries)[number]>();
23 for (const entry of entries) {
24 const key = JSON.stringify([entry.workflowName ?? "", entry.check.name]);
25 const kept = newestByCheck.get(key);
26 if (kept === undefined || isAtLeastAsNew(entry.at, kept.at)) newestByCheck.set(key, entry);
27 }
28 const survivors = [...newestByCheck.values()];
29 const countsByName = new Map<string, number>();
30 for (const entry of survivors) {
31 countsByName.set(entry.check.name, (countsByName.get(entry.check.name) ?? 0) + 1);
32 }
33 return survivors.map((entry) => {
34 const workflowName = entry.workflowName ?? "";
35 return workflowName.length > 0 && (countsByName.get(entry.check.name) ?? 0) > 1
36 ? { ...entry.check, name: `${workflowName} / ${entry.check.name}` }
37 : entry.check;
38 });