Fix fork PR detection for same-name upstreams (#6446)

Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
Brennan Benson
2026-06-26 14:49:19 -07:00
committed by GitHub
co-authored by Neil
parent cdda9fd16d
commit 85d626b998
2 changed files with 184 additions and 27 deletions
+142
View File
@@ -1736,6 +1736,97 @@ describe('getPRForBranch', () => {
})
})
it('uses the tracked upstream remote owner when the fork branch name matches locally', async () => {
resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({
candidates: [
{ owner: 'stablyai', repo: 'orca' },
{ owner: 'origin-owner', repo: 'orca' }
],
headRepo: { owner: 'origin-owner', repo: 'orca' }
})
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'brennanb2025', repo: 'orca' })
ghExecFileAsyncMock
.mockResolvedValueOnce({ stdout: JSON.stringify([]) })
.mockResolvedValueOnce({ stdout: JSON.stringify([]) })
.mockResolvedValueOnce({
stdout: JSON.stringify([
{
number: 6433,
title: 'Recover Windows worktree deletes from long paths',
state: 'open',
html_url: 'https://github.com/stablyai/orca/pull/6433',
updated_at: '2026-06-26T00:00:00Z',
draft: false,
mergeable: true,
base: { ref: 'main', sha: 'base-oid' },
head: {
ref: 'brennanb2025/worktree-remove-fix',
sha: 'same-name-fork-head-oid'
}
}
])
})
.mockResolvedValueOnce({
stdout: JSON.stringify({
number: 6433,
title: 'Recover Windows worktree deletes from long paths',
state: 'OPEN',
url: 'https://github.com/stablyai/orca/pull/6433',
statusCheckRollup: [],
updatedAt: '2026-06-26T00:00:00Z',
isDraft: false,
mergeable: 'MERGEABLE',
baseRefName: 'main',
headRefName: 'brennanb2025/worktree-remove-fix',
baseRefOid: 'base-oid',
headRefOid: 'same-name-fork-head-oid'
})
})
gitExecFileAsyncMock.mockResolvedValueOnce({
stdout: 'brennanb2025/worktree-remove-fix\0brennan/brennanb2025/worktree-remove-fix\n',
stderr: ''
})
const pr = await getPRForBranch('/repo-root', 'brennanb2025/worktree-remove-fix')
expect(getOwnerRepoForRemoteMock).toHaveBeenCalledWith('/repo-root', 'brennan', undefined)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
[
'api',
'repos/stablyai/orca/pulls?head=brennanb2025%3Abrennanb2025%2Fworktree-remove-fix&state=all&per_page=1'
],
{ cwd: '/repo-root' }
)
expect(pr).toMatchObject({
number: 6433,
prRepo: { owner: 'stablyai', repo: 'orca' },
headRepo: { owner: 'brennanb2025', repo: 'orca' }
})
})
it('does not retry same-name tracked upstream lookup for the same head repo', async () => {
resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({
candidates: [{ owner: 'acme', repo: 'widgets' }],
headRepo: { owner: 'acme', repo: 'widgets' }
})
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
ghExecFileAsyncMock.mockResolvedValueOnce({ stdout: JSON.stringify([]) })
gitExecFileAsyncMock.mockResolvedValueOnce({
stdout: 'feature/no-pr\0origin/feature/no-pr\n',
stderr: ''
})
await expect(getPRForBranch('/repo-root', 'feature/no-pr')).resolves.toBeNull()
expect(getOwnerRepoForRemoteMock).toHaveBeenCalledWith('/repo-root', 'origin', undefined)
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(1)
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['api', 'repos/acme/widgets/pulls?head=acme%3Afeature%2Fno-pr&state=all&per_page=1'],
{ cwd: '/repo-root' }
)
})
it('checks the tracked upstream branch through the SSH git provider', async () => {
const sshGitProvider = {
exec: vi.fn().mockResolvedValue({
@@ -1784,6 +1875,57 @@ describe('getPRForBranch', () => {
expect(pr).toMatchObject({ number: 78, title: 'SSH upstream branch PR' })
})
it('uses the same-name tracked upstream fork owner through the SSH git provider', async () => {
const sshGitProvider = {
exec: vi.fn().mockResolvedValue({
stdout: 'contributor/fix\0fork/contributor/fix\n',
stderr: ''
})
}
getSshGitProviderMock.mockReturnValue(sshGitProvider)
resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({
candidates: [
{ owner: 'stablyai', repo: 'orca' },
{ owner: 'origin-owner', repo: 'orca' }
],
headRepo: { owner: 'origin-owner', repo: 'orca' }
})
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'fork-owner', repo: 'orca' })
ghExecFileAsyncMock
.mockResolvedValueOnce({ stdout: JSON.stringify([]) })
.mockResolvedValueOnce({ stdout: JSON.stringify([]) })
.mockResolvedValueOnce({
stdout: JSON.stringify([
{
number: 79,
title: 'SSH same-name fork PR',
state: 'open',
html_url: 'https://github.com/stablyai/orca/pull/79',
updated_at: '2026-03-28T00:00:00Z',
draft: false,
mergeable: true,
base: { ref: 'main', sha: 'base-oid' },
head: { ref: 'contributor/fix', sha: 'same-name-ssh-head-oid' }
}
])
})
const pr = await getPRForBranch('/remote/repo-root', 'contributor/fix', undefined, 'ssh-1')
expect(getOwnerRepoForRemoteMock).toHaveBeenCalledWith('/remote/repo-root', 'fork', 'ssh-1')
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['api', 'repos/stablyai/orca/pulls?head=fork-owner%3Acontributor%2Ffix&state=all&per_page=1'],
{}
)
expect(pr).toMatchObject({
number: 79,
title: 'SSH same-name fork PR',
prRepo: { owner: 'stablyai', repo: 'orca' },
headRepo: { owner: 'fork-owner', repo: 'orca' }
})
})
it('caches positive tracked-upstream entries for unsigned SSH runtimes during PR refresh polling', async () => {
const sshGitProvider = {
exec: vi.fn().mockResolvedValue({
+42 -27
View File
@@ -2314,17 +2314,33 @@ export function __resetTrackedUpstreamBranchCacheForTests(): void {
trackedUpstreamSnapshotGenerations.clear()
}
function parseTrackedUpstreamBranch(
upstreamRef: string,
branchName: string
): TrackedUpstreamBranch | null {
function parseTrackedUpstreamBranch(upstreamRef: string): TrackedUpstreamBranch | null {
const parsed = splitRemoteBranchName(upstreamRef.trim())
if (!parsed || parsed.branchName === branchName) {
if (!parsed) {
return null
}
return parsed
}
function prOwnerRepoKey(ownerRepo: OwnerRepo): string {
return `${ownerRepo.owner.toLowerCase()}/${ownerRepo.repo.toLowerCase()}`
}
function shouldRetryTrackedUpstreamBranch(
upstreamBranch: TrackedUpstreamBranch,
branchName: string,
upstreamHeadRepo: OwnerRepo,
headRepo: OwnerRepo | null
): boolean {
if (upstreamBranch.branchName !== branchName) {
return true
}
if (!headRepo) {
return true
}
return prOwnerRepoKey(upstreamHeadRepo) !== prOwnerRepoKey(headRepo)
}
async function getTrackedUpstreamBranch(
repoPath: string,
branchName: string,
@@ -2513,27 +2529,21 @@ function parseTrackedUpstreamBranches(stdout: string): Map<string, TrackedUpstre
if (!localBranchName) {
continue
}
upstreamsByBranchName.set(
localBranchName,
parseTrackedUpstreamRef(upstreamRef ?? '', localBranchName)
)
upstreamsByBranchName.set(localBranchName, parseTrackedUpstreamRef(upstreamRef ?? ''))
}
return upstreamsByBranchName
}
function parseTrackedUpstreamRef(
upstreamRef: string,
branchName: string
): TrackedUpstreamBranch | null {
function parseTrackedUpstreamRef(upstreamRef: string): TrackedUpstreamBranch | null {
const remoteRefPrefix = 'refs/remotes/'
const normalizedRef = upstreamRef.trim()
if (normalizedRef.startsWith(remoteRefPrefix)) {
return parseTrackedUpstreamBranch(normalizedRef.slice(remoteRefPrefix.length), branchName)
return parseTrackedUpstreamBranch(normalizedRef.slice(remoteRefPrefix.length))
}
if (normalizedRef.startsWith('refs/heads/')) {
return null
}
return parseTrackedUpstreamBranch(normalizedRef, branchName)
return parseTrackedUpstreamBranch(normalizedRef)
}
async function lookupPRByBranchName(args: {
@@ -2799,8 +2809,8 @@ export async function getPRForBranchOutcome(
data = branchLookup.data
dataRepo = branchLookup.dataRepo
if (!data) {
// Why: worktrees can have a short local branch tracking a differently
// named remote PR head; after the local miss, try that configured head.
// Why: the tracked upstream can identify the real PR head by branch
// name or by fork owner even when branch names match locally.
const upstreamBranch = await getTrackedUpstreamBranch(
repoPath,
branchName,
@@ -2815,16 +2825,21 @@ export async function getPRForBranchOutcome(
connectionId,
...localGitArgs
)) ?? headRepo
const upstreamLookup = await lookupPRByBranchName({
candidates,
headRepo: upstreamHeadRepo,
branchName: upstreamBranch.branchName,
ghOptions
})
data = upstreamLookup.data
dataRepo = upstreamLookup.dataRepo
if (data) {
dataHeadRepo = upstreamHeadRepo
if (
upstreamHeadRepo &&
shouldRetryTrackedUpstreamBranch(upstreamBranch, branchName, upstreamHeadRepo, headRepo)
) {
const upstreamLookup = await lookupPRByBranchName({
candidates,
headRepo: upstreamHeadRepo,
branchName: upstreamBranch.branchName,
ghOptions
})
data = upstreamLookup.data
dataRepo = upstreamLookup.dataRepo
if (data) {
dataHeadRepo = upstreamHeadRepo
}
}
}
}