From 39661fc222537047586266891b27616eb02e4f57 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 11 Nov 2026 14:50:02 -0800 Subject: [PATCH] fix(github): gate non-origin remote probes in getGitHubApiRepositoryForRemote Callers such as getRepoUpstream and the tracked-upstream branch fallback called getGitHubApiRepositoryForRemote without checking the cached remote listing, causing repos without an upstream remote to run failing git remote get-url upstream execs on cold lookups. Gate non-origin remotes on shouldProbeGitRemote at the shared entry point while keeping origin unconditional. Refs #22684 Co-authored-by: h5k <31630310+h5kk@users.noreply.github.com> --- ...github-api-repository-remote-probe.test.ts | 92 +++++++++++++++++++ .../github-api-repository-remote-probe.ts | 8 ++ 2 files changed, 100 insertions(+) create mode 100644 src/main/github/github-api-repository-remote-probe.test.ts diff --git a/src/main/github/github-api-repository-remote-probe.test.ts b/src/main/github/github-api-repository-remote-probe.test.ts new file mode 100644 index 000000000000..ecd8f8ab7a45 --- /dev/null +++ b/src/main/github/github-api-repository-remote-probe.test.ts @@ -0,0 +1,92 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type * as GitExecFileModule from '../git/command-runner/git-exec-file' + +const { gitExecFileAsyncMock } = vi.hoisted(() => ({ + gitExecFileAsyncMock: vi.fn<(args: string[]) => Promise<{ stdout: string; stderr: string }>>() +})) + +vi.mock('../git/command-runner/git-exec-file', async (importOriginal) => ({ + ...(await importOriginal()), + gitExecFileAsync: gitExecFileAsyncMock +})) + +import { _resetOwnerRepoCache } from './gh-utils' +import { _resetRemoteNameListingCache } from '../git/remotes/remote-name-listing' +import { + _resetOriginGitHubApiRepositoryCache, + getGitHubApiRepositoryForRemote +} from './github-api-repository-remote-probe' + +// Why: no real .git config means no config signature, so caches use their short unsigned TTLs. +const REPO_PATH = '/does-not-exist/orca-remote-probe-fixture' + +function mockRemotes(remotes: Record): void { + gitExecFileAsyncMock.mockImplementation((args) => { + if (args[0] === 'remote' && args.length === 1) { + return Promise.resolve({ stdout: Object.keys(remotes).join('\n'), stderr: '' }) + } + if (args[0] === 'remote' && args[1] === 'get-url') { + const url = remotes[args[2]] + if (url) { + return Promise.resolve({ stdout: `${url}\n`, stderr: '' }) + } + return Promise.reject( + Object.assign(new Error(`git remote get-url ${args[2]}`), { + stderr: `error: No such remote '${args[2]}'` + }) + ) + } + return Promise.reject(new Error(`unexpected git exec: ${JSON.stringify(args)}`)) + }) +} + +function getUrlCalls(remoteName: string): number { + return gitExecFileAsyncMock.mock.calls.filter( + ([args]) => args[0] === 'remote' && args[1] === 'get-url' && args[2] === remoteName + ).length +} + +beforeEach(() => { + vi.useFakeTimers() + gitExecFileAsyncMock.mockReset() + _resetOriginGitHubApiRepositoryCache() + _resetOwnerRepoCache() + _resetRemoteNameListingCache() +}) + +afterEach(() => { + vi.useRealTimers() +}) + +describe('getGitHubApiRepositoryForRemote', () => { + it('does not run get-url for a non-origin remote the listing says is absent', async () => { + mockRemotes({ origin: 'https://github.com/acme/widgets.git' }) + + for (let poll = 0; poll < 3; poll += 1) { + await expect(getGitHubApiRepositoryForRemote(REPO_PATH, 'upstream')).resolves.toBeNull() + await vi.advanceTimersByTimeAsync(60_000) + } + + expect(getUrlCalls('upstream')).toBe(0) + }) + + it('still resolves a configured upstream remote', async () => { + mockRemotes({ + origin: 'https://github.com/acme/orca.git', + upstream: 'https://github.com/stablyai/orca.git' + }) + + await expect(getGitHubApiRepositoryForRemote(REPO_PATH, 'upstream')).resolves.toEqual({ + owner: 'stablyai', + repo: 'orca', + host: 'github.com' + }) + }) + + it('probes origin even when the listing omits it', async () => { + mockRemotes({}) + + await expect(getGitHubApiRepositoryForRemote(REPO_PATH, 'origin')).resolves.toBeNull() + expect(getUrlCalls('origin')).toBeGreaterThan(0) + }) +}) diff --git a/src/main/github/github-api-repository-remote-probe.ts b/src/main/github/github-api-repository-remote-probe.ts index 10d98af41248..b7c59ed52d0c 100644 --- a/src/main/github/github-api-repository-remote-probe.ts +++ b/src/main/github/github-api-repository-remote-probe.ts @@ -1,5 +1,6 @@ import { withGitHubRepoExecution } from './github-repository-identity' import type { GitHubApiRepository } from './github-api-repository' +import { shouldProbeGitRemote } from '../git/remotes/remote-name-listing' import { getOwnerRepoForRemote, type GitHubRemoteIdentityProbeOptions, @@ -49,6 +50,13 @@ export async function getGitHubApiRepositoryForRemote( probeOptions: GitHubRemoteIdentityProbeOptions = {} ): Promise { return withGitHubRepoExecution(repoPath, connectionId, localGitOptions, async () => { + // Why: skip remotes the listing says are absent; origin stays unconditional so its absence is definitive. + if ( + remoteName !== 'origin' && + !(await shouldProbeGitRemote(repoPath, remoteName, connectionId, localGitOptions)) + ) { + return null + } // Generic PR resolution prefers upstream; this entry point must honor the selected remote. const requireVerifiedSshProbe = probeOptions.requireVerifiedSshProbe === true const verifiedIdentityArgs = requireVerifiedSshProbe ? ([probeOptions] as const) : []