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