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 pullRequestChecksSummary(pr: Pick<PullRequestDetail, "checks">) {
92 const failed = pr.checks.filter((check) => check.status === "failure").length;
93 const required = pr.checks.filter((check) => check.status === "action-required").length;
94 const pending = pr.checks.filter((check) => check.status === "pending").length;
95 const passed = pr.checks.filter((check) => check.status === "success").length;
96 const count = pr.checks.length ? `${passed} of ${pr.checks.length}` : undefined;
97 if (failed)
98 return {
99 label: `${failed} ${failed === 1 ? "check" : "checks"} failing`,
100 title: `${failed} ${failed === 1 ? "check" : "checks"} failing`,
101 count,
102 icon: "exclamationmark.circle",
103 tint: "accent-danger-foreground",
104 } as const;
105 if (required)
106 return {
107 label: `${required} need attention`,
108 title: "Checks need attention",
109 count: `${required} awaiting action`,
110 icon: "exclamationmark.circle",
111 tint: "accent-warning-foreground",
112 } as const;
113 if (pending)
114 return {
115 label: `${pending} pending`,
116 title: "Checks running",
117 count: `${pending} pending`,
118 icon: "clock",
119 tint: "accent-warning-foreground",
120 } as const;
121 if (passed)
122 return {
123 label: `${passed} of ${pr.checks.length} passed`,
124 title: passed === pr.checks.length ? "All checks passed" : "Checks completed",
125 count,
126 icon: "checkmark.circle",
127 tint: "accent-adaptive-emerald-600-400",
128 } as const;
129 return {
130 label: pr.checks.length ? `${pr.checks.length} completed` : "No checks reported",
131 title: pr.checks.length ? "Checks completed" : "No checks reported",
132 count,
133 icon: "minus",
134 tint: "accent-icon-muted",
135 } as const;
136}
137
138export function pullRequestReviewSummary(
139 pr: Pick<PullRequestDetail, "reviewers">,
140 activity: Pick<PullRequestActivity, "comments" | "commits"> | null,
141) {
142 const latest = new Map<string, PullRequestActivity["comments"][number]>();
143 const comments = new Map<string, PullRequestActivity["comments"][number]>();
144 for (const review of activity?.comments ?? []) {
145 if (review.kind !== "review" || !review.reviewState) continue;
146 const state = review.reviewState.toUpperCase().replaceAll("-", "_");
147 const key = review.author?.login ?? review.id;
148 if (state === "COMMENTED") {
149 const previous = comments.get(key);
150 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
151 comments.set(key, { ...review, reviewState: state });
152 continue;
153 }
154 if (!["APPROVED", "CHANGES_REQUESTED", "DISMISSED"].includes(state)) continue;
155 const previous = latest.get(key);
156 if (!previous || Date.parse(review.createdAt) >= Date.parse(previous.createdAt))
157 latest.set(key, { ...review, reviewState: state });
158 }
159 const reviews = [
160 ...[...latest.values()].filter((review) => review.reviewState !== "DISMISSED"),
161 ...[...comments.entries()].filter(([key]) => !latest.has(key)).map(([, review]) => review),
162 ];
163 const requested = reviews.filter((review) => review.reviewState === "CHANGES_REQUESTED");
164 const newestCommit = Math.max(
165 0,
166 ...(activity?.commits.map((commit) => Date.parse(commit.committedDate)) ?? []),
167 );
168 const approved = reviews.filter(
169 (review) => review.reviewState === "APPROVED" && Date.parse(review.createdAt) >= newestCommit,
170 );
171 if (requested.length)
172 return {
173 title: "Changes requested",
174 count: `${requested.length} ${requested.length === 1 ? "reviewer" : "reviewers"}`,
175 icon: "xmark.circle.fill",
176 tint: "accent-danger-foreground",
177 reviews,
178 } as const;
179 if (approved.length)
180 return {
181 title:
182 approved.length === 1 && approved[0]?.author
183 ? `Approved by ${approved[0].author.login}`
184 : "Reviews approved",
185 count: `${approved.length} approved`,
186 icon: "checkmark.circle",
187 tint: "accent-adaptive-emerald-600-400",
188 reviews,
189 } as const;
190 const commented = reviews.filter((review) => review.reviewState === "COMMENTED");
191 if (commented.length && !pr.reviewers.length)
192 return {
193 title: "Reviews",
194 count: `${commented.length} commented`,
195 icon: "bubble.left",
196 tint: "accent-icon-muted",
197 reviews,
198 } as const;
199 return {
200 title: pr.reviewers.length ? "Awaiting review" : "Request a review",
201 count: pr.reviewers.length ? `${pr.reviewers.length} requested` : "None assigned",
202 icon: "person.2",
203 tint: "accent-icon-muted",
204 reviews,
205 } as const;
206}
207
208export function pullRequestBranchSummary(
209 pr: Pick<PullRequestDetail, "mergeability" | "baseComparison" | "behindBy" | "baseBranch">,
210) {
211 if (pr.mergeability === "conflicting") return "Has conflicts";
212 if (pr.baseComparison === "behind") return `${pr.behindBy ?? ""} behind ${pr.baseBranch}`.trim();
213 if (pr.baseComparison === "up-to-date") return "Up to date";
214 return pr.mergeability === "mergeable" ? "No conflicts" : "Not reported";
215}
216
217export function pullRequestChecksStatus(state: PullRequestChecksState) {
218 if (state === "passing")
219 return { icon: "checkmark.circle", tint: "accent-adaptive-emerald-600-400" } as const;
220 if (state === "failing")
221 return { icon: "xmark.circle.fill", tint: "accent-danger-foreground" } as const;
222 return { icon: "clock", tint: "accent-warning-foreground" } as const;
223}