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

pullRequestChecks.ts

70 lines3.2 KB

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 };
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 );
38​
39/**
40 * A host's rollup is a list of runs, not a list of checks: while a workflow is being re-run — or
41 * while a second run of it is already live — the same check arrives twice, and both copies reach
42 * the reader as what looks like a duplicate. Nothing in a check carries an id, so the name is what
43 * identifies it, qualified by the workflow it belongs to since two workflows are free to name a
44 * job the same thing.
45 */
46export function dedupeChecks(
47 entries: ReadonlyArray<{
48 readonly check: PullRequestCheck;
49 readonly workflowName: string | null;
50 readonly at: string | null;
51 }>,
52): ReadonlyArray<PullRequestCheck> {
53 const newestByCheck = new Map<string, (typeof entries)[number]>();
54 for (const entry of entries) {
55 const key = JSON.stringify([entry.workflowName ?? "", entry.check.name]);
56 const kept = newestByCheck.get(key);
57 if (kept === undefined || isAtLeastAsNew(entry.at, kept.at)) newestByCheck.set(key, entry);
58 }
59 const survivors = [...newestByCheck.values()];
60 const countsByName = new Map<string, number>();
61 for (const entry of survivors) {
62 countsByName.set(entry.check.name, (countsByName.get(entry.check.name) ?? 0) + 1);
63 }
64 return survivors.map((entry) => {
65 const workflowName = entry.workflowName ?? "";
66 return workflowName.length > 0 && (countsByName.get(entry.check.name) ?? 0) > 1
67 ? { ...entry.check, name: `${workflowName} / ${entry.check.name}` }
68 : entry.check;
69 });