Fix start-from PR failures for fork-backed PRs (#2233)
* Fix start-from PR failures for some fork PRs * fix: limit PR fallback fetches to missing refs --------- Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
@@ -350,4 +350,47 @@ describe('listWorkItems', () => {
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('marks fork PRs as cross-repository when REST payload only includes head.label', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' })
|
||||
ghExecFileAsyncMock
|
||||
.mockResolvedValueOnce({ stdout: '[]' })
|
||||
.mockResolvedValueOnce({
|
||||
stdout: JSON.stringify([
|
||||
{
|
||||
number: 1849,
|
||||
title: 'Fork PR with missing head repo',
|
||||
state: 'open',
|
||||
html_url: 'https://github.com/stablyai/orca/pull/1849',
|
||||
updated_at: '2026-04-01T00:00:00Z',
|
||||
user: { login: 'contributor' },
|
||||
head: {
|
||||
ref: 'feat/onboarding-model-choice-782',
|
||||
repo: null,
|
||||
label: 'contributor:feat/onboarding-model-choice-782'
|
||||
},
|
||||
base: { ref: 'main' }
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
const { items } = await listWorkItems('/repo-root', 10)
|
||||
expect(items).toEqual([
|
||||
{
|
||||
id: 'pr:1849',
|
||||
type: 'pr',
|
||||
number: 1849,
|
||||
title: 'Fork PR with missing head repo',
|
||||
state: 'open',
|
||||
url: 'https://github.com/stablyai/orca/pull/1849',
|
||||
labels: [],
|
||||
updatedAt: '2026-04-01T00:00:00Z',
|
||||
author: 'contributor',
|
||||
branchName: 'feat/onboarding-model-choice-782',
|
||||
baseRefName: 'main',
|
||||
isCrossRepository: true
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -793,6 +793,48 @@ describe('getPRForBranch', () => {
|
||||
})
|
||||
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('probes additional PR repo candidates when the first lookup is not found', async () => {
|
||||
resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({
|
||||
candidates: [
|
||||
{ owner: 'fork', repo: 'orca' },
|
||||
{ owner: 'stablyai', repo: 'orca' }
|
||||
],
|
||||
headRepo: { owner: 'fork', repo: 'orca' }
|
||||
})
|
||||
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'fork', repo: 'orca' })
|
||||
ghExecFileAsyncMock
|
||||
.mockRejectedValueOnce(new Error('HTTP 404: Not Found'))
|
||||
.mockResolvedValueOnce({
|
||||
stdout: JSON.stringify({
|
||||
head: {
|
||||
ref: 'feature/test',
|
||||
repo: {
|
||||
full_name: 'fork/orca',
|
||||
name: 'orca',
|
||||
clone_url: 'https://github.com/fork/orca.git',
|
||||
ssh_url: 'git@github.com:fork/orca.git',
|
||||
owner: { login: 'fork' }
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
await expect(getPullRequestPushTarget('/repo-root', 1849)).resolves.toEqual({
|
||||
remoteName: 'origin',
|
||||
branchName: 'feature/test'
|
||||
})
|
||||
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
['api', 'repos/fork/orca/pulls/1849'],
|
||||
{ cwd: '/repo-root' }
|
||||
)
|
||||
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
['api', 'repos/stablyai/orca/pulls/1849'],
|
||||
{ cwd: '/repo-root' }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GitHub GraphQL rate-limit guard', () => {
|
||||
|
||||
@@ -119,19 +119,38 @@ export async function getPullRequestPushTarget(
|
||||
): Promise<GitPushTarget | null> {
|
||||
const context = githubRepoContext(repoPath, connectionId)
|
||||
const ghOptions = ghRepoExecOptions(context)
|
||||
const ownerRepo = await getOwnerRepo(repoPath, connectionId)
|
||||
if (!ownerRepo) {
|
||||
const { candidates } = await resolvePRRepositoryCandidates(repoPath, connectionId)
|
||||
if (candidates.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
await acquire()
|
||||
try {
|
||||
const [{ stdout: prStdout }, origin] = await Promise.all([
|
||||
ghExecFileAsync(['api', `repos/${ownerRepo.owner}/${ownerRepo.repo}/pulls/${prNumber}`], {
|
||||
...ghOptions
|
||||
}),
|
||||
getOwnerRepoForRemote(repoPath, 'origin', connectionId)
|
||||
])
|
||||
let prStdout = ''
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
const { stdout } = await ghExecFileAsync(
|
||||
['api', `repos/${candidate.owner}/${candidate.repo}/pulls/${prNumber}`],
|
||||
{
|
||||
...ghOptions
|
||||
}
|
||||
)
|
||||
prStdout = stdout
|
||||
break
|
||||
} catch (error) {
|
||||
// Why: in fork workflows `origin` is often the contributor fork while
|
||||
// the PR number belongs to `upstream`; probe all known PR repos before
|
||||
// deciding this PR number is unavailable.
|
||||
if (isNotFoundGhError(error)) {
|
||||
continue
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
if (!prStdout) {
|
||||
return null
|
||||
}
|
||||
const origin = await getOwnerRepoForRemote(repoPath, 'origin', connectionId)
|
||||
const pr = JSON.parse(prStdout) as {
|
||||
head?: {
|
||||
ref?: string
|
||||
@@ -275,7 +294,8 @@ function extractHeadOwnerLogin(item: Record<string, unknown>): string | null {
|
||||
}
|
||||
// REST API `pull_request` shape: head.repo.owner.login
|
||||
if (typeof item.head === 'object' && item.head !== null) {
|
||||
const repo = (item.head as { repo?: unknown }).repo
|
||||
const head = item.head as { repo?: unknown; user?: unknown; label?: unknown }
|
||||
const repo = head.repo
|
||||
if (typeof repo === 'object' && repo !== null) {
|
||||
const owner = (repo as { owner?: unknown }).owner
|
||||
if (typeof owner === 'object' && owner !== null) {
|
||||
@@ -285,6 +305,21 @@ function extractHeadOwnerLogin(item: Record<string, unknown>): string | null {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Why: when a fork is deleted or made inaccessible, GitHub can return
|
||||
// `head.repo = null` but still include `head.user`/`head.label`.
|
||||
const user = head.user
|
||||
if (typeof user === 'object' && user !== null) {
|
||||
const login = (user as { login?: unknown }).login
|
||||
if (typeof login === 'string' && login.trim()) {
|
||||
return login
|
||||
}
|
||||
}
|
||||
if (typeof head.label === 'string') {
|
||||
const owner = head.label.split(':', 1)[0]?.trim()
|
||||
if (owner) {
|
||||
return owner
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user