Open History to see the last commit
1import type {
2 PullRequestActivity,
3 PullRequestCheck,
4 PullRequestChecksState,
5 PullRequestDetail,
6 PullRequestListEntry,
7 PullRequestMergeability,
8} from "@t3tools/contracts";
9import { 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("Open", { $context: "pull request state" }),
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("Merged", { $context: "pull request state" }),
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("Closed", { $context: "pull request state" }),
32 icon: PullRequestGlyph.closed,
33 tint: "accent-danger-foreground",
34 text: "text-danger-foreground",
35 background: "bg-danger",
36 },
37 draft: {
38 label: msg("Draft", { $context: "pull request state" }),
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; 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) };
55}
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}", { branch: pr.baseBranch }))
70 : resolve(msg("Has conflicts")),
71 };
72}
73
74export function checkStatus(check: PullRequestCheck, resolve: MessageResolver = m) {
75 switch (check.status) {
76 case "success":
77 return {
78 label: resolve(msg("Passed", { $context: "CI check status" })),
79 icon: "checkmark.circle",
80 tint: "accent-adaptive-emerald-600-400",
81 } as const;
82 case "failure":
83 return {
84 label: resolve(msg("Failed", { $context: "CI check status" })),
85 icon: "exclamationmark.circle",
86 tint: "accent-danger-foreground",
87 } as const;
88 case "action-required":
89 return {
90 label: resolve(msg("Action required", { $context: "CI check status" })),
91 icon: "exclamationmark.circle",
92 tint: "accent-warning-foreground",
93 } as const;
94 case "pending":
95 return {
96 label: resolve(msg("Pending", { $context: "CI check status" })),
97 icon: "clock",
98 tint: "accent-warning-foreground",
99 } as const;
100 case "cancelled":
101 return {
102 label: resolve(msg("Cancelled", { $context: "CI check status" })),
103 icon: "minus",
104 tint: "accent-icon-muted",
105 } as const;
106 case "skipped":
107 return {
108 label: resolve(msg("Skipped", { $context: "CI check status" })),
109 icon: "slash.circle",
110 tint: "accent-icon-muted",
111 } as const;
112 case "neutral":
113 return {
114 label: resolve(msg("Neutral", { $context: "CI check status" })),
115 icon: "minus",
116 tint: "accent-icon-muted",
117 } as const;
118 }
119}
120
121export function isFailingCheck(check: PullRequestCheck) {
122 return check.status === "failure" || check.status === "cancelled";
123}
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}", { passed, total })) : undefined;
135 if (failed) {
136 const failing = resolve(
137 msg("{count, plural, one {{count} check failing} other {{count} checks failing}}", {
138 count: failed,
139 }),
140 );
141 return {
142 label: failing,
143 title: failing,
144 count,
145 icon: "exclamationmark.circle",
146 tint: "accent-danger-foreground",
147 } as const;
148 }
149 if (required)
150 return {
151 label: resolve(
152 msg("{count, plural, one {{count} need attention} other {{count} need attention}}", {
153 count: required,
154 }),
155 ),
156 title: resolve(msg("Checks need attention")),
157 count: resolve(
158 msg("{count, plural, one {{count} awaiting action} other {{count} awaiting action}}", {
159 count: required,
160 }),
161 ),
162 icon: "exclamationmark.circle",
163 tint: "accent-warning-foreground",
164 } as const;
165 if (pending)
166 return {
167 label: resolve(
168 msg("{count, plural, one {{count} pending} other {{count} pending}}", { count: pending }),
169 ),
170 title: resolve(msg("Checks running")),
171 count: resolve(
172 msg("{count, plural, one {{count} pending} other {{count} pending}}", { count: pending }),
173 ),
174 icon: "clock",
175 tint: "accent-warning-foreground",
176 } as const;
177 if (passed)
178 return {
179 label: resolve(msg("{passed} of {total} passed", { passed, total })),
180 title:
181 passed === total ? resolve(msg("All checks passed")) : resolve(msg("Checks completed")),
182 count,
183 icon: "checkmark.circle",
184 tint: "accent-adaptive-emerald-600-400",
185 } as const;
186 return {
187 label: total
188 ? resolve(
189 msg("{count, plural, one {{count} completed} other {{count} completed}}", {
190 count: total,
191 }),
192 )
193 : resolve(msg("No checks reported")),
194 title: total ? resolve(msg("Checks completed")) : resolve(msg("No checks reported")),
195 count,
196 icon: "minus",
197 tint: "accent-icon-muted",
198 } as const;
199}
200
201// GitHub and Forgejo send uppercase review states; Bitbucket and GitCafe send lowercase.
202export function normalizedReviewState(reviewState: string | null) {
203 return reviewState?.toUpperCase().replaceAll("-", "_");
204}
205
206export function pullRequestReviewSummary(
207 pr: Pick<PullRequestDetail, "reviewers">,
208 activity: Pick<PullRequestActivity, "comments" | "commits"> | null,
209 resolve: MessageResolver = m,
210) {
211 const latest = new Map<string, PullRequestActivity["comments"][number]>();
212 const comments = new Map<string, PullRequestActivity["comments"][number]>();
213 for (const review of activity?.comments ?? []) {
214 const state = normalizedReviewState(review.reviewState);
215 if (review.kind !== "review" || !state) continue;
216 const key = review.author?.login ?? review.id;
217 if (state === "COMMENTED") {
218 const previous = comments.get(key);
219 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
220 comments.set(key, { ...review, reviewState: state });
221 continue;
222 }
223 if (!["APPROVED", "CHANGES_REQUESTED", "DISMISSED"].includes(state)) continue;
224 const previous = latest.get(key);
225 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
226 latest.set(key, { ...review, reviewState: state });
227 }
228 const newestCommit = Math.max(
229 0,
230 ...(activity?.commits.map((commit) => Date.parse(commit.committedDate)) ?? []),
231 );
232 const reviews = [
233 ...[...latest.values()].filter((review) => review.reviewState !== "DISMISSED"),
234 ...[...comments.entries()].filter(([key]) => !latest.has(key)).map(([, review]) => review),
235 ].map((review) => ({
236 ...review,
237 outdated: review.reviewState === "APPROVED" && Date.parse(review.createdAt) < newestCommit,
238 }));
239 const requested = reviews.filter((review) => review.reviewState === "CHANGES_REQUESTED");
240 const approved = reviews.filter(
241 (review) => review.reviewState === "APPROVED" && !review.outdated,
242 );
243 if (requested.length)
244 return {
245 title: resolve(msg("Changes requested")),
246 count: resolve(
247 msg("{count, plural, one {{count} reviewer} other {{count} reviewers}}", {
248 count: requested.length,
249 }),
250 ),
251 icon: "xmark.circle.fill",
252 tint: "accent-danger-foreground",
253 reviews,
254 } as const;
255 if (approved.length)
256 return {
257 title:
258 approved.length === 1 && approved[0]?.author
259 ? resolve(msg("Approved by {login}", { login: approved[0].author.login }))
260 : resolve(msg("Reviews approved")),
261 count: resolve(
262 msg("{count, plural, one {{count} approved} other {{count} approved}}", {
263 count: approved.length,
264 }),
265 ),
266 icon: "checkmark.circle",
267 tint: "accent-adaptive-emerald-600-400",
268 reviews,
269 } as const;
270 const commented = reviews.filter((review) => review.reviewState === "COMMENTED");
271 if (commented.length && !pr.reviewers.length)
272 return {
273 title: resolve(msg("Reviews")),
274 count: resolve(
275 msg("{count, plural, one {{count} commented} other {{count} commented}}", {
276 count: commented.length,
277 }),
278 ),
279 icon: "bubble.left",
280 tint: "accent-icon-muted",
281 reviews,
282 } as const;
283 return {
284 title: pr.reviewers.length ? resolve(msg("Awaiting review")) : resolve(msg("Request a review")),
285 count: pr.reviewers.length
286 ? resolve(
287 msg("{count, plural, one {{count} requested} other {{count} requested}}", {
288 count: pr.reviewers.length,
289 }),
290 )
291 : resolve(msg("None assigned")),
292 icon: "person.2",
293 tint: "accent-icon-muted",
294 reviews,
295 } as const;
296}
297
298export function pullRequestBranchSummary(
299 pr: Pick<PullRequestDetail, "mergeability" | "baseComparison" | "behindBy" | "baseBranch">,
300 resolve: MessageResolver = m,
301) {
302 if (pr.mergeability === "conflicting") return resolve(msg("Has conflicts"));
303 if (pr.baseComparison === "behind")
304 return pr.behindBy == null
305 ? resolve(msg("behind {branch}", { branch: pr.baseBranch }))
306 : resolve(
307 msg("{count, plural, one {{count} behind {branch}} other {{count} behind {branch}}}", {
308 count: pr.behindBy,
309 branch: pr.baseBranch,
310 }),
311 );
312 if (pr.baseComparison === "up-to-date") return resolve(msg("Up to date"));
313 return pr.mergeability === "mergeable"
314 ? resolve(msg("No conflicts"))
315 : resolve(msg("Not reported"));
316}
317
318export function pullRequestChecksStatus(state: PullRequestChecksState) {
319 if (state === "passing")
320 return { icon: "checkmark.circle", tint: "accent-adaptive-emerald-600-400" } as const;
321 if (state === "failing")
322 return { icon: "xmark.circle.fill", tint: "accent-danger-foreground" } as const;
323 return { icon: "clock", tint: "accent-warning-foreground" } as const;
324}