diff --git a/github/index.ts b/github/index.ts index 6bfa964623..4e3539a7d1 100644 --- a/github/index.ts +++ b/github/index.ts @@ -72,7 +72,7 @@ type GitHubPullRequest = { } headRepository: { nameWithOwner: string - } + } | null commits: { totalCount: number nodes: Array<{ @@ -163,8 +163,8 @@ try { // 3. Fork PR if (isPullRequest()) { const prData = await fetchPR() - // Local PR - if (prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner) { + // Local PR (headRepository is null when the fork has been deleted) + if (prData.headRepository?.nameWithOwner === prData.baseRepository.nameWithOwner) { await checkoutLocalBranch(prData) const dataPrompt = buildPromptDataForPR(prData) const response = await chat(`${userPrompt}\n\n${dataPrompt}`, promptFiles) @@ -698,6 +698,9 @@ async function checkoutForkBranch(pr: GitHubPullRequest) { const localBranch = generateBranchName("pr") const depth = Math.max(pr.commits.totalCount, 20) + if (!pr.headRepository?.nameWithOwner) { + throw new Error("Cannot checkout fork branch: headRepository is not available (the fork may have been deleted)") + } await $`git remote add fork https://github.com/${pr.headRepository.nameWithOwner}.git` await $`git fetch fork --depth=${depth} ${remoteBranch}` await $`git checkout -b ${localBranch} fork/${remoteBranch}` diff --git a/packages/opencode/src/cli/cmd/github.ts b/packages/opencode/src/cli/cmd/github.ts index e8f3e6a11e..70303c8404 100644 --- a/packages/opencode/src/cli/cmd/github.ts +++ b/packages/opencode/src/cli/cmd/github.ts @@ -94,7 +94,7 @@ type GitHubPullRequest = { } headRepository: { nameWithOwner: string - } + } | null commits: { totalCount: number nodes: Array<{ @@ -606,8 +606,8 @@ export const GithubRunCommand = cmd({ issueEvent?.issue.pull_request ) { const prData = await fetchPR() - // Local PR - if (prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner) { + // Local PR (headRepository is null when the fork has been deleted) + if (prData.headRepository?.nameWithOwner === prData.baseRepository.nameWithOwner) { await checkoutLocalBranch(prData) const head = await gitText(["rev-parse", "HEAD"]) const dataPrompt = buildPromptDataForPR(prData) @@ -1113,6 +1113,9 @@ export const GithubRunCommand = cmd({ const localBranch = generateBranchName("pr") const depth = Math.max(pr.commits.totalCount, 20) + if (!pr.headRepository?.nameWithOwner) { + throw new Error("Cannot checkout fork branch: headRepository is not available (the fork may have been deleted)") + } await gitRun(["remote", "add", "fork", `https://github.com/${pr.headRepository.nameWithOwner}.git`]) await gitRun(["fetch", "fork", `--depth=${depth}`, remoteBranch]) await gitRun(["checkout", "-b", localBranch, `fork/${remoteBranch}`])