Repository navigation
Expand file tree
/
Copy pathpullRequestStatus.ts
More file actions
228 lines (219 loc) · 8.21 KB
/
Copy pathpullRequestStatus.ts
File metadata and controls
228 lines (219 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import type {
PullRequestActivity,
PullRequestCheck,
PullRequestChecksState,
PullRequestDetail,
PullRequestListEntry,
PullRequestMergeability,
} from "@t3tools/contracts";
import type { AppSymbolName } from "../../components/ui/AppSymbol";
import { PullRequestGlyph } from "./pullRequestGlyphs";
const STATES = {
open: {
label: "Open",
icon: PullRequestGlyph.pullRequest,
tint: "accent-adaptive-emerald-600-400",
text: "text-adaptive-emerald-600-400",
background: "bg-adaptive-emerald-500-a12-a16",
},
merged: {
label: "Merged",
icon: PullRequestGlyph.merged,
tint: "accent-adaptive-violet-600-400",
text: "text-adaptive-violet-600-400",
background: "bg-adaptive-violet-500-a12-a16",
},
closed: {
label: "Closed",
icon: PullRequestGlyph.closed,
tint: "accent-danger-foreground",
text: "text-danger-foreground",
background: "bg-danger",
},
draft: {
label: "Draft",
icon: PullRequestGlyph.draft,
tint: "accent-icon-muted",
text: "text-foreground-muted",
background: "bg-grouped-card",
},
} satisfies Record<
string,
{ label: string; icon: AppSymbolName; tint: string; text: string; background: string }
>;
export function pullRequestStatus(pr: Pick<PullRequestListEntry, "state" | "isDraft">) {
return STATES[pr.isDraft && pr.state === "open" ? "draft" : pr.state];
}
export function pullRequestConflict(pr: {
readonly state: PullRequestListEntry["state"];
readonly isDraft: boolean;
readonly mergeability?: PullRequestMergeability | undefined;
readonly baseBranch?: string | undefined;
}) {
if (pr.state !== "open" || pr.isDraft || pr.mergeability !== "conflicting") return null;
return { label: pr.baseBranch ? `Conflicts with ${pr.baseBranch}` : "Has conflicts" };
}
export function checkStatus(check: PullRequestCheck) {
switch (check.status) {
case "success":
return {
label: "Passed",
icon: "checkmark.circle",
tint: "accent-adaptive-emerald-600-400",
} as const;
case "failure":
return {
label: "Failed",
icon: "exclamationmark.circle",
tint: "accent-danger-foreground",
} as const;
case "action-required":
return {
label: "Action required",
icon: "exclamationmark.circle",
tint: "accent-warning-foreground",
} as const;
case "pending":
return { label: "Pending", icon: "clock", tint: "accent-warning-foreground" } as const;
case "cancelled":
return { label: "Cancelled", icon: "minus", tint: "accent-icon-muted" } as const;
case "skipped":
return { label: "Skipped", icon: "slash.circle", tint: "accent-icon-muted" } as const;
case "neutral":
return { label: "Neutral", icon: "minus", tint: "accent-icon-muted" } as const;
}
}
export function pullRequestChecksSummary(pr: Pick<PullRequestDetail, "checks">) {
const failed = pr.checks.filter((check) => check.status === "failure").length;
const required = pr.checks.filter((check) => check.status === "action-required").length;
const pending = pr.checks.filter((check) => check.status === "pending").length;
const passed = pr.checks.filter((check) => check.status === "success").length;
const count = pr.checks.length ? `${passed} of ${pr.checks.length}` : undefined;
if (failed)
return {
label: `${failed} ${failed === 1 ? "check" : "checks"} failing`,
title: `${failed} ${failed === 1 ? "check" : "checks"} failing`,
count,
icon: "exclamationmark.circle",
tint: "accent-danger-foreground",
} as const;
if (required)
return {
label: `${required} need attention`,
title: "Checks need attention",
count: `${required} awaiting action`,
icon: "exclamationmark.circle",
tint: "accent-warning-foreground",
} as const;
if (pending)
return {
label: `${pending} pending`,
title: "Checks running",
count: `${pending} pending`,
icon: "clock",
tint: "accent-warning-foreground",
} as const;
if (passed)
return {
label: `${passed} of ${pr.checks.length} passed`,
title: passed === pr.checks.length ? "All checks passed" : "Checks completed",
count,
icon: "checkmark.circle",
tint: "accent-adaptive-emerald-600-400",
} as const;
return {
label: pr.checks.length ? `${pr.checks.length} completed` : "No checks reported",
title: pr.checks.length ? "Checks completed" : "No checks reported",
count,
icon: "minus",
tint: "accent-icon-muted",
} as const;
}
// GitHub and Forgejo send uppercase review states; Bitbucket and GitCafe send lowercase.
export function normalizedReviewState(reviewState: string | null) {
return reviewState?.toUpperCase().replaceAll("-", "_");
}
export function pullRequestReviewSummary(
pr: Pick<PullRequestDetail, "reviewers">,
activity: Pick<PullRequestActivity, "comments" | "commits"> | null,
) {
const latest = new Map<string, PullRequestActivity["comments"][number]>();
const comments = new Map<string, PullRequestActivity["comments"][number]>();
for (const review of activity?.comments ?? []) {
const state = normalizedReviewState(review.reviewState);
if (review.kind !== "review" || !state) continue;
const key = review.author?.login ?? review.id;
if (state === "COMMENTED") {
const previous = comments.get(key);
if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
comments.set(key, { ...review, reviewState: state });
continue;
}
if (!["APPROVED", "CHANGES_REQUESTED", "DISMISSED"].includes(state)) continue;
const previous = latest.get(key);
if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
latest.set(key, { ...review, reviewState: state });
}
const reviews = [
...[...latest.values()].filter((review) => review.reviewState !== "DISMISSED"),
...[...comments.entries()].filter(([key]) => !latest.has(key)).map(([, review]) => review),
];
const requested = reviews.filter((review) => review.reviewState === "CHANGES_REQUESTED");
const newestCommit = Math.max(
0,
...(activity?.commits.map((commit) => Date.parse(commit.committedDate)) ?? []),
);
const approved = reviews.filter(
(review) => review.reviewState === "APPROVED" && Date.parse(review.createdAt) >= newestCommit,
);
if (requested.length)
return {
title: "Changes requested",
count: `${requested.length} ${requested.length === 1 ? "reviewer" : "reviewers"}`,
icon: "xmark.circle.fill",
tint: "accent-danger-foreground",
reviews,
} as const;
if (approved.length)
return {
title:
approved.length === 1 && approved[0]?.author
? `Approved by ${approved[0].author.login}`
: "Reviews approved",
count: `${approved.length} approved`,
icon: "checkmark.circle",
tint: "accent-adaptive-emerald-600-400",
reviews,
} as const;
const commented = reviews.filter((review) => review.reviewState === "COMMENTED");
if (commented.length && !pr.reviewers.length)
return {
title: "Reviews",
count: `${commented.length} commented`,
icon: "bubble.left",
tint: "accent-icon-muted",
reviews,
} as const;
return {
title: pr.reviewers.length ? "Awaiting review" : "Request a review",
count: pr.reviewers.length ? `${pr.reviewers.length} requested` : "None assigned",
icon: "person.2",
tint: "accent-icon-muted",
reviews,
} as const;
}
export function pullRequestBranchSummary(
pr: Pick<PullRequestDetail, "mergeability" | "baseComparison" | "behindBy" | "baseBranch">,
) {
if (pr.mergeability === "conflicting") return "Has conflicts";
if (pr.baseComparison === "behind") return `${pr.behindBy ?? ""} behind ${pr.baseBranch}`.trim();
if (pr.baseComparison === "up-to-date") return "Up to date";
return pr.mergeability === "mergeable" ? "No conflicts" : "Not reported";
}
export function pullRequestChecksStatus(state: PullRequestChecksState) {
if (state === "passing")
return { icon: "checkmark.circle", tint: "accent-adaptive-emerald-600-400" } as const;
if (state === "failing")
return { icon: "xmark.circle.fill", tint: "accent-danger-foreground" } as const;
return { icon: "clock", tint: "accent-warning-foreground" } as const;
}