CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
pr-size.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 74d8c4d | 1 | /** |
| 2 | * PR size analysis — computes lines-changed from a git diff and maps to | |
| 3 | * an XS/S/M/L/XL label with a matching colour. Zero DB tables; pure git | |
| 4 | * subprocess + arithmetic. | |
| 5 | */ | |
| 6 | ||
| 7 | import { join } from "path"; | |
| 8 | ||
| 9 | export type PrSizeLabel = "XS" | "S" | "M" | "L" | "XL"; | |
| 10 | ||
| 11 | export interface PrSizeInfo { | |
| 12 | label: PrSizeLabel; | |
| 13 | linesChanged: number; | |
| 14 | added: number; | |
| 15 | deleted: number; | |
| 16 | color: string; | |
| 17 | bgColor: string; | |
| 18 | } | |
| 19 | ||
| 20 | const SIZE_COLORS: Record<PrSizeLabel, { fg: string; bg: string }> = { | |
| 21 | XS: { fg: "#34d399", bg: "rgba(52,211,153,0.12)" }, | |
| 22 | S: { fg: "#60a5fa", bg: "rgba(96,165,250,0.12)" }, | |
| 23 | M: { fg: "#facc15", bg: "rgba(250,204,21,0.12)" }, | |
| 24 | L: { fg: "#f97316", bg: "rgba(249,115,22,0.12)" }, | |
| 25 | XL: { fg: "#f87171", bg: "rgba(248,113,113,0.12)" }, | |
| 26 | }; | |
| 27 | ||
| 28 | export function computeSizeLabel(linesChanged: number): PrSizeLabel { | |
| 29 | if (linesChanged < 10) return "XS"; | |
| 30 | if (linesChanged < 50) return "S"; | |
| 31 | if (linesChanged < 200) return "M"; | |
| 32 | if (linesChanged < 500) return "L"; | |
| 33 | return "XL"; | |
| 34 | } | |
| 35 | ||
| 36 | export async function computePrSize( | |
| 37 | ownerName: string, | |
| 38 | repoName: string, | |
| 39 | baseBranch: string, | |
| 40 | headBranch: string | |
| 41 | ): Promise<PrSizeInfo | null> { | |
| 42 | try { | |
| 43 | const repoBase = process.env.GIT_REPOS_PATH || "./repos"; | |
| 44 | const diskPath = join(repoBase, `${ownerName}/${repoName}.git`); | |
| 45 | ||
| 46 | const proc = Bun.spawn( | |
| 47 | ["git", "--git-dir", diskPath, "diff", "--numstat", `${baseBranch}...${headBranch}`], | |
| 48 | { stdout: "pipe", stderr: "pipe" } | |
| 49 | ); | |
| 50 | const out = await new Response(proc.stdout).text(); | |
| 51 | await proc.exited; | |
| 52 | ||
| 53 | let added = 0; | |
| 54 | let deleted = 0; | |
| 55 | for (const line of out.trim().split("\n")) { | |
| 56 | if (!line) continue; | |
| 57 | const parts = line.split("\t"); | |
| 58 | if (parts.length >= 2) { | |
| 59 | const a = parseInt(parts[0], 10); | |
| 60 | const d = parseInt(parts[1], 10); | |
| 61 | if (!isNaN(a)) added += a; | |
| 62 | if (!isNaN(d)) deleted += d; | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | const linesChanged = added + deleted; | |
| 67 | const label = computeSizeLabel(linesChanged); | |
| 68 | const { fg, bg } = SIZE_COLORS[label]; | |
| 69 | return { label, linesChanged, added, deleted, color: fg, bgColor: bg }; | |
| 70 | } catch { | |
| 71 | return null; | |
| 72 | } | |
| 73 | } |