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 * Rollups repeat checks during reruns or overlapping runs, and checks carry no ids.
51 * Identify checks by workflow plus name because different workflows may share job names.
52 */
53export function dedupeChecks(
54 entries: ReadonlyArray<{
55 readonly check: PullRequestCheck;
56 readonly workflowName: string | null;
57 readonly at: string | null;
58 // Hosts with one-second timestamps can date a final and an earlier status alike.
59 readonly sequence?: number;
60 }>,
61): ReadonlyArray<PullRequestCheck> {
62 const newestByCheck = new Map<string, (typeof entries)[number]>();
63 for (const entry of entries) {
64 const key = JSON.stringify([entry.workflowName ?? "", entry.check.name]);
65 const kept = newestByCheck.get(key);
66 if (kept !== undefined) {
67 if (!isAtLeastAsNew(entry.at, kept.at)) continue;
68 if (
69 entry.sequence !== undefined &&
70 kept.sequence !== undefined &&
71 entry.sequence < kept.sequence &&
72 isAtLeastAsNew(kept.at, entry.at)
73 )
74 continue;
75 }
76 newestByCheck.set(key, entry);
77 }
78 const survivors = [...newestByCheck.values()];
79 const countsByName = new Map<string, number>();
80 for (const entry of survivors) {
81 countsByName.set(entry.check.name, (countsByName.get(entry.check.name) ?? 0) + 1);
82 }
83 return sortChecks(
84 survivors.map((entry) => {
85 const workflowName = entry.workflowName ?? "";
86 return workflowName.length > 0 && (countsByName.get(entry.check.name) ?? 0) > 1
87 ? { ...entry.check, name: `${workflowName} / ${entry.check.name}` }
88 : entry.check;
89 }),
90 );
91}