|
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,7 @@ 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)), |
261 | 263 | name: TrimmedNonEmptyString, |
262 | 264 | status: Schema.Literals(["queued", "in_progress", "completed"]), |
263 | 265 | conclusion: Schema.NullOr(Schema.String), |
@@ -638,13 +640,39 @@ function checkStatus(check: (typeof GitCafeChecks.Type.items)[number]): PullRequ |
638 | 640 | } |
639 | 641 | } |
640 | 642 |
|
| 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 | + */ |
641 | 655 | 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 | + } |
642 | 661 | return sortChecks( |
643 | | - checks.items.map((check) => ({ |
| 662 | + [...newestByName.values()].map((check) => ({ |
644 | 663 | name: check.name, |
645 | 664 | status: checkStatus(check), |
646 | 665 | description: check.summary, |
647 | 666 | url: check.detailsUrl, |
648 | 667 | })), |
649 | 668 | ); |
650 | 669 | } |
| 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