| ... | ... | @@ -7,6 +7,7 @@ import { |
| 7 | 7 | gitRunStackedActionInputSchema, |
| 8 | 8 | gitRunStackedActionResultSchema, |
| 9 | 9 | gitStatusInputSchema, |
| 10 | type GitStatusPrState, |
| 10 | 11 | type GitRunStackedActionInput, |
| 11 | 12 | type GitRunStackedActionResult, |
| 12 | 13 | type GitStatusInput, |
| ... | ... | @@ -34,18 +35,20 @@ interface GitManagerDeps { |
| 34 | 35 | gitCore?: GitCoreService; |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | | interface OpenPrInfo { |
| 38 | interface PullRequestInfo { |
| 38 | 39 | number: number; |
| 39 | 40 | title: string; |
| 40 | 41 | url: string; |
| 41 | 42 | baseRefName: string; |
| 42 | 43 | headRefName: string; |
| 44 | state: GitStatusPrState; |
| 45 | updatedAt: string | null; |
| 43 | 46 | } |
| 44 | 47 | |
| 45 | | function parseOpenPrList(raw: unknown): OpenPrInfo[] { |
| 48 | function parsePullRequestList(raw: unknown): PullRequestInfo[] { |
| 46 | 49 | if (!Array.isArray(raw)) return []; |
| 47 | 50 | |
| 48 | | const parsed: OpenPrInfo[] = []; |
| 51 | const parsed: PullRequestInfo[] = []; |
| 49 | 52 | for (const entry of raw) { |
| 50 | 53 | if (!entry || typeof entry !== "object") continue; |
| 51 | 54 | const record = entry as Record<string, unknown>; |
| ... | ... | @@ -54,6 +57,9 @@ function parseOpenPrList(raw: unknown): OpenPrInfo[] { |
| 54 | 57 | const url = record.url; |
| 55 | 58 | const baseRefName = record.baseRefName; |
| 56 | 59 | const headRefName = record.headRefName; |
| 60 | const state = record.state; |
| 61 | const mergedAt = record.mergedAt; |
| 62 | const updatedAt = record.updatedAt; |
| 57 | 63 | if (typeof number !== "number" || !Number.isInteger(number) || number <= 0) { |
| 58 | 64 | continue; |
| 59 | 65 | } |
| ... | ... | @@ -61,16 +67,34 @@ function parseOpenPrList(raw: unknown): OpenPrInfo[] { |
| 61 | 67 | typeof title !== "string" || |
| 62 | 68 | typeof url !== "string" || |
| 63 | 69 | typeof baseRefName !== "string" || |
| 64 | | typeof headRefName !== "string" |
| 70 | typeof headRefName !== "string" || |
| 71 | typeof state !== "string" |
| 65 | 72 | ) { |
| 66 | 73 | continue; |
| 67 | 74 | } |
| 75 | |
| 76 | let normalizedState: GitStatusPrState; |
| 77 | if ((typeof mergedAt === "string" && mergedAt.trim().length > 0) || state === "MERGED") { |
| 78 | normalizedState = "merged"; |
| 79 | } else if (state === "OPEN") { |
| 80 | normalizedState = "open"; |
| 81 | } else if (state === "CLOSED") { |
| 82 | normalizedState = "closed"; |
| 83 | } else { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | const normalizedUpdatedAt = |
| 88 | typeof updatedAt === "string" && updatedAt.trim().length > 0 ? updatedAt : null; |
| 89 | |
| 68 | 90 | parsed.push({ |
| 69 | 91 | number, |
| 70 | 92 | title, |
| 71 | 93 | url, |
| 72 | 94 | baseRefName, |
| 73 | 95 | headRefName, |
| 96 | state: normalizedState, |
| 97 | updatedAt: normalizedUpdatedAt, |
| 74 | 98 | }); |
| 75 | 99 | } |
| 76 | 100 | return parsed; |
| ... | ... | @@ -160,12 +184,13 @@ function normalizeGitHubAuthError(error: unknown): Error | undefined { |
| 160 | 184 | return undefined; |
| 161 | 185 | } |
| 162 | 186 | |
| 163 | | function toStatusOpenPr(pr: OpenPrInfo): { |
| 187 | function toStatusPr(pr: PullRequestInfo): { |
| 164 | 188 | number: number; |
| 165 | 189 | title: string; |
| 166 | 190 | url: string; |
| 167 | 191 | baseBranch: string; |
| 168 | 192 | headBranch: string; |
| 193 | state: GitStatusPrState; |
| 169 | 194 | } { |
| 170 | 195 | return { |
| 171 | 196 | number: pr.number, |
| ... | ... | @@ -173,6 +198,7 @@ function toStatusOpenPr(pr: OpenPrInfo): { |
| 173 | 198 | url: pr.url, |
| 174 | 199 | baseBranch: pr.baseRefName, |
| 175 | 200 | headBranch: pr.headRefName, |
| 201 | state: pr.state, |
| 176 | 202 | }; |
| 177 | 203 | } |
| 178 | 204 | |
| ... | ... | @@ -191,21 +217,12 @@ export class GitManager { |
| 191 | 217 | const input = gitStatusInputSchema.parse(raw); |
| 192 | 218 | const details = await this.gitCore.statusDetails(input.cwd); |
| 193 | 219 | |
| 194 | | let openPr: ReturnType<typeof toStatusOpenPr> | null = null; |
| 195 | | let mergedPr: ReturnType<typeof toStatusOpenPr> | null = null; |
| 220 | let pr: ReturnType<typeof toStatusPr> | null = null; |
| 196 | 221 | if (details.branch) { |
| 197 | 222 | try { |
| 198 | | if (details.hasUpstream) { |
| 199 | | const existing = await this.findOpenPr(input.cwd, details.branch); |
| 200 | | if (existing) { |
| 201 | | openPr = toStatusOpenPr(existing); |
| 202 | | } |
| 203 | | } |
| 204 | | if (!openPr) { |
| 205 | | const merged = await this.findMergedPr(input.cwd, details.branch); |
| 206 | | if (merged) { |
| 207 | | mergedPr = toStatusOpenPr(merged); |
| 208 | | } |
| 223 | const existing = await this.findLatestPr(input.cwd, details.branch); |
| 224 | if (existing) { |
| 225 | pr = toStatusPr(existing); |
| 209 | 226 | } |
| 210 | 227 | } catch { |
| 211 | 228 | // PR lookup is best-effort for status rendering. |
| ... | ... | @@ -219,8 +236,7 @@ export class GitManager { |
| 219 | 236 | hasUpstream: details.hasUpstream, |
| 220 | 237 | aheadCount: details.aheadCount, |
| 221 | 238 | behindCount: details.behindCount, |
| 222 | | openPr, |
| 223 | | mergedPr, |
| 239 | pr, |
| 224 | 240 | }; |
| 225 | 241 | } |
| 226 | 242 | |
| ... | ... | @@ -390,7 +406,7 @@ export class GitManager { |
| 390 | 406 | }; |
| 391 | 407 | } |
| 392 | 408 | |
| 393 | | private async findOpenPr(cwd: string, branch: string): Promise<OpenPrInfo | null> { |
| 409 | private async findOpenPr(cwd: string, branch: string): Promise<PullRequestInfo | null> { |
| 394 | 410 | const stdout = await this.runGhStdout(cwd, [ |
| 395 | 411 | "pr", |
| 396 | 412 | "list", |
| ... | ... | @@ -401,7 +417,7 @@ export class GitManager { |
| 401 | 417 | "--limit", |
| 402 | 418 | "1", |
| 403 | 419 | "--json", |
| 404 | | "number,title,url,baseRefName,headRefName", |
| 420 | "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt", |
| 405 | 421 | ]); |
| 406 | 422 | |
| 407 | 423 | const raw = trimStdout(stdout); |
| ... | ... | @@ -414,22 +430,22 @@ export class GitManager { |
| 414 | 430 | throw new Error("GitHub CLI returned invalid PR list JSON."); |
| 415 | 431 | } |
| 416 | 432 | |
| 417 | | const parsed = parseOpenPrList(parsedJson); |
| 433 | const parsed = parsePullRequestList(parsedJson); |
| 418 | 434 | return parsed[0] ?? null; |
| 419 | 435 | } |
| 420 | 436 | |
| 421 | | private async findMergedPr(cwd: string, branch: string): Promise<OpenPrInfo | null> { |
| 437 | private async findLatestPr(cwd: string, branch: string): Promise<PullRequestInfo | null> { |
| 422 | 438 | const stdout = await this.runGhStdout(cwd, [ |
| 423 | 439 | "pr", |
| 424 | 440 | "list", |
| 425 | 441 | "--head", |
| 426 | 442 | branch, |
| 427 | 443 | "--state", |
| 428 | | "merged", |
| 444 | "all", |
| 429 | 445 | "--limit", |
| 430 | | "1", |
| 446 | "20", |
| 431 | 447 | "--json", |
| 432 | | "number,title,url,baseRefName,headRefName", |
| 448 | "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt", |
| 433 | 449 | ]); |
| 434 | 450 | |
| 435 | 451 | const raw = trimStdout(stdout); |
| ... | ... | @@ -442,7 +458,11 @@ export class GitManager { |
| 442 | 458 | throw new Error("GitHub CLI returned invalid PR list JSON."); |
| 443 | 459 | } |
| 444 | 460 | |
| 445 | | const parsed = parseOpenPrList(parsedJson); |
| 461 | const parsed = parsePullRequestList(parsedJson).toSorted((a, b) => { |
| 462 | const left = a.updatedAt ? Date.parse(a.updatedAt) : 0; |
| 463 | const right = b.updatedAt ? Date.parse(b.updatedAt) : 0; |
| 464 | return right - left; |
| 465 | }); |
| 446 | 466 | return parsed[0] ?? null; |
| 447 | 467 | } |
| 448 | 468 | |