( worktreePath: string, branch: string, )
| 93 | } |
| 94 | |
| 95 | async function getPRForBranch( |
| 96 | worktreePath: string, |
| 97 | branch: string, |
| 98 | ): Promise<GitHubStatus["pr"]> { |
| 99 | try { |
| 100 | // Use execWithShellEnv to handle macOS GUI app PATH issues |
| 101 | const { stdout } = await execWithShellEnv( |
| 102 | "gh", |
| 103 | [ |
| 104 | "pr", |
| 105 | "view", |
| 106 | branch, |
| 107 | "--json", |
| 108 | "number,title,url,state,isDraft,mergedAt,additions,deletions,reviewDecision,statusCheckRollup,mergeable", |
| 109 | ], |
| 110 | { cwd: worktreePath }, |
| 111 | ); |
| 112 | const raw = JSON.parse(stdout); |
| 113 | const result = GHPRResponseSchema.safeParse(raw); |
| 114 | if (!result.success) { |
| 115 | console.error("[GitHub] PR schema validation failed:", result.error); |
| 116 | console.error("[GitHub] Raw data:", JSON.stringify(raw, null, 2)); |
| 117 | throw new Error("PR schema validation failed"); |
| 118 | } |
| 119 | const data = result.data; |
| 120 | |
| 121 | const checks = parseChecks(data.statusCheckRollup); |
| 122 | |
| 123 | return { |
| 124 | number: data.number, |
| 125 | title: data.title, |
| 126 | url: data.url, |
| 127 | state: mapPRState(data.state, data.isDraft), |
| 128 | mergedAt: data.mergedAt ? new Date(data.mergedAt).getTime() : undefined, |
| 129 | additions: data.additions, |
| 130 | deletions: data.deletions, |
| 131 | reviewDecision: mapReviewDecision(data.reviewDecision), |
| 132 | checksStatus: computeChecksStatus(data.statusCheckRollup), |
| 133 | checks, |
| 134 | mergeable: data.mergeable, |
| 135 | }; |
| 136 | } catch (error) { |
| 137 | // "no pull requests found" is not an error - just no PR |
| 138 | if ( |
| 139 | error instanceof Error && |
| 140 | error.message.includes("no pull requests found") |
| 141 | ) { |
| 142 | return null; |
| 143 | } |
| 144 | // Re-throw other errors to be caught by parent |
| 145 | throw error; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | function mapPRState( |
| 150 | state: GHPRResponse["state"], |
no test coverage detected