Files
orca/src/main/github/review-head-remote.ts
T
Neil a40e5012f5 perf(git): reuse queries and stop canceled catalog scans (#24923)
* perf(git): reuse queries and stop canceled catalog scans

* refactor(git): check relay filesystem error codes
2026-10-03 02:54:20 -07:00

54 lines
2.0 KiB
TypeScript

import type { IssueSourcePreference } from '../../shared/repo-types'
import type { GhAccountBinding } from '../../shared/github/account-binding'
import { pickPreferredGitRemote } from '../../shared/preferred-git-remote'
import { getDefaultRemote } from '../git/repo'
import { getGitHubApiRepositoryForRemote } from './github-api-repository'
type GitExec = (args: string[]) => Promise<{ stdout: string; stderr: string }>
// Why: explicit origin must match issue listing; otherwise hosting identity
// keeps contributor clones on the upstream project's PR namespace.
export async function resolveGitHubReviewHeadRemote(args: {
repoPath: string
issueSourcePreference?: IssueSourcePreference
connectionId?: string | null
localGitOptions?: { wslDistro?: string; ghAccount?: GhAccountBinding }
gitExec: GitExec
}): Promise<string> {
const { stdout } = await args.gitExec(['remote'])
const remotes = stdout
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
if (args.issueSourcePreference === 'origin') {
if (remotes.includes('origin')) {
return 'origin'
}
throw new Error('Repo has no configured origin remote.')
}
// Why: identity probes cost a `remote get-url` (plus a possible gh auth
// lookup) each; only multi-remote clones are ambiguous enough to need them.
if (remotes.length > 1) {
for (const remote of ['upstream', 'origin']) {
if (!remotes.includes(remote)) {
continue
}
const repository = await getGitHubApiRepositoryForRemote(
args.repoPath,
remote,
args.connectionId ?? null,
args.localGitOptions ?? {}
)
if (repository) {
return remote
}
}
}
// Why: when no remote maps to a GitHub project the hosting identity cannot
// guide the choice; keep the legacy per-transport fallback.
if (args.connectionId) {
return pickPreferredGitRemote(remotes)
}
return getDefaultRemote(args.repoPath, args.localGitOptions ?? {}, remotes)
}