Skip to content

Commit 6ae212e

Browse files
authored
fix(source-control): compare check run timestamps by instant (pingdotgg#20094)
Co-authored-by: Simone <[email protected]>
1 parent b18a9bf commit 6ae212e

3 files changed

Lines changed: 117 additions & 3 deletions

File tree

‎packages/source-control-core/src/server/pullRequestChecks.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ function entry(
2020
}
2121

2222
describe("dedupeChecks", () => {
23+
it.each([
24+
["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.500Z"],
25+
["2026-10-16T12:00:00+02:00", "2026-10-16T10:30:00Z"],
26+
["2026-10-16T12:00:00.0001Z", "2026-10-16T12:00:00.0002Z"],
27+
["2026-10-16T12:00:00.0001z", "2026-10-16T12:00:00.0002z"],
28+
["2026-10-16T12:00:00.00009Z", "2026-10-16T12:00:00.0001Z"],
29+
["2024-02-28T23:59:59Z", "2024-02-29T00:00:00.0001Z"],
30+
["2000-02-28T23:59:59Z", "2000-02-29T00:00:00Z"],
31+
["2024-03-01T01:00:00Z", "2024-02-29T23:30:00-02:00"],
32+
["2026-10-16t12:00:00z", "2026-10-16t12:00:00.0001z"],
33+
["+010000-01-01T00:00:00Z", "+010000-01-01T00:00:00.0001Z"],
34+
])("keeps the newer instant between %s and %s in either listing order", (older, newer) => {
35+
const oldRun = entry("build", "success", { at: older });
36+
const newRun = entry("build", "failure", { at: newer });
37+
38+
expect(
39+
[
40+
[oldRun, newRun],
41+
[newRun, oldRun],
42+
].map((runs) => dedupeChecks(runs)[0]?.status),
43+
).toEqual(["failure", "failure"]);
44+
});
45+
46+
it.each([
47+
["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.000Z"],
48+
["2026-10-16T12:00:00Z", "2026-10-16T14:00:00+02:00"],
49+
["2026-10-16T24:00:00.000Z", "2026-10-17T00:00:00Z"],
50+
["2026-10-16T12:00Z", "2026-10-16T12:00:00Z"],
51+
])("takes the last copy for equal instants %s and %s", (first, second) => {
52+
const forward = dedupeChecks([
53+
entry("build", "success", { at: first }),
54+
entry("build", "failure", { at: second }),
55+
]);
56+
const reversed = dedupeChecks([
57+
entry("build", "success", { at: second }),
58+
entry("build", "failure", { at: first }),
59+
]);
60+
61+
expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
62+
});
63+
2364
it("keeps the newest run of a check the host listed twice", () => {
2465
const checks = dedupeChecks([
2566
entry("Prepare PR size config", "success", {
@@ -37,6 +78,48 @@ describe("dedupeChecks", () => {
3778
]);
3879
});
3980

81+
it("preserves text ordering when a host supplies an unparseable timestamp", () => {
82+
expect(
83+
dedupeChecks([
84+
entry("build", "success", { at: "invalid-a" }),
85+
entry("build", "failure", { at: "invalid-b" }),
86+
]).map((check) => check.status),
87+
).toEqual(["failure"]);
88+
});
89+
90+
it.each([
91+
["2026-10-16", "2026-10-15T23:00:00-02:00", "success"],
92+
["2026-10-16T00:00:00", "2026-10-15T23:00:00-02:00", "success"],
93+
["2026-10-16T24:00:00.0001Z", "2026-10-17T00:00:00Z", "failure"],
94+
])("uses text fallback for incomplete or invalid timestamp %s", (malformed, valid, expected) => {
95+
const malformedRun = entry("build", "success", { at: malformed });
96+
const validRun = entry("build", "failure", { at: valid });
97+
98+
expect(
99+
[
100+
[malformedRun, validRun],
101+
[validRun, malformedRun],
102+
].map((runs) => dedupeChecks(runs)[0]?.status),
103+
).toEqual([expected, expected]);
104+
});
105+
106+
it.each([
107+
["2026-02-30T00:00:00Z", "2026-03-01T00:00:00Z"],
108+
["2026-02-29T00:00:00Z", "2026-03-01T00:00:00Z"],
109+
["1900-02-29T00:00:00Z", "1900-03-01T00:00:00Z"],
110+
["2026-04-31T00:00:00.0001-02:00", "2026-05-01T00:00:00Z"],
111+
])("uses text fallback for impossible date %s", (malformed, valid) => {
112+
const malformedRun = entry("build", "success", { at: malformed });
113+
const validRun = entry("build", "failure", { at: valid });
114+
115+
expect(
116+
[
117+
[malformedRun, validRun],
118+
[validRun, malformedRun],
119+
].map((runs) => dedupeChecks(runs)[0]?.status),
120+
).toEqual(["failure", "failure"]);
121+
});
122+
40123
it("holds a check at the place it first appeared, so a re-run does not reshuffle the list", () => {
41124
const checks = dedupeChecks([
42125
entry("lint", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),

‎packages/source-control-core/src/server/pullRequestChecks.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import type { PullRequestCheck } from "@t3tools/contracts";
2+
import * as DateTime from "effect/DateTime";
3+
import * as Option from "effect/Option";
4+
5+
const 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.
9+
function 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+
}
225

326
function isAtLeastAsNew(candidate: string | null, kept: string | null): boolean {
427
if (candidate === null) return kept === null;
5-
return kept === null || candidate >= kept;
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+
);
637
}
738

839
/**

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ it.effect("submits a Forgejo review without sending its summary in the prelimina
122122
);
123123
});
124124

125-
it.effect("reads Forgejo checks without repository or viewer requests", () => {
125+
it.effect("reads the newest Forgejo checks without repository or viewer requests", () => {
126126
const paths: string[] = [];
127127
return Effect.gen(function* () {
128128
const provider = yield* ForgejoPullRequestProvider.make;
@@ -154,7 +154,7 @@ it.effect("reads Forgejo checks without repository or viewer requests", () => {
154154
"head":{"ref":"feature","sha":"head","repo":null},"base":{"ref":"main","sha":"base","repo":null},
155155
"created_at":"2026-09-16T00:00:00Z","updated_at":"2026-09-16T00:00:00Z","closed_at":null,"merged_at":null,"labels":[]}`
156156
: input.path.endsWith("page=1")
157-
? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
157+
? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00.500Z"},{"context":"build","status":"success","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
158158
: "[]",
159159
),
160160
);

0 commit comments

Comments
 (0)