(opts: GitCodePullCreateOptions)
| 249 | * Returns the PR web URL (`html_url`) on success. |
| 250 | */ |
| 251 | export async function gitcodePullCreate(opts: GitCodePullCreateOptions): Promise<string> { |
| 252 | const token = requireToken(); |
| 253 | |
| 254 | const body: Record<string, unknown> = { |
| 255 | title: opts.title, |
| 256 | head: opts.source, |
| 257 | base: opts.target, |
| 258 | }; |
| 259 | if (opts.description) body.body = opts.description; |
| 260 | |
| 261 | const resp = await fetch(`${gitcodeApiBase()}/repos/${opts.repo}/pulls`, { |
| 262 | method: 'POST', |
| 263 | headers: authHeaders(token), |
| 264 | body: JSON.stringify(body), |
| 265 | redirect: 'manual', |
| 266 | }); |
| 267 | if (!resp.ok) { |
| 268 | const errBody = await resp.text().catch(() => ''); |
| 269 | throw new Error(`Failed to create GitCode PR: ${resp.status} ${errBody}`); |
| 270 | } |
| 271 | |
| 272 | const pr = (await resp.json()) as { html_url?: string; number?: number }; |
| 273 | if (pr.html_url) return pr.html_url; |
| 274 | // Fallback when html_url is absent: GitCode's PR web path uses `/pull/` |
| 275 | // (singular), verified against the live API — not `/pulls/`. |
| 276 | if (pr.number) return `https://${GITCODE_HOST}/${opts.repo}/pull/${pr.number}`; |
| 277 | throw new Error('GitCode PR created but response did not include html_url.'); |
| 278 | } |
no test coverage detected