TypeScript
fix(source-control): GitCafe checks show each check's newest run (#24406)
Julius Marmingeea41701979c3
diff --git a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.tsindex 3a9ef4e852b..7ac94bfd963 100644--- a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts+++ b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts@@ -648,6 +648,57 @@ describe("GitCafePullRequestProvider", () => {}).pipe(Effect.provide(server.layer));});+ it.effect("reads the checks state from each check's newest run", () => {+ const run = (id: string, conclusion: string) => ({+ id,+ name: "Check",+ status: "completed",+ conclusion,+ summary: null,+ detailsUrl: null,+ });+ const items = [run("120859457782", "success"), run("120857059247", "failure")];+ const detailWith = (checks: (request: SentRequest) => unknown) => {+ const server = fakeGitCafe({+ "GET /repos/owner/repo/pulls/7": pull,+ "GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },+ "GET /repos/owner/repo/pulls/7/labels": { items: [], next: null },+ "GET /repos/owner/repo/pulls/7/status": {+ merge: { conflicts: "none", fastForward: null, strategies: [] },+ // GitCafe counts the cancelled run's failure beside the re-run that passed.+ checks: { pending: 0, failing: 1, total: 2 },+ },+ [`GET /repos/owner/repo/commits/${headOid}/checks`]: checks,+ "GET /repos/owner/repo/pulls/7/changes": {+ version: 4,+ headOid,+ comparisonBaseOid: baseOid,+ items: [],+ next: null,+ },+ });+ return Effect.gen(function* () {+ const provider = yield* GitCafePullRequestProvider.make;+ return yield* provider.getChangeRequest(target);+ }).pipe(Effect.provide(server.layer));+ };+ return Effect.gen(function* () {+ const detail = yield* detailWith(() => ({ items, next: null }));+ assert.deepStrictEqual(+ detail.checks.map((check) => [check.name, check.status]),+ [["Check", "success"]],+ );+ assert.strictEqual(detail.checksState, "passing");++ // A listing cut off at the page limit may hide a failure, so GitCafe's counts decide.+ const truncated = yield* detailWith((request) => ({+ items,+ next: String(Number(request.query.get("after") ?? 0) + 1),+ }));+ assert.strictEqual(truncated.checksState, "failing");+ });+ });+it.effect("still reads a pull request whose reviewers the token may not see", () => {const server = fakeGitCafe({"GET /repos/owner/repo/pulls/7": { ...pull, headOid: null },diff --git a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.tsindex 04bf4c4b595..6d62801ad4b 100644--- a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts+++ b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts@@ -796,6 +796,19 @@ export const make = Effect.gen(function* () {{ concurrency: 5 },);const listed = { ...pull, reviewers: reviewers.items, labels: labels.items };+ const checkList = checks === null ? [] : Json.toChecks(checks);+ // GitCafe's own counts include runs a re-run replaced, so the newest runs are the state. A+ // truncated listing may be missing a failure; the counts never call failing checks passing.+ const checksState =+ checks !== null && checks.next === null+ ? Json.checksStateOf(checkList)+ : status.checks.total === 0+ ? null+ : status.checks.failing > 0+ ? ("failing" as const)+ : status.checks.pending > 0+ ? ("pending" as const)+ : ("passing" as const);const permissions = Json.toViewerPermissions(pull);const merging = status.merge.reasons?.some((reason) => MERGE_IN_FLIGHT.has(reason)) ?? false;return {@@ -813,17 +826,8 @@ export const make = Effect.gen(function* () {mergedAt: pull.mergedAt,...settledMerge(pull),reviewers: Json.toReviewers(listed, input.host),- checks: checks === null ? [] : Json.toChecks(checks),- ...(status.checks.total === 0- ? {}- : {- checksState:- status.checks.failing > 0- ? ("failing" as const)- : status.checks.pending > 0- ? ("pending" as const)- : ("passing" as const),- }),+ checks: checkList,+ ...(checksState === null ? {} : { checksState }),mergeability:status.merge.conflicts === "conflicting"? ("conflicting" as const)diff --git a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.ts b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.tsindex f3b08b6c647..b3a070b8417 100644--- a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.ts+++ b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.ts@@ -429,7 +429,7 @@ describe("toChecks", () => {check("completed", "success", "Build"),check("completed", "cancelled"),check("completed", "action_required"),- check("completed", "timed_out", "Build"),+ check("completed", "timed_out", "Lint"),],next: null,}),@@ -438,7 +438,7 @@ describe("toChecks", () => {checks.map((item) => [item.name, item.status]),[["completed-cancelled", "cancelled"],- ["Build", "failure"],+ ["Lint", "failure"],["queued-null", "pending"],["in_progress-null", "pending"],["Build", "success"],@@ -446,4 +446,33 @@ describe("toChecks", () => {],);});++ it("keeps only the newest run of a re-run check", () => {+ const run = (id: string, conclusion: string, githubApp = "github-actions") => ({+ id,+ provider: "github",+ githubApp,+ name: "Check",+ status: "completed",+ conclusion,+ summary: null,+ detailsUrl: null,+ });+ const statusOf = (items: ReadonlyArray<ReturnType<typeof run>>) =>+ GitCafePullRequestJson.toChecks(decodeChecks({ items, next: null })).map(+ (check) => check.status,+ );+ assert.deepStrictEqual(statusOf([run("99", "failure"), run("100", "success")]), ["success"]);+ assert.deepStrictEqual(statusOf([run("101", "failure"), run("100", "success")]), ["failure"]);+ // Another app's run of the same name is a separate check, not a re-run.+ assert.deepStrictEqual(statusOf([run("100", "failure", "a"), run("101", "success", "b")]), [+ "failure",+ "success",+ ]);+ // Commit statuses carry no run order, so neither is taken for the other's re-run.+ assert.deepStrictEqual(statusOf([run("status:2", "failure"), run("status:1", "success")]), [+ "failure",+ "success",+ ]);+ });});diff --git a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.tsindex a3ba916bd3b..c932b243b23 100644--- a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts+++ b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts@@ -9,6 +9,7 @@ import {TrimmedNonEmptyString,type PullRequestActor,type PullRequestCheck,+ type PullRequestChecksState,type PullRequestComment,type PullRequestReaction,type PullRequestReactionContent,@@ -258,6 +259,9 @@ export const GitCafeDiff = Schema.Struct({export const GitCafeChecks = Schema.Struct({items: Schema.Array(Schema.Struct({+ id: Schema.optional(Schema.NullOr(Schema.String)),+ provider: Schema.optional(Schema.NullOr(Schema.String)),+ githubApp: Schema.optional(Schema.NullOr(Schema.String)),name: TrimmedNonEmptyString,status: Schema.Literals(["queued", "in_progress", "completed"]),conclusion: Schema.NullOr(Schema.String),@@ -638,9 +642,40 @@ function checkStatus(check: (typeof GitCafeChecks.Type.items)[number]): PullRequ}}+type GitCafeCheck = (typeof GitCafeChecks.Type.items)[number];++/** A check run's id, which grows with each run. Commit statuses (`status:…`) carry no such order. */+function runOrder(check: GitCafeCheck): bigint | null {+ return check.id != null && /^\d+$/u.test(check.id) ? BigInt(check.id) : null;+}++/**+ * GitCafe lists every run of a commit's checks, so a re-run arrives beside the run it replaced.+ * The newest run is the check's state; an older failure is history, not a failing check. Runs whose+ * order cannot be told apart are all kept, so no failure is hidden. Two reporters may share a check+ * name, so a run only replaces runs from the same provider and GitHub app.+ */export function toChecks(checks: typeof GitCafeChecks.Type): ReadonlyArray<PullRequestCheck> {+ const kept: Array<GitCafeCheck> = [];+ const newestByKey = new Map<string, { index: number; order: bigint }>();+ for (const check of checks.items) {+ const order = runOrder(check);+ if (order === null) {+ kept.push(check);+ continue;+ }+ const key = JSON.stringify([check.provider ?? null, check.githubApp ?? null, check.name]);+ const newest = newestByKey.get(key);+ if (newest === undefined) {+ newestByKey.set(key, { index: kept.length, order });+ kept.push(check);+ } else if (order > newest.order) {+ kept[newest.index] = check;+ newest.order = order;+ }+ }return sortChecks(- checks.items.map((check) => ({+ kept.map((check) => ({name: check.name,status: checkStatus(check),description: check.summary,@@ -648,3 +683,12 @@ export function toChecks(checks: typeof GitCafeChecks.Type): ReadonlyArray<PullR})),);}++export function checksStateOf(+ checks: ReadonlyArray<PullRequestCheck>,+): PullRequestChecksState | null {+ const statuses = new Set(checks.map((check) => check.status));+ if (statuses.has("failure") || statuses.has("cancelled")) return "failing";+ if (statuses.has("pending") || statuses.has("action-required")) return "pending";+ return statuses.has("success") ? "passing" : null;+}