Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import { dedupeChecks } from "./pullRequestChecks.ts";
function entry(
name: string,
status: PullRequestCheckStatus,
extra: { readonly workflowName?: string | null; readonly at?: string | null } = {},
extra: {
readonly workflowName?: string | null;
readonly at?: string | null;
readonly sequence?: number;
} = {},
): {
readonly check: PullRequestCheck;
readonly workflowName: string | null;
readonly at: string | null;
readonly sequence?: number;
} {
return {
check: { name, status, description: null, url: null },
workflowName: extra.workflowName ?? null,
at: extra.at ?? null,
...(extra.sequence === undefined ? {} : { sequence: extra.sequence }),
};
}

Expand Down Expand Up @@ -53,6 +59,39 @@ describe("dedupeChecks", () => {
expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
});

it("takes the higher sequence when instants tie, whichever is listed first", () => {
const final = entry("build", "success", { at: "2026-10-12T10:14:29Z", sequence: 15 });
const pending = entry("build", "pending", { at: "2026-10-12T10:14:29Z", sequence: 14 });

expect(
[
[final, pending],
[pending, final],
].map((runs) => dedupeChecks(runs)[0]?.status),
).toEqual(["success", "success"]);
});

it("lets a newer instant win over a higher sequence", () => {
const stale = entry("build", "success", { at: "2026-10-12T10:14:29Z", sequence: 15 });
const fresh = entry("build", "failure", { at: "2026-10-12T10:14:30Z", sequence: 14 });

expect(
[
[stale, fresh],
[fresh, stale],
].map((runs) => dedupeChecks(runs)[0]?.status),
).toEqual(["failure", "failure"]);
});

it("takes the last copy for equal instants when only one run has a sequence", () => {
const checks = dedupeChecks([
entry("build", "success", { at: "2026-10-12T10:14:29Z", sequence: 15 }),
entry("build", "failure", { at: "2026-10-12T10:14:29Z" }),
]);

expect(checks.map((check) => check.status)).toEqual(["failure"]);
});

it.each([
["2026-10-16", "2026-10-15T23:00:00-02:00", "success"],
["2026-10-16T00:00:00", "2026-10-15T23:00:00-02:00", "success"],
Expand Down
14 changes: 13 additions & 1 deletion packages/source-control-core/src/server/pullRequestChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,25 @@ export function dedupeChecks(
readonly check: PullRequestCheck;
readonly workflowName: string | null;
readonly at: string | null;
// Hosts with one-second timestamps can date a final and an earlier status alike.
readonly sequence?: number;
}>,
): ReadonlyArray<PullRequestCheck> {
const newestByCheck = new Map<string, (typeof entries)[number]>();
for (const entry of entries) {
const key = JSON.stringify([entry.workflowName ?? "", entry.check.name]);
const kept = newestByCheck.get(key);
if (kept === undefined || isAtLeastAsNew(entry.at, kept.at)) newestByCheck.set(key, entry);
if (kept !== undefined) {
if (!isAtLeastAsNew(entry.at, kept.at)) continue;
if (
entry.sequence !== undefined &&
kept.sequence !== undefined &&
entry.sequence < kept.sequence &&
isAtLeastAsNew(kept.at, entry.at)
)
continue;
}
newestByCheck.set(key, entry);
}
const survivors = [...newestByCheck.values()];
const countsByName = new Map<string, number>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,37 @@ it("treats a required pattern's `*` as a wildcard even when the check name has o
);
});

const tiedStatuses = [
{ id: 15, status: "success" },
{ id: 14, status: "pending" },
].map(({ id, status }) => ({
id,
context: "ci/required-build",
status,
description: null,
target_url: null,
updated_at: "2026-10-12T10:14:29Z",
}));

it.effect.each([
["newest first", tiedStatuses],
["oldest first", tiedStatuses.toReversed()],
] as const)("reads the status created last when two share a second, listed %s", ([, statuses]) =>
Effect.gen(function* () {
const provider = yield* ForgejoPullRequestProvider.make;
const readChecks = provider.getChangeRequestChecks;
if (readChecks === undefined) return yield* Effect.die("checks read missing");
const summary = yield* readChecks(requiredChecksInput);
const detail = yield* provider.getChangeRequest(requiredChecksInput);
for (const checks of [summary.checks, detail.checks]) {
assert.deepStrictEqual(
checks.map((check) => [check.name, check.status]),
[["ci/required-build", "success"]],
);
}
}).pipe(Effect.provide(requiredChecksLayer(Effect.succeed({}), statuses))),
);

const forgejoMergeProvider = (repository: Effect.Effect<string, ForgejoCli.ForgejoCliError>) => {
const writes: Array<ForgejoCli.ForgejoApiInput> = [];
const provider = ForgejoPullRequestProvider.make.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const ForgejoCommit = Schema.Struct({
),
});
export const ForgejoStatus = Schema.Struct({
id: Schema.optional(Schema.Int),
context: Schema.String,
status: Schema.String,
description: Schema.NullOr(Schema.String),
Expand Down Expand Up @@ -236,6 +237,7 @@ export function forgejoChecks(
statuses.map((status) => ({
workflowName: null,
at: status.updated_at,
...(status.id === undefined ? {} : { sequence: status.id }),
check: {
name: status.context || "check",
description: status.description || null,
Expand Down
Loading