|
9 | 9 | TrimmedNonEmptyString, |
10 | 10 | type PullRequestActor, |
11 | 11 | type PullRequestCheck, |
| 12 | + type PullRequestChecksState, |
12 | 13 | type PullRequestComment, |
13 | 14 | type PullRequestReaction, |
14 | 15 | type PullRequestReactionContent, |
@@ -258,6 +259,9 @@ export const GitCafeDiff = Schema.Struct({ |
258 | 259 | export const GitCafeChecks = Schema.Struct({ |
259 | 260 | items: Schema.Array( |
260 | 261 | 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)), |
261 | 265 | name: TrimmedNonEmptyString, |
262 | 266 | status: Schema.Literals(["queued", "in_progress", "completed"]), |
263 | 267 | conclusion: Schema.NullOr(Schema.String), |
@@ -638,13 +642,53 @@ function checkStatus(check: (typeof GitCafeChecks.Type.items)[number]): PullRequ |
638 | 642 | } |
639 | 643 | } |
640 | 644 |
|
| 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 | + */ |
641 | 658 | 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 | + } |
642 | 677 | return sortChecks( |
643 | | - checks.items.map((check) => ({ |
| 678 | + kept.map((check) => ({ |
644 | 679 | name: check.name, |
645 | 680 | status: checkStatus(check), |
646 | 681 | description: check.summary, |
647 | 682 | url: check.detailsUrl, |
648 | 683 | })), |
649 | 684 | ); |
650 | 685 | } |
| 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