The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | PullRequestActivity, |
| 3 | PullRequestCheck, |
| 4 | PullRequestChecksState, |
| 5 | PullRequestDetail, |
| 6 | PullRequestListEntry, |
| 7 | PullRequestMergeability, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { msg } from "gt-react-native"; |
| 10 | |
| 11 | import type { AppSymbolName } from "../../components/ui/AppSymbol"; |
| 12 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 13 | import { PullRequestGlyph } from "./pullRequestGlyphs"; |
| 14 | |
| 15 | const 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 | |
| 49 | export 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 | |
| 57 | export 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 | |
| 74 | export 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 | |
| 121 | export function isFailingCheck(check: PullRequestCheck) { |
| 122 | return check.status === "failure" || check.status === "cancelled"; |
| 123 | } |
| 124 | |
| 125 | export 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. |
| 202 | export function normalizedReviewState(reviewState: string | null) { |
| 203 | return reviewState?.toUpperCase().replaceAll("-", "_"); |
| 204 | } |
| 205 | |
| 206 | export 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 reviews = [ |
| 229 | ...[...latest.values()].filter((review) => review.reviewState !== "DISMISSED"), |
| 230 | ...[...comments.entries()].filter(([key]) => !latest.has(key)).map(([, review]) => review), |
| 231 | ]; |
| 232 | const requested = reviews.filter((review) => review.reviewState === "CHANGES_REQUESTED"); |
| 233 | const newestCommit = Math.max( |
| 234 | 0, |
| 235 | ...(activity?.commits.map((commit) => Date.parse(commit.committedDate)) ?? []), |
| 236 | ); |
| 237 | const approved = reviews.filter( |
| 238 | (review) => review.reviewState === "APPROVED" && Date.parse(review.createdAt) >= newestCommit, |
| 239 | ); |
| 240 | if (requested.length) |
| 241 | return { |
| 242 | title: resolve(msg("Changes requested")), |
| 243 | count: resolve( |
| 244 | msg("{count, plural, one {{count} reviewer} other {{count} reviewers}}", { |
| 245 | count: requested.length, |
| 246 | }), |
| 247 | ), |
| 248 | icon: "xmark.circle.fill", |
| 249 | tint: "accent-danger-foreground", |
| 250 | reviews, |
| 251 | } as const; |
| 252 | if (approved.length) |
| 253 | return { |
| 254 | title: |
| 255 | approved.length === 1 && approved[0]?.author |
| 256 | ? resolve(msg("Approved by {login}", { login: approved[0].author.login })) |
| 257 | : resolve(msg("Reviews approved")), |
| 258 | count: resolve( |
| 259 | msg("{count, plural, one {{count} approved} other {{count} approved}}", { |
| 260 | count: approved.length, |
| 261 | }), |
| 262 | ), |
| 263 | icon: "checkmark.circle", |
| 264 | tint: "accent-adaptive-emerald-600-400", |
| 265 | reviews, |
| 266 | } as const; |
| 267 | const commented = reviews.filter((review) => review.reviewState === "COMMENTED"); |
| 268 | if (commented.length && !pr.reviewers.length) |
| 269 | return { |
| 270 | title: resolve(msg("Reviews")), |
| 271 | count: resolve( |
| 272 | msg("{count, plural, one {{count} commented} other {{count} commented}}", { |
| 273 | count: commented.length, |
| 274 | }), |
| 275 | ), |
| 276 | icon: "bubble.left", |
| 277 | tint: "accent-icon-muted", |
| 278 | reviews, |
| 279 | } as const; |
| 280 | return { |
| 281 | title: pr.reviewers.length ? resolve(msg("Awaiting review")) : resolve(msg("Request a review")), |
| 282 | count: pr.reviewers.length |
| 283 | ? resolve( |
| 284 | msg("{count, plural, one {{count} requested} other {{count} requested}}", { |
| 285 | count: pr.reviewers.length, |
| 286 | }), |
| 287 | ) |
| 288 | : resolve(msg("None assigned")), |
| 289 | icon: "person.2", |
| 290 | tint: "accent-icon-muted", |
| 291 | reviews, |
| 292 | } as const; |
| 293 | } |
| 294 | |
| 295 | export function pullRequestBranchSummary( |
| 296 | pr: Pick<PullRequestDetail, "mergeability" | "baseComparison" | "behindBy" | "baseBranch">, |
| 297 | resolve: MessageResolver = m, |
| 298 | ) { |
| 299 | if (pr.mergeability === "conflicting") return resolve(msg("Has conflicts")); |
| 300 | if (pr.baseComparison === "behind") |
| 301 | return pr.behindBy == null |
| 302 | ? resolve(msg("behind {branch}", { branch: pr.baseBranch })) |
| 303 | : resolve( |
| 304 | msg("{count, plural, one {{count} behind {branch}} other {{count} behind {branch}}}", { |
| 305 | count: pr.behindBy, |
| 306 | branch: pr.baseBranch, |
| 307 | }), |
| 308 | ); |
| 309 | if (pr.baseComparison === "up-to-date") return resolve(msg("Up to date")); |
| 310 | return pr.mergeability === "mergeable" |
| 311 | ? resolve(msg("No conflicts")) |
| 312 | : resolve(msg("Not reported")); |
| 313 | } |
| 314 | |
| 315 | export function pullRequestChecksStatus(state: PullRequestChecksState) { |
| 316 | if (state === "passing") |
| 317 | return { icon: "checkmark.circle", tint: "accent-adaptive-emerald-600-400" } as const; |
| 318 | if (state === "failing") |
| 319 | return { icon: "xmark.circle.fill", tint: "accent-danger-foreground" } as const; |
| 320 | return { icon: "clock", tint: "accent-warning-foreground" } as const; |
| 321 | } |