Open History to see the last commit
1import type { PullRequestCheck } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import * as Option from "effect/Option";
4
5const zonedDateTimePattern =
6 /^(?:\d{4}|[+-]\d{6})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?|24:00(?::00(?:\.0+)?)?)(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/i;
7
8// Parse whole seconds separately so sub-millisecond fractions keep their precision.
9function timestampParts(value: string) {
10 if (!zonedDateTimePattern.test(value)) return { time: Number.NaN, fraction: "" };
11 const fraction = /\.(\d+)(?=Z$|[+-]\d{2}:\d{2}$)/i.exec(value);
12 // Date can normalize impossible days. Check the calendar date before applying the offset.
13 const datePart = value.split(/[Tt]/, 1)[0] ?? "";
14 const date = DateTime.make(`${datePart}T00:00:00Z`);
15 const parts = Option.isSome(date) ? DateTime.toPartsUtc(date.value) : null;
16 const validDate =
17 parts !== null &&
18 parts.month === Number(datePart.slice(-5, -3)) &&
19 parts.day === Number(datePart.slice(-2));
20 return {
21 time: validDate ? Date.parse(fraction ? value.replace(fraction[0], "") : value) : Number.NaN,
22 fraction: fraction?.[1] ?? "",
23 };
24}
25
26function isAtLeastAsNew(candidate: string | null, kept: string | null): boolean {
27 if (candidate === null) return kept === null;
28 if (kept === null) return true;
29 const candidateParts = timestampParts(candidate);
30 const keptParts = timestampParts(kept);
31 if (Number.isNaN(candidateParts.time) || Number.isNaN(keptParts.time)) return candidate >= kept;
32 if (candidateParts.time !== keptParts.time) return candidateParts.time > keptParts.time;
33 const precision = Math.max(candidateParts.fraction.length, keptParts.fraction.length);
34 return (
35 candidateParts.fraction.padEnd(precision, "0") >= keptParts.fraction.padEnd(precision, "0")
36 );
37}
38
39export function sortChecks(
40 checks: ReadonlyArray<PullRequestCheck>,
41): ReadonlyArray<PullRequestCheck> {
42 return checks.toSorted(
43 (a, b) =>
44 Number(b.status === "failure" || b.status === "cancelled") -
45 Number(a.status === "failure" || a.status === "cancelled"),
46 );
47}
48
49/**
50 * A host's rollup is a list of runs, not a list of checks: while a workflow is being re-run — or
51 * while a second run of it is already live — the same check arrives twice, and both copies reach
52 * the reader as what looks like a duplicate. Nothing in a check carries an id, so the name is what
53 * identifies it, qualified by the workflow it belongs to since two workflows are free to name a
54 * job the same thing.
55 */
56export function dedupeChecks(
57 entries: ReadonlyArray<{
58 readonly check: PullRequestCheck;
59 readonly workflowName: string | null;
60 readonly at: string | null;
61 }>,
62): ReadonlyArray<PullRequestCheck> {
63 const newestByCheck = new Map<string, (typeof entries)[number]>();
64 for (const entry of entries) {
65 const key = JSON.stringify([entry.workflowName ?? "", entry.check.name]);
66 const kept = newestByCheck.get(key);
67 if (kept === undefined || isAtLeastAsNew(entry.at, kept.at)) newestByCheck.set(key, entry);
68 }
69 const survivors = [...newestByCheck.values()];
70 const countsByName = new Map<string, number>();
71 for (const entry of survivors) {
72 countsByName.set(entry.check.name, (countsByName.get(entry.check.name) ?? 0) + 1);
73 }
74 return sortChecks(
75 survivors.map((entry) => {
76 const workflowName = entry.workflowName ?? "";
77 return workflowName.length > 0 && (countsByName.get(entry.check.name) ?? 0) > 1
78 ? { ...entry.check, name: `${workflowName} / ${entry.check.name}` }
79 : entry.check;
80 }),
81 );
82}