Skip to content

Commit ea41701

Browse files
fix(source-control): GitCafe checks show each check's newest run (pingdotgg#24406)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0345e85 commit ea41701

4 files changed

Lines changed: 142 additions & 14 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,57 @@ 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 items = [run("120859457782", "success"), run("120857059247", "failure")];
661+
const detailWith = (checks: (request: SentRequest) => unknown) => {
662+
const server = fakeGitCafe({
663+
"GET /repos/owner/repo/pulls/7": pull,
664+
"GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },
665+
"GET /repos/owner/repo/pulls/7/labels": { items: [], next: null },
666+
"GET /repos/owner/repo/pulls/7/status": {
667+
merge: { conflicts: "none", fastForward: null, strategies: [] },
668+
// GitCafe counts the cancelled run's failure beside the re-run that passed.
669+
checks: { pending: 0, failing: 1, total: 2 },
670+
},
671+
[`GET /repos/owner/repo/commits/${headOid}/checks`]: checks,
672+
"GET /repos/owner/repo/pulls/7/changes": {
673+
version: 4,
674+
headOid,
675+
comparisonBaseOid: baseOid,
676+
items: [],
677+
next: null,
678+
},
679+
});
680+
return Effect.gen(function* () {
681+
const provider = yield* GitCafePullRequestProvider.make;
682+
return yield* provider.getChangeRequest(target);
683+
}).pipe(Effect.provide(server.layer));
684+
};
685+
return Effect.gen(function* () {
686+
const detail = yield* detailWith(() => ({ items, next: null }));
687+
assert.deepStrictEqual(
688+
detail.checks.map((check) => [check.name, check.status]),
689+
[["Check", "success"]],
690+
);
691+
assert.strictEqual(detail.checksState, "passing");
692+
693+
// A listing cut off at the page limit may hide a failure, so GitCafe's counts decide.
694+
const truncated = yield* detailWith((request) => ({
695+
items,
696+
next: String(Number(request.query.get("after") ?? 0) + 1),
697+
}));
698+
assert.strictEqual(truncated.checksState, "failing");
699+
});
700+
});
701+
651702
it.effect("still reads a pull request whose reviewers the token may not see", () => {
652703
const server = fakeGitCafe({
653704
"GET /repos/owner/repo/pulls/7": { ...pull, headOid: null },

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,19 @@ 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, so the newest runs are the state. A
801+
// truncated listing may be missing a failure; the counts never call failing checks passing.
802+
const checksState =
803+
checks !== null && checks.next === null
804+
? Json.checksStateOf(checkList)
805+
: status.checks.total === 0
806+
? null
807+
: status.checks.failing > 0
808+
? ("failing" as const)
809+
: status.checks.pending > 0
810+
? ("pending" as const)
811+
: ("passing" as const);
799812
const permissions = Json.toViewerPermissions(pull);
800813
const merging = status.merge.reasons?.some((reason) => MERGE_IN_FLIGHT.has(reason)) ?? false;
801814
return {
@@ -813,17 +826,8 @@ export const make = Effect.gen(function* () {
813826
mergedAt: pull.mergedAt,
814827
...settledMerge(pull),
815828
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-
}),
829+
checks: checkList,
830+
...(checksState === null ? {} : { checksState }),
827831
mergeability:
828832
status.merge.conflicts === "conflicting"
829833
? ("conflicting" as const)

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

Lines changed: 31 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,41 @@ 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, githubApp = "github-actions") => ({
452+
id,
453+
provider: "github",
454+
githubApp,
455+
name: "Check",
456+
status: "completed",
457+
conclusion,
458+
summary: null,
459+
detailsUrl: null,
460+
});
461+
const statusOf = (items: ReadonlyArray<ReturnType<typeof run>>) =>
462+
GitCafePullRequestJson.toChecks(decodeChecks({ items, next: null })).map(
463+
(check) => check.status,
464+
);
465+
assert.deepStrictEqual(statusOf([run("99", "failure"), run("100", "success")]), ["success"]);
466+
assert.deepStrictEqual(statusOf([run("101", "failure"), run("100", "success")]), ["failure"]);
467+
// Another app's run of the same name is a separate check, not a re-run.
468+
assert.deepStrictEqual(statusOf([run("100", "failure", "a"), run("101", "success", "b")]), [
469+
"failure",
470+
"success",
471+
]);
472+
// Commit statuses carry no run order, so neither is taken for the other's re-run.
473+
assert.deepStrictEqual(statusOf([run("status:2", "failure"), run("status:1", "success")]), [
474+
"failure",
475+
"success",
476+
]);
477+
});
449478
});

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

Lines changed: 45 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,9 @@ 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)),
263+
provider: Schema.optional(Schema.NullOr(Schema.String)),
264+
githubApp: Schema.optional(Schema.NullOr(Schema.String)),
261265
name: TrimmedNonEmptyString,
262266
status: Schema.Literals(["queued", "in_progress", "completed"]),
263267
conclusion: Schema.NullOr(Schema.String),
@@ -638,13 +642,53 @@ function checkStatus(check: (typeof GitCafeChecks.Type.items)[number]): PullRequ
638642
}
639643
}
640644

645+
type GitCafeCheck = (typeof GitCafeChecks.Type.items)[number];
646+
647+
/** A check run's id, which grows with each run. Commit statuses (`status:…`) carry no such order. */
648+
function runOrder(check: GitCafeCheck): bigint | null {
649+
return check.id != null && /^\d+$/u.test(check.id) ? BigInt(check.id) : null;
650+
}
651+
652+
/**
653+
* GitCafe lists every run of a commit's checks, so a re-run arrives beside the run it replaced.
654+
* The newest run is the check's state; an older failure is history, not a failing check. Runs whose
655+
* order cannot be told apart are all kept, so no failure is hidden. Two reporters may share a check
656+
* name, so a run only replaces runs from the same provider and GitHub app.
657+
*/
641658
export function toChecks(checks: typeof GitCafeChecks.Type): ReadonlyArray<PullRequestCheck> {
659+
const kept: Array<GitCafeCheck> = [];
660+
const newestByKey = new Map<string, { index: number; order: bigint }>();
661+
for (const check of checks.items) {
662+
const order = runOrder(check);
663+
if (order === null) {
664+
kept.push(check);
665+
continue;
666+
}
667+
const key = JSON.stringify([check.provider ?? null, check.githubApp ?? null, check.name]);
668+
const newest = newestByKey.get(key);
669+
if (newest === undefined) {
670+
newestByKey.set(key, { index: kept.length, order });
671+
kept.push(check);
672+
} else if (order > newest.order) {
673+
kept[newest.index] = check;
674+
newest.order = order;
675+
}
676+
}
642677
return sortChecks(
643-
checks.items.map((check) => ({
678+
kept.map((check) => ({
644679
name: check.name,
645680
status: checkStatus(check),
646681
description: check.summary,
647682
url: check.detailsUrl,
648683
})),
649684
);
650685
}
686+
687+
export function checksStateOf(
688+
checks: ReadonlyArray<PullRequestCheck>,
689+
): PullRequestChecksState | null {
690+
const statuses = new Set(checks.map((check) => check.status));
691+
if (statuses.has("failure") || statuses.has("cancelled")) return "failing";
692+
if (statuses.has("pending") || statuses.has("action-required")) return "pending";
693+
return statuses.has("success") ? "passing" : null;
694+
}

0 commit comments

Comments
 (0)