Skip to content

Commit 8c1fe57

Browse files
fix(source-control): GitCafe checks show each check's newest run
GitCafe lists every run of a commit's checks, and its status counts them all, so a cancelled run's failure kept a pull request failing after its re-run passed. The adapter now keeps the newest run of each check (by the host's run id) and derives the checks state from those. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6e79211 commit 8c1fe57

4 files changed

Lines changed: 104 additions & 14 deletions

File tree

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,47 @@ describe("GitCafePullRequestProvider", () => {
648648
}).pipe(Effect.provide(server.layer));
649649
});
650650

651+
it.effect("reads the checks state from each check's newest run", () => {
652+
const run = (id: string, conclusion: string) => ({
653+
id,
654+
name: "Check",
655+
status: "completed",
656+
conclusion,
657+
summary: null,
658+
detailsUrl: null,
659+
});
660+
const server = fakeGitCafe({
661+
"GET /repos/owner/repo/pulls/7": pull,
662+
"GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },
663+
"GET /repos/owner/repo/pulls/7/labels": { items: [], next: null },
664+
"GET /repos/owner/repo/pulls/7/status": {
665+
merge: { conflicts: "none", fastForward: null, strategies: [] },
666+
// GitCafe counts the cancelled run's failure beside the re-run that passed.
667+
checks: { pending: 0, failing: 1, total: 2 },
668+
},
669+
[`GET /repos/owner/repo/commits/${headOid}/checks`]: {
670+
items: [run("120859457782", "success"), run("120857059247", "failure")],
671+
next: null,
672+
},
673+
"GET /repos/owner/repo/pulls/7/changes": {
674+
version: 4,
675+
headOid,
676+
comparisonBaseOid: baseOid,
677+
items: [],
678+
next: null,
679+
},
680+
});
681+
return Effect.gen(function* () {
682+
const provider = yield* GitCafePullRequestProvider.make;
683+
const detail = yield* provider.getChangeRequest(target);
684+
assert.deepStrictEqual(
685+
detail.checks.map((check) => [check.name, check.status]),
686+
[["Check", "success"]],
687+
);
688+
assert.strictEqual(detail.checksState, "passing");
689+
}).pipe(Effect.provide(server.layer));
690+
});
691+
651692
it.effect("still reads a pull request whose reviewers the token may not see", () => {
652693
const server = fakeGitCafe({
653694
"GET /repos/owner/repo/pulls/7": { ...pull, headOid: null },

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,18 @@ export const make = Effect.gen(function* () {
796796
{ concurrency: 5 },
797797
);
798798
const listed = { ...pull, reviewers: reviewers.items, labels: labels.items };
799+
const checkList = checks === null ? [] : Json.toChecks(checks);
800+
// GitCafe's own counts include runs a re-run replaced; the newest runs are the state.
801+
const checksState =
802+
checks !== null
803+
? Json.checksStateOf(checkList)
804+
: status.checks.total === 0
805+
? null
806+
: status.checks.failing > 0
807+
? ("failing" as const)
808+
: status.checks.pending > 0
809+
? ("pending" as const)
810+
: ("passing" as const);
799811
const permissions = Json.toViewerPermissions(pull);
800812
const merging = status.merge.reasons?.some((reason) => MERGE_IN_FLIGHT.has(reason)) ?? false;
801813
return {
@@ -813,17 +825,8 @@ export const make = Effect.gen(function* () {
813825
mergedAt: pull.mergedAt,
814826
...settledMerge(pull),
815827
reviewers: Json.toReviewers(listed, input.host),
816-
checks: checks === null ? [] : Json.toChecks(checks),
817-
...(status.checks.total === 0
818-
? {}
819-
: {
820-
checksState:
821-
status.checks.failing > 0
822-
? ("failing" as const)
823-
: status.checks.pending > 0
824-
? ("pending" as const)
825-
: ("passing" as const),
826-
}),
828+
checks: checkList,
829+
...(checksState === null ? {} : { checksState }),
827830
mergeability:
828831
status.merge.conflicts === "conflicting"
829832
? ("conflicting" as const)

‎packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ describe("toChecks", () => {
429429
check("completed", "success", "Build"),
430430
check("completed", "cancelled"),
431431
check("completed", "action_required"),
432-
check("completed", "timed_out", "Build"),
432+
check("completed", "timed_out", "Lint"),
433433
],
434434
next: null,
435435
}),
@@ -438,12 +438,30 @@ describe("toChecks", () => {
438438
checks.map((item) => [item.name, item.status]),
439439
[
440440
["completed-cancelled", "cancelled"],
441-
["Build", "failure"],
441+
["Lint", "failure"],
442442
["queued-null", "pending"],
443443
["in_progress-null", "pending"],
444444
["Build", "success"],
445445
["completed-action_required", "action-required"],
446446
],
447447
);
448448
});
449+
450+
it("keeps only the newest run of a re-run check", () => {
451+
const run = (id: string, conclusion: string) => ({
452+
id,
453+
name: "Check",
454+
status: "completed",
455+
conclusion,
456+
summary: null,
457+
detailsUrl: null,
458+
});
459+
const statusOf = (items: ReadonlyArray<ReturnType<typeof run>>) =>
460+
GitCafePullRequestJson.toChecks(decodeChecks({ items, next: null })).map(
461+
(check) => check.status,
462+
);
463+
assert.deepStrictEqual(statusOf([run("99", "failure"), run("100", "success")]), ["success"]);
464+
assert.deepStrictEqual(statusOf([run("100", "success"), run("99", "failure")]), ["success"]);
465+
assert.deepStrictEqual(statusOf([run("100", "success"), run("101", "failure")]), ["failure"]);
466+
});
449467
});

‎packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TrimmedNonEmptyString,
1010
type PullRequestActor,
1111
type PullRequestCheck,
12+
type PullRequestChecksState,
1213
type PullRequestComment,
1314
type PullRequestReaction,
1415
type PullRequestReactionContent,
@@ -258,6 +259,7 @@ export const GitCafeDiff = Schema.Struct({
258259
export const GitCafeChecks = Schema.Struct({
259260
items: Schema.Array(
260261
Schema.Struct({
262+
id: Schema.optional(Schema.NullOr(Schema.String)),
261263
name: TrimmedNonEmptyString,
262264
status: Schema.Literals(["queued", "in_progress", "completed"]),
263265
conclusion: Schema.NullOr(Schema.String),
@@ -638,13 +640,39 @@ function checkStatus(check: (typeof GitCafeChecks.Type.items)[number]): PullRequ
638640
}
639641
}
640642

643+
type GitCafeCheck = (typeof GitCafeChecks.Type.items)[number];
644+
645+
// GitCafe's ids are the host's check run ids, which grow with each run.
646+
function isNewerRun(candidate: GitCafeCheck, kept: GitCafeCheck): boolean {
647+
const [a, b] = [candidate.id, kept.id];
648+
return a != null && b != null && /^\d+$/u.test(a) && /^\d+$/u.test(b) && BigInt(a) > BigInt(b);
649+
}
650+
651+
/**
652+
* GitCafe lists every run of a commit's checks, so a re-run arrives beside the run it replaced.
653+
* The newest run is the check's state; an older failure is history, not a failing check.
654+
*/
641655
export function toChecks(checks: typeof GitCafeChecks.Type): ReadonlyArray<PullRequestCheck> {
656+
const newestByName = new Map<string, GitCafeCheck>();
657+
for (const check of checks.items) {
658+
const kept = newestByName.get(check.name);
659+
if (kept === undefined || isNewerRun(check, kept)) newestByName.set(check.name, check);
660+
}
642661
return sortChecks(
643-
checks.items.map((check) => ({
662+
[...newestByName.values()].map((check) => ({
644663
name: check.name,
645664
status: checkStatus(check),
646665
description: check.summary,
647666
url: check.detailsUrl,
648667
})),
649668
);
650669
}
670+
671+
export function checksStateOf(
672+
checks: ReadonlyArray<PullRequestCheck>,
673+
): PullRequestChecksState | null {
674+
const statuses = checks.map((check) => check.status);
675+
if (statuses.includes("failure") || statuses.includes("cancelled")) return "failing";
676+
if (statuses.includes("pending") || statuses.includes("action-required")) return "pending";
677+
return statuses.includes("success") ? "passing" : null;
678+
}

0 commit comments

Comments
 (0)