From 9879f11ce4b5d7e7b80edebc7424ca2cb7c258e7 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Thu, 16 Jul 2026 17:59:31 -0700 Subject: [PATCH] Resolve typed PR lookups across hosted repository candidates getWorkItem's PR path probes upstream-then-origin hosted candidates instead of origin alone, so fork checkouts resolve the base repo's PR with the right host; issue detail resolution reuses the up-front hosted identity and keeps the SSH unresolved-host guards. Co-authored-by: Orca --- src/main/github/client-issue-source.test.ts | 95 +++++++++++++++++++-- src/main/github/client.ts | 53 ++++++++++-- src/main/github/work-item-details.test.ts | 68 +++++---------- src/main/github/work-item-details.ts | 66 +++----------- 4 files changed, 166 insertions(+), 116 deletions(-) diff --git a/src/main/github/client-issue-source.test.ts b/src/main/github/client-issue-source.test.ts index 2399a599d..5b8674897 100644 --- a/src/main/github/client-issue-source.test.ts +++ b/src/main/github/client-issue-source.test.ts @@ -8,6 +8,7 @@ const { getOwnerRepoMock, getIssueOwnerRepoMock, getOwnerRepoForRemoteMock, + resolvePRRepositoryCandidatesMock, resolveIssueSourceMock, rateLimitGuardMock, noteRateLimitSpendMock, @@ -19,6 +20,7 @@ const { getOwnerRepoMock: vi.fn(), getIssueOwnerRepoMock: vi.fn(), getOwnerRepoForRemoteMock: vi.fn(), + resolvePRRepositoryCandidatesMock: vi.fn(), resolveIssueSourceMock: vi.fn(), rateLimitGuardMock: vi.fn(() => ({ blocked: false })), noteRateLimitSpendMock: vi.fn(), @@ -78,7 +80,12 @@ vi.mock('./github-api-repository', async (importOriginal) => { ) => remoteName === 'origin' ? getOwnerRepoMock(repoPath, connectionId, localGitOptions) - : getOwnerRepoForRemoteMock(repoPath, remoteName, connectionId, localGitOptions) + : getOwnerRepoForRemoteMock(repoPath, remoteName, connectionId, localGitOptions), + resolveGitHubApiRepositoryCandidates: ( + repoPath: string, + connectionId?: string | null, + localGitOptions?: unknown + ) => resolvePRRepositoryCandidatesMock(repoPath, connectionId, localGitOptions) } }) @@ -142,6 +149,7 @@ describe('GitHub issue source split', () => { getOwnerRepoMock.mockReset() getIssueOwnerRepoMock.mockReset() getOwnerRepoForRemoteMock.mockReset() + resolvePRRepositoryCandidatesMock.mockReset() resolveIssueSourceMock.mockReset() rateLimitGuardMock.mockReset() rateLimitGuardMock.mockReturnValue({ blocked: false }) @@ -158,15 +166,17 @@ describe('GitHub issue source split', () => { source: await getIssueOwnerRepoMock(), fellBack: false })) - // Why: since #7331 `resolvePrWorkItemSource` fetches origin through - // `getOwnerRepoForRemote` (getOwnerRepo became upstream-first). Route the - // origin probe to `getOwnerRepoMock` so existing tests keep defining the - // origin candidate through it; default upstream to null. Tests that care - // about upstream override with their own implementation. + // Why: keep origin on the legacy mock while hosted candidate tests opt in + // to upstream behavior explicitly. getOwnerRepoForRemoteMock.mockImplementation( async (repoPath: string, remoteName: string, connectionId?: string | null, opts = {}) => remoteName === 'origin' ? getOwnerRepoMock(repoPath, connectionId, opts) : null ) + resolvePRRepositoryCandidatesMock.mockImplementation(async (repoPath, connectionId) => { + const origin = await getOwnerRepoMock(repoPath, connectionId) + const repository = origin ? { host: 'github.com', ...origin } : null + return { candidates: repository ? [repository] : [], headRepo: repository } + }) _resetOwnerRepoCache() }) @@ -461,11 +471,80 @@ describe('GitHub issue source split', () => { '--json', expect.stringContaining('reviewDecision') ], - { cwd: '/repo-root' } + { cwd: '/repo-root', host: 'github.com' } ) expect(item?.type).toBe('pr') }) + it('probes the upstream repository for a typed fork PR before origin', async () => { + const upstream = { owner: 'stablyai', repo: 'orca', host: 'github.com' } + const origin = { owner: 'fork', repo: 'orca', host: 'github.com' } + resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({ + candidates: [upstream, origin], + headRepo: origin + }) + ghExecFileAsyncMock.mockResolvedValueOnce({ + stdout: JSON.stringify({ + number: 42, + title: 'Upstream PR', + state: 'open', + url: 'https://github.com/stablyai/orca/pull/42', + labels: [], + updatedAt: '2026-04-02T00:00:00Z', + author: { login: 'octocat' }, + isDraft: false + }) + }) + + const item = await getWorkItem('/repo-root', 42, 'pr') + + expect(ghExecFileAsyncMock).toHaveBeenCalledWith( + [ + 'pr', + 'view', + '42', + '--repo', + 'stablyai/orca', + '--json', + expect.stringContaining('reviewDecision') + ], + { cwd: '/repo-root', host: 'github.com' } + ) + expect(item?.prRepo).toEqual(upstream) + }) + + it('does not run a bare gh lookup for an SSH repo without candidates', async () => { + resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({ candidates: [], headRepo: null }) + + await expect(getWorkItem('/remote/repo', 42, 'pr', 'ssh-1')).resolves.toBeNull() + + expect(ghExecFileAsyncMock).not.toHaveBeenCalled() + }) + + it('does not probe a second PR repository after a non-not-found failure', async () => { + resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({ + candidates: [ + { owner: 'stablyai', repo: 'orca', host: 'github.com' }, + { owner: 'fork', repo: 'orca', host: 'github.com' } + ], + headRepo: { owner: 'fork', repo: 'orca', host: 'github.com' } + }) + ghExecFileAsyncMock + .mockRejectedValueOnce(new Error('HTTP 500: server error')) + .mockRejectedValueOnce(new Error('HTTP 500: server error')) + + await expect(getWorkItem('/repo-root', 42, 'pr')).resolves.toBeNull() + + // The first candidate uses `pr view` and its REST compatibility fallback; + // neither failure is a 404, so a same-number PR on origin is never queried. + expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(2) + expect( + ghExecFileAsyncMock.mock.calls.some(([args]) => + (args as string[]).some((arg) => arg.includes('fork/orca')) + ) + ).toBe(false) + }) + it('raw number lookup tries upstream issue before origin PR', async () => { getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' }) // Why: simulate a real gh 404 (the only error type that should fall through). @@ -504,7 +583,7 @@ describe('GitHub issue source split', () => { '--json', expect.stringContaining('reviewDecision') ], - { cwd: '/repo-root' } + { cwd: '/repo-root', host: 'github.com' } ) expect(item?.type).toBe('pr') }) diff --git a/src/main/github/client.ts b/src/main/github/client.ts index 429104ef0..8172246aa 100644 --- a/src/main/github/client.ts +++ b/src/main/github/client.ts @@ -883,6 +883,12 @@ async function fetchIssueWorkItem( return mapIssueWorkItem(item) } + if (connectionId) { + // Why: SSH-backed gh has no repository cwd. A bare lookup could honor the + // local process GH_REPO/GH_HOST and return an unrelated repository item. + return null + } + const { stdout } = await ghExecFileAsync( ['issue', 'view', String(number), '--json', 'number,title,state,url,labels,updatedAt,author'], ghOptions @@ -933,7 +939,7 @@ async function fetchPullRequestWorkItem( number: number, connectionId?: string | null, localGitOptions: LocalGitExecOptions = {} -): Promise { +): Promise { const ghOptions = { ...ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)), ...githubHostExecOptions(ownerRepo) @@ -987,6 +993,12 @@ async function fetchPullRequestWorkItem( } } + if (connectionId) { + // Why: connection-backed gh cannot infer a repository from cwd. Refuse a + // bare call so process-level GH_REPO/GH_HOST cannot redirect the lookup. + return null + } + const { stdout } = await ghExecFileAsync( ['pr', 'view', String(number), '--json', WORK_ITEM_PR_DETAIL_JSON_FIELDS], ghOptions @@ -994,6 +1006,39 @@ async function fetchPullRequestWorkItem( return mapPullRequestWorkItem(JSON.parse(stdout) as Record) } +async function fetchPullRequestWorkItemFromCandidates( + repoPath: string, + number: number, + connectionId?: string | null, + localGitOptions: LocalGitExecOptions = {} +): Promise { + const { candidates } = await resolveGitHubApiRepositoryCandidates( + repoPath, + connectionId, + localGitOptions + ) + if (candidates.length === 0) { + return fetchPullRequestWorkItem(repoPath, null, number, connectionId, localGitOptions) + } + for (const candidate of candidates) { + try { + return await fetchPullRequestWorkItem( + repoPath, + candidate, + number, + connectionId, + localGitOptions + ) + } catch (err) { + const message = err instanceof Error ? err.message : String(err) + if (classifyGhError(message).type !== 'not_found') { + throw err + } + } + } + return null +} + type WorkItemListRequest = { args: string[] offset: number @@ -2068,9 +2113,8 @@ export async function getWorkItem( ) } if (type === 'pr') { - return await fetchPullRequestWorkItem( + return await fetchPullRequestWorkItemFromCandidates( repoPath, - await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions), number, connectionId, localGitOptions @@ -2099,9 +2143,8 @@ export async function getWorkItem( throw err } } - return await fetchPullRequestWorkItem( + return await fetchPullRequestWorkItemFromCandidates( repoPath, - await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions), number, connectionId, localGitOptions diff --git a/src/main/github/work-item-details.test.ts b/src/main/github/work-item-details.test.ts index b34eb0acb..60b39d149 100644 --- a/src/main/github/work-item-details.test.ts +++ b/src/main/github/work-item-details.test.ts @@ -239,13 +239,14 @@ describe('getWorkItemDetails', () => { } ]) expect(details?.participants?.[0]?.login).toBe('issue-author') + // Why: issue identities resolve hosted, pinned to github.com for dotcom. expect(repositoryRateLimitGuardMock).toHaveBeenCalledWith( - { owner: 'acme', repo: 'widgets' }, + { owner: 'acme', repo: 'widgets', host: 'github.com' }, 'graphql', {} ) expect(noteRepositoryRateLimitSpendMock).toHaveBeenCalledWith( - { owner: 'acme', repo: 'widgets' }, + { owner: 'acme', repo: 'widgets', host: 'github.com' }, 'graphql', 1, {} @@ -266,7 +267,7 @@ describe('getWorkItemDetails', () => { updatedAt: '2026-04-01T00:00:00Z', author: 'issue-author' }) - getIssueOwnerRepoMock.mockResolvedValue(repository) + getOwnerRepoForRemoteMock.mockResolvedValue(repository) ghExecFileAsyncMock .mockResolvedValueOnce({ stdout: JSON.stringify({ @@ -287,13 +288,14 @@ describe('getWorkItemDetails', () => { const details = await getWorkItemDetails('/repo-root', 923, 'issue', null, localGitOptions) expect(details?.body).toBe('WSL issue body') + // Why: issue identities resolve hosted, pinned to github.com for dotcom. expect(repositoryRateLimitGuardMock).toHaveBeenCalledWith( - repository, + { ...repository, host: 'github.com' }, 'graphql', localGitOptions ) expect(noteRepositoryRateLimitSpendMock).toHaveBeenCalledWith( - repository, + { ...repository, host: 'github.com' }, 'graphql', 1, localGitOptions @@ -620,12 +622,11 @@ describe('getWorkItemDetails', () => { }) it('does not query the default host when an SSH issue repository is unresolved', async () => { - getOwnerRepoMock.mockResolvedValue(null) - getEnterpriseGitHubRepoSlugMock.mockResolvedValue(null) + getWorkItemMock.mockResolvedValue(null) await expect(getWorkItemDetails('/remote/repo', 7, 'issue', 'ssh-1')).resolves.toBeNull() - expect(getWorkItemMock).not.toHaveBeenCalled() + expect(getWorkItemMock).toHaveBeenCalledWith('/remote/repo', 7, 'issue', 'ssh-1') expect(getWorkItemByOwnerRepoMock).not.toHaveBeenCalled() expect(ghExecFileAsyncMock).not.toHaveBeenCalled() }) @@ -712,7 +713,7 @@ describe('getWorkItemDetails', () => { }) it('uses the GitHub Enterprise host for SSH-backed PR work item details', async () => { - getWorkItemByOwnerRepoMock.mockResolvedValueOnce({ + getWorkItemMock.mockResolvedValueOnce({ id: 'pr:7', type: 'pr', number: 7, @@ -721,13 +722,8 @@ describe('getWorkItemDetails', () => { url: 'https://github.acme-corp.com/team/orca/pull/7', labels: [], updatedAt: '2026-07-16T00:00:00Z', - author: 'pr-author' - }) - getOwnerRepoMock.mockResolvedValue(null) - getEnterpriseGitHubRepoSlugMock.mockResolvedValue({ - owner: 'team', - repo: 'orca', - host: 'github.acme-corp.com' + author: 'pr-author', + prRepo: { owner: 'team', repo: 'orca', host: 'github.acme-corp.com' } }) getPRCommentsMock.mockResolvedValue([]) getPRChecksMock.mockResolvedValue([]) @@ -802,15 +798,8 @@ describe('getWorkItemDetails', () => { viewerViewedState: 'VIEWED' } ]) - expect(getWorkItemMock).not.toHaveBeenCalled() - expect(getWorkItemByOwnerRepoMock).toHaveBeenCalledWith( - '/remote/repo', - { owner: 'team', repo: 'orca', host: 'github.acme-corp.com' }, - 7, - 'pr', - 'ssh-1' - ) - expect(getEnterpriseGitHubRepoSlugMock).toHaveBeenCalledTimes(1) + expect(getWorkItemMock).toHaveBeenCalledWith('/remote/repo', 7, 'pr', 'ssh-1') + expect(getWorkItemByOwnerRepoMock).not.toHaveBeenCalled() expect(getPRCommentsMock).toHaveBeenCalledWith( '/remote/repo', 7, @@ -838,38 +827,21 @@ describe('getWorkItemDetails', () => { }) it('does not fall back to the default host after an Enterprise PR lookup fails', async () => { - getOwnerRepoMock.mockResolvedValue(null) - getEnterpriseGitHubRepoSlugMock.mockResolvedValue({ - owner: 'team', - repo: 'orca', - host: 'github.acme-corp.com' - }) - getWorkItemByOwnerRepoMock.mockResolvedValue(null) - getWorkItemMock.mockResolvedValue({ - id: 'pr:7', - type: 'pr', - number: 7, - title: 'Wrong github.com PR', - state: 'open', - url: 'https://github.com/team/orca/pull/7', - labels: [], - updatedAt: '2026-07-16T00:00:00Z', - author: 'wrong-author' - }) + getWorkItemMock.mockResolvedValue(null) await expect(getWorkItemDetails('/remote/repo', 7, 'pr', 'ssh-1')).resolves.toBeNull() - expect(getWorkItemByOwnerRepoMock).toHaveBeenCalledTimes(1) - expect(getWorkItemMock).not.toHaveBeenCalled() + expect(getWorkItemMock).toHaveBeenCalledTimes(1) + expect(getWorkItemByOwnerRepoMock).not.toHaveBeenCalled() + expect(ghExecFileAsyncMock).not.toHaveBeenCalled() }) it('does not query the default host when an SSH PR repository is unresolved', async () => { - getOwnerRepoMock.mockResolvedValue(null) - getEnterpriseGitHubRepoSlugMock.mockResolvedValue(null) + getWorkItemMock.mockResolvedValue(null) await expect(getWorkItemDetails('/remote/repo', 7, 'pr', 'ssh-1')).resolves.toBeNull() - expect(getWorkItemMock).not.toHaveBeenCalled() + expect(getWorkItemMock).toHaveBeenCalledWith('/remote/repo', 7, 'pr', 'ssh-1') expect(getWorkItemByOwnerRepoMock).not.toHaveBeenCalled() expect(ghExecFileAsyncMock).not.toHaveBeenCalled() }) diff --git a/src/main/github/work-item-details.ts b/src/main/github/work-item-details.ts index 7ed75c877..1cb9e9d31 100644 --- a/src/main/github/work-item-details.ts +++ b/src/main/github/work-item-details.ts @@ -21,12 +21,11 @@ import { githubRepoContext, type LocalGitExecOptions } from './gh-utils' -import { getWorkItem, getWorkItemByOwnerRepo, getPRChecks, getPRComments } from './client' +import { getWorkItem, getPRChecks, getPRComments } from './client' import { getIssueGitHubApiRepository, getOriginGitHubApiRepository, githubHostExecOptions, - isGitHubDotComRepository, type GitHubApiRepository } from './github-api-repository' import { noteRepositoryRateLimitSpend, repositoryRateLimitGuard } from './rate-limit' @@ -1074,65 +1073,22 @@ export async function getWorkItemDetails( connectionId?: string | null, localGitOptions: LocalGitExecOptions = {} ): Promise { - const originApiRepository = - type === 'issue' - ? null - : await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions) - // Why: SSH-backed gh runs without a cwd, so an unresolved PR repository must - // not fall through to gh's default host and show a same-number github.com PR. - // Covers type-less lookups too — they can also reach the PR path below. - if (connectionId && type !== 'issue' && !originApiRepository) { - return null - } - const issueApiRepository = - type === 'issue' - ? await getIssueGitHubApiRepository(repoPath, connectionId, localGitOptions) - : null - // Why: the same cwd-less fallthrough exists for issues — an unresolved issue - // repository over SSH must not surface a same-numbered github.com issue. - if (connectionId && type === 'issue' && !issueApiRepository) { - return null - } - // Why: connection-backed repos have no local cwd for gh to inspect, so GHES - // must use its host-qualified repository identity during the initial lookup. - const useExplicitEnterpriseRepository = - type === 'pr' && originApiRepository !== null && !isGitHubDotComRepository(originApiRepository) - let item: Omit | null = useExplicitEnterpriseRepository - ? await getWorkItemByOwnerRepo( - repoPath, - originApiRepository, - number, - 'pr', - connectionId, - ...localGitOptionArgs(localGitOptions) - ) - : await getWorkItem( - repoPath, - number, - type, - connectionId, - ...localGitOptionArgs(localGitOptions) - ) - if (!item && useExplicitEnterpriseRepository && !connectionId) { - // Why: on a local fork checkout the PR lives on the base repo, which the - // origin slug can't see — the cwd lets gh resolve the actual remote. - item = await getWorkItem( - repoPath, - number, - type, - connectionId, - ...localGitOptionArgs(localGitOptions) - ) - } + const item: Omit | null = await getWorkItem( + repoPath, + number, + type, + connectionId, + ...localGitOptionArgs(localGitOptions) + ) if (!item) { return null } const resolvedRepository = item.type === 'issue' - ? (issueApiRepository ?? - (await getIssueGitHubApiRepository(repoPath, connectionId, localGitOptions))) - : (item.prRepo ?? originApiRepository) + ? await getIssueGitHubApiRepository(repoPath, connectionId, localGitOptions) + : (item.prRepo ?? + (await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions))) await acquire() try {