Skip to content

Commit e5c3920

Browse files
committed
fix(source-control): compare check run timestamps by instant
1 parent 4e85a93 commit e5c3920

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ 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+
])("keeps the newer instant between %s and %s in either listing order", (older, newer) => {
30+
const oldRun = entry("build", "success", { at: older });
31+
const newRun = entry("build", "failure", { at: newer });
32+
33+
expect(
34+
[
35+
[oldRun, newRun],
36+
[newRun, oldRun],
37+
].map((runs) => dedupeChecks(runs)[0]?.status),
38+
).toEqual(["failure", "failure"]);
39+
});
40+
41+
it.each([
42+
["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.000Z"],
43+
["2026-10-16T12:00:00Z", "2026-10-16T14:00:00+02:00"],
44+
])("takes the last copy for equal instants %s and %s", (first, second) => {
45+
const forward = dedupeChecks([
46+
entry("build", "success", { at: first }),
47+
entry("build", "failure", { at: second }),
48+
]);
49+
const reversed = dedupeChecks([
50+
entry("build", "success", { at: second }),
51+
entry("build", "failure", { at: first }),
52+
]);
53+
54+
expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
55+
});
56+
2357
it("keeps the newest run of a check the host listed twice", () => {
2458
const checks = dedupeChecks([
2559
entry("Prepare PR size config", "success", {
@@ -37,6 +71,15 @@ describe("dedupeChecks", () => {
3771
]);
3872
});
3973

74+
it("preserves text ordering when a host supplies an unparseable timestamp", () => {
75+
expect(
76+
dedupeChecks([
77+
entry("build", "success", { at: "invalid-a" }),
78+
entry("build", "failure", { at: "invalid-b" }),
79+
]).map((check) => check.status),
80+
).toEqual(["failure"]);
81+
});
82+
4083
it("holds a check at the place it first appeared, so a re-run does not reshuffle the list", () => {
4184
const checks = dedupeChecks([
4285
entry("lint", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import type { PullRequestCheck } from "@t3tools/contracts";
22

3-
/** ISO-8601 timestamps in UTC compare correctly as plain text, which is all the ordering needs. */
3+
// Parse whole seconds separately so sub-millisecond fractions keep their precision.
4+
function timestampParts(value: string) {
5+
const fraction = /\.(\d+)(?=Z$|[+-]\d{2}:\d{2}$)/i.exec(value);
6+
return {
7+
time: Date.parse(fraction ? value.replace(fraction[0], "") : value),
8+
fraction: fraction?.[1] ?? "",
9+
};
10+
}
11+
412
function isAtLeastAsNew(candidate: string | null, kept: string | null): boolean {
513
if (candidate === null) return kept === null;
6-
return kept === null || candidate >= kept;
14+
if (kept === null) return true;
15+
const candidateParts = timestampParts(candidate);
16+
const keptParts = timestampParts(kept);
17+
if (Number.isNaN(candidateParts.time) || Number.isNaN(keptParts.time)) return candidate >= kept;
18+
if (candidateParts.time !== keptParts.time) return candidateParts.time > keptParts.time;
19+
const precision = Math.max(candidateParts.fraction.length, keptParts.fraction.length);
20+
return (
21+
candidateParts.fraction.padEnd(precision, "0") >= keptParts.fraction.padEnd(precision, "0")
22+
);
723
}
824

925
/**

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

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

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

0 commit comments

Comments
 (0)