Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestStatus.ts

371 lines12.0 KB

1import type {
2 PullRequestActivity,
3 PullRequestCheck,
4 PullRequestChecksState,
5 PullRequestDetail,
6 PullRequestListEntry,
7 PullRequestMergeability,
8} from "@t3tools/contracts";
9import { msg, plural, part, type Msg } from "gt-react-native";
10​
11import type { AppSymbolName } from "../../components/ui/AppSymbol";
12import { m, type MessageResolver } from "../../i18n/messages";
13import { PullRequestGlyph } from "./pullRequestGlyphs";
14​
15const STATES = {
16 open: {
17 label: msg({ context: "pull request state" })`Open`,
18 icon: PullRequestGlyph.pullRequest,
19 tint: "accent-adaptive-emerald-600-400",
20 text: "text-adaptive-emerald-600-400",
21 background: "bg-adaptive-emerald-500-a12-a16",
22 },
23 merged: {
24 label: msg({ context: "pull request state" })`Merged`,
25 icon: PullRequestGlyph.merged,
26 tint: "accent-adaptive-violet-600-400",
27 text: "text-adaptive-violet-600-400",
28 background: "bg-adaptive-violet-500-a12-a16",
29 },
30 closed: {
31 label: msg({ context: "pull request state" })`Closed`,
32 icon: PullRequestGlyph.closed,
33 tint: "accent-danger-foreground",
34 text: "text-danger-foreground",
35 background: "bg-danger",
36 },
37 draft: {
38 label: msg({ context: "pull request state" })`Draft`,
39 icon: PullRequestGlyph.draft,
40 tint: "accent-icon-muted",
41 text: "text-foreground-muted",
42 background: "bg-grouped-card",
43 },
44} satisfies Record<
45 string,
46 { label: string | Msg; icon: AppSymbolName; tint: string; text: string; background: string }
47>;
48​
49export function pullRequestStatus(
50 pr: Pick<PullRequestListEntry, "state" | "isDraft">,
51 resolve: MessageResolver = m,
52) {
53 const status = STATES[pr.isDraft && pr.state === "open" ? "draft" : pr.state];
54 return { ...status, label: resolve(status.label) };
56​
57export function pullRequestConflict(
58 pr: {
59 readonly state: PullRequestListEntry["state"];
60 readonly isDraft: boolean;
61 readonly mergeability?: PullRequestMergeability | undefined;
62 readonly baseBranch?: string | undefined;
63 },
64 resolve: MessageResolver = m,
65) {
66 if (pr.state !== "open" || pr.isDraft || pr.mergeability !== "conflicting") return null;
67 return {
68 label: pr.baseBranch
69 ? resolve(msg`Conflicts with ${{ branch: pr.baseBranch }}`)
70 : resolve(msg("Has conflicts")),
71 };
73​
74export function checkStatus(check: PullRequestCheck, resolve: MessageResolver = m) {
75 switch (check.status) {
76 case "success":
77 return {
78 label: resolve(msg({ context: "CI check status" })`Passed`),
79 icon: "checkmark.circle",
80 tint: "accent-adaptive-emerald-600-400",
81 } as const;
82 case "failure":
83 return {
84 label: resolve(msg({ context: "CI check status" })`Failed`),
85 icon: "exclamationmark.circle",
86 tint: "accent-danger-foreground",
87 } as const;
88 case "action-required":
89 return {
90 label: resolve(msg({ context: "CI check status" })`Action required`),
91 icon: "exclamationmark.circle",
92 tint: "accent-warning-foreground",
93 } as const;
94 case "pending":
95 return {
96 label: resolve(msg({ context: "CI check status" })`Pending`),
97 icon: "clock",
98 tint: "accent-warning-foreground",
99 } as const;
100 case "cancelled":
101 return {
102 label: resolve(msg({ context: "CI check status" })`Cancelled`),
103 icon: "minus",
104 tint: "accent-icon-muted",
105 } as const;
106 case "skipped":
107 return {
108 label: resolve(msg({ context: "CI check status" })`Skipped`),
109 icon: "slash.circle",
110 tint: "accent-icon-muted",
111 } as const;
112 case "neutral":
113 return {
114 label: resolve(msg({ context: "CI check status" })`Neutral`),
115 icon: "minus",
116 tint: "accent-icon-muted",
117 } as const;
118 }
120​
121export function isFailingCheck(check: PullRequestCheck) {
122 return check.status === "failure" || check.status === "cancelled";
124​
125export function pullRequestChecksSummary(
126 pr: Pick<PullRequestDetail, "checks">,
127 resolve: MessageResolver = m,
128) {
129 const failed = pr.checks.filter(isFailingCheck).length;
130 const required = pr.checks.filter((check) => check.status === "action-required").length;
131 const pending = pr.checks.filter((check) => check.status === "pending").length;
132 const passed = pr.checks.filter((check) => check.status === "success").length;
133 const total = pr.checks.length;
134 const count = total ? resolve(msg`${{ passed }} of ${{ total }}`) : undefined;
135 if (failed) {
136 const failing = resolve(
137 msg`${plural(
138 { count: failed },
139 {
140 one: part`${{ count: failed }} check failing`,
141 other: part`${{ count: failed }} checks failing`,
142 },
143 )}`,
144 );
145 return {
146 label: failing,
147 title: failing,
148 count,
149 icon: "exclamationmark.circle",
150 tint: "accent-danger-foreground",
151 } as const;
152 }
153 if (required)
154 return {
155 label: resolve(
156 msg`${plural(
157 { count: required },
158 {
159 one: part`${{ count: required }} needs attention`,
160 other: part`${{ count: required }} need attention`,
161 },
162 )}`,
163 ),
164 title: resolve(msg("Checks need attention")),
165 count: resolve(
166 msg`${plural(
167 { count: required },
168 {
169 one: part`${{ count: required }} awaiting action`,
170 other: part`${{ count: required }} awaiting action`,
171 },
172 )}`,
173 ),
174 icon: "exclamationmark.circle",
175 tint: "accent-warning-foreground",
176 } as const;
177 if (pending)
178 return {
179 label: resolve(
180 msg`${plural(
181 { count: pending },
182 {
183 one: part`${{ count: pending }} pending`,
184 other: part`${{ count: pending }} pending`,
185 },
186 )}`,
187 ),
188 title: resolve(msg("Checks running")),
189 count: resolve(
190 msg`${plural(
191 { count: pending },
192 {
193 one: part`${{ count: pending }} pending`,
194 other: part`${{ count: pending }} pending`,
195 },
196 )}`,
197 ),
198 icon: "clock",
199 tint: "accent-warning-foreground",
200 } as const;
201 if (passed)
202 return {
203 label: resolve(msg`${{ passed }} of ${{ total }} passed`),
204 title:
205 passed === total ? resolve(msg("All checks passed")) : resolve(msg("Checks completed")),
206 count,
207 icon: "checkmark.circle",
208 tint: "accent-adaptive-emerald-600-400",
209 } as const;
210 return {
211 label: total
212 ? resolve(
213 msg`${plural(
214 { count: total },
215 {
216 one: part`${{ count: total }} completed`,
217 other: part`${{ count: total }} completed`,
218 },
219 )}`,
220 )
221 : resolve(msg("No checks reported")),
222 title: total ? resolve(msg("Checks completed")) : resolve(msg("No checks reported")),
223 count,
224 icon: "minus",
225 tint: "accent-icon-muted",
226 } as const;
228​
229// GitHub and Forgejo send uppercase review states; Bitbucket and GitCafe send lowercase.
230export function normalizedReviewState(reviewState: string | null) {
231 return reviewState?.toUpperCase().replaceAll("-", "_");
233​
234export function pullRequestReviewSummary(
235 pr: Pick<PullRequestDetail, "reviewers">,
236 activity: Pick<PullRequestActivity, "comments" | "commits"> | null,
237 resolve: MessageResolver = m,
238) {
239 const latest = new Map<string, PullRequestActivity["comments"][number]>();
240 const comments = new Map<string, PullRequestActivity["comments"][number]>();
241 for (const review of activity?.comments ?? []) {
242 const state = normalizedReviewState(review.reviewState);
243 if (review.kind !== "review" || !state) continue;
244 const key = review.author?.login ?? review.id;
245 if (state === "COMMENTED") {
246 const previous = comments.get(key);
247 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
248 comments.set(key, { ...review, reviewState: state });
249 continue;
250 }
251 if (!["APPROVED", "CHANGES_REQUESTED", "DISMISSED"].includes(state)) continue;
252 const previous = latest.get(key);
253 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
254 latest.set(key, { ...review, reviewState: state });
255 }
256 const newestCommit = Math.max(
257 0,
258 ...(activity?.commits.map((commit) => Date.parse(commit.committedDate)) ?? []),
259 );
260 const reviews = [
261 ...[...latest.values()].filter((review) => review.reviewState !== "DISMISSED"),
262 ...[...comments.entries()].filter(([key]) => !latest.has(key)).map(([, review]) => review),
263 ].map((review) => ({
264 ...review,
265 outdated: review.reviewState === "APPROVED" && Date.parse(review.createdAt) < newestCommit,
266 }));
267 const requested = reviews.filter((review) => review.reviewState === "CHANGES_REQUESTED");
268 const approved = reviews.filter(
269 (review) => review.reviewState === "APPROVED" && !review.outdated,
270 );
271 if (requested.length)
272 return {
273 title: resolve(msg("Changes requested")),
274 count: resolve(
275 msg`${plural(
276 { count: requested.length },
277 {
278 one: part`${{ count: requested.length }} reviewer`,
279 other: part`${{ count: requested.length }} reviewers`,
280 },
281 )}`,
282 ),
283 icon: "xmark.circle.fill",
284 tint: "accent-danger-foreground",
285 reviews,
286 } as const;
287 if (approved.length)
288 return {
289 title:
290 approved.length === 1 && approved[0]?.author
291 ? resolve(msg`Approved by ${{ login: approved[0].author.login }}`)
292 : resolve(msg("Reviews approved")),
293 count: resolve(
294 msg`${plural(
295 { count: approved.length },
296 {
297 one: part`${{ count: approved.length }} approved`,
298 other: part`${{ count: approved.length }} approved`,
299 },
300 )}`,
301 ),
302 icon: "checkmark.circle",
303 tint: "accent-adaptive-emerald-600-400",
304 reviews,
305 } as const;
306 const commented = reviews.filter((review) => review.reviewState === "COMMENTED");
307 if (commented.length && !pr.reviewers.length)
308 return {
309 title: resolve(msg("Reviews")),
310 count: resolve(
311 msg`${plural(
312 { count: commented.length },
313 {
314 one: part`${{ count: commented.length }} commented`,
315 other: part`${{ count: commented.length }} commented`,
316 },
317 )}`,
318 ),
319 icon: "bubble.left",
320 tint: "accent-icon-muted",
321 reviews,
322 } as const;
323 return {
324 title: pr.reviewers.length ? resolve(msg("Awaiting review")) : resolve(msg("Request a review")),
325 count: pr.reviewers.length
326 ? resolve(
327 msg`${plural(
328 { count: pr.reviewers.length },
329 {
330 one: part`${{ count: pr.reviewers.length }} requested`,
331 other: part`${{ count: pr.reviewers.length }} requested`,
332 },
333 )}`,
334 )
335 : resolve(msg("None assigned")),
336 icon: "person.2",
337 tint: "accent-icon-muted",
338 reviews,
339 } as const;
341​
342export function pullRequestBranchSummary(
343 pr: Pick<PullRequestDetail, "mergeability" | "baseComparison" | "behindBy" | "baseBranch">,
344 resolve: MessageResolver = m,
345) {
346 if (pr.mergeability === "conflicting") return resolve(msg("Has conflicts"));
347 if (pr.baseComparison === "behind")
348 return pr.behindBy == null
349 ? resolve(msg`behind ${{ branch: pr.baseBranch }}`)
350 : resolve(
351 msg`${plural(
352 { count: pr.behindBy },
353 {
354 one: part`${{ count: pr.behindBy }} behind ${{ branch: pr.baseBranch }}`,
355 other: part`${{ count: pr.behindBy }} behind ${{ branch: pr.baseBranch }}`,
356 },
357 )}`,
358 );
359 if (pr.baseComparison === "up-to-date") return resolve(msg("Up to date"));
360 return pr.mergeability === "mergeable"
361 ? resolve(msg("No conflicts"))
362 : resolve(msg("Not reported"));
364​
365export function pullRequestChecksStatus(state: PullRequestChecksState) {
366 if (state === "passing")
367 return { icon: "checkmark.circle", tint: "accent-adaptive-emerald-600-400" } as const;
368 if (state === "failing")
369 return { icon: "xmark.circle.fill", tint: "accent-danger-foreground" } as const;
370 return { icon: "clock", tint: "accent-warning-foreground" } as const;