fix(github): verify cached integration PRs after tracking is removed

Use the existing REST PR mapping to read the head repository's default
branch when no tracked remote or origin can identify it. Require matching
repository and head metadata, and preserve the PR if the lookup fails.

This clears both closed and merged integration PRs cached by a fork
checkout after its upstream tracking is removed.

Co-authored-by: petersindex <287557875+petersindex@users.noreply.github.com>
This commit is contained in:
Neil
2026-10-10 17:08:10 -07:00
committed by Neil
co-authored by petersindex
parent 82c5484f66
commit 884575a797
3 changed files with 116 additions and 4 deletions
@@ -87,7 +87,8 @@ export async function resolvePRForBranchOutcome(input: {
linkedPRNumber,
repoPath,
connectionId,
localGitOptions
localGitOptions,
ghOptions
}
const implicitPRPolicy = createImplicitDefaultBranchPRPolicy(defaultBranchPRContext)
@@ -4,13 +4,14 @@ import {
getRemoteHeadBranchName,
getRepoDefaultBranchName
} from '../../../source-control/repo-default-branch'
import type { HostedReviewLocalGitOptions } from './../github-exec-scope'
import type { GhExecOptions, HostedReviewLocalGitOptions } from './../github-exec-scope'
import type { TrackedUpstreamBranch } from './tracked-upstream-cache'
import {
pullRequestHeadRepository,
type PullRequestLookupData,
type PullRequestLookupPolicy
} from './pull-request-lookup-data'
import { getRestPRByNumber } from './pr-number-lookup'
export type ImplicitDefaultBranchPRContext = {
originHeadRepo: OwnerRepo | null
@@ -20,6 +21,7 @@ export type ImplicitDefaultBranchPRContext = {
repoPath: string
connectionId?: string | null
localGitOptions: HostedReviewLocalGitOptions
ghOptions: GhExecOptions
}
export function createImplicitDefaultBranchPRPolicy(
@@ -73,7 +75,19 @@ async function shouldHideImplicitDefaultBranchPR(
? 'origin'
: null
if (!remoteName) {
return false
try {
// Removed tracking can leave a cached integration PR without a matching remote.
const metadata = await getRestPRByNumber(prRepo, data.number, input.ghOptions)
const metadataHeadRepo = pullRequestHeadRepository(metadata, prRepo)
return (
metadataHeadRepo !== null &&
githubRepoIdentityKey(metadataHeadRepo) === headRepoKey &&
metadata.headRefName === data.headRefName &&
metadata.headDefaultBranchName === data.headRefName
)
} catch {
return false
}
}
// An unknown upstream HEAD cannot borrow a different repository's origin default.
const defaultBranchName =