Make repository identity host-aware across all GitHub surfaces

Generalize the auth-gated enterprise resolver to any remote and build a
cached hosted-identity family (origin/issue/candidates/source) on top of
it, then migrate every github.com-only consumer: Tasks listing/counting,
branch-to-PR discovery, push targets, fork upstream, issue operations,
Projects, web links, avatars, and PR-link facts. Scope the rate-limit
breaker probe per runtime:host and classify WSL UNC cwds correctly.

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinjing
2026-07-16 22:32:04 -07:00
co-authored by Orca
parent 8bbbc62f3d
commit 3587da94b3
39 changed files with 1670 additions and 558 deletions
+71 -39
View File
@@ -49,11 +49,6 @@ import {
extractExecError,
acquire,
release,
getOwnerRepo,
getIssueOwnerRepo,
getOwnerRepoForRemote,
resolvePRRepositoryCandidates,
resolveIssueSource,
classifyGhError,
classifyListIssuesError,
ghRepoExecOptions,
@@ -80,10 +75,15 @@ import {
} from './gh-cwd-repo-negative-cache'
import type { GitHubRepoContext } from './github-repository-identity'
import {
getGitHubApiRepositoryForRemote,
getIssueGitHubApiRepository,
getOriginGitHubApiRepository,
githubHostExecOptions,
githubRepositorySlugArg,
githubRepositoryWebHost,
resolveGitHubApiRepository,
resolveGitHubApiRepositoryCandidates,
resolveIssueGitHubApiRepositorySource,
type GitHubApiRepository
} from './github-api-repository'
import { githubRepoIdentityKey } from '../../shared/github-repository-identity-key'
@@ -329,7 +329,7 @@ export async function getPullRequestPushTarget(
): Promise<PullRequestPushTarget | null> {
const context = githubRepoContext(repoPath, connectionId, localGitOptions)
const ghOptions = ghRepoExecOptions(context)
const { candidates } = await resolvePRRepositoryCandidates(
const { candidates } = await resolveGitHubApiRepositoryCandidates(
repoPath,
connectionId,
localGitOptions
@@ -345,9 +345,7 @@ export async function getPullRequestPushTarget(
try {
const { stdout } = await ghExecFileAsync(
['api', `repos/${candidate.owner}/${candidate.repo}/pulls/${prNumber}`],
{
...ghOptions
}
{ ...ghOptions, ...githubHostExecOptions(candidate) }
)
prStdout = stdout
break
@@ -364,7 +362,12 @@ export async function getPullRequestPushTarget(
if (!prStdout) {
return null
}
const origin = await getOwnerRepoForRemote(repoPath, 'origin', connectionId, localGitOptions)
const origin = await getGitHubApiRepositoryForRemote(
repoPath,
'origin',
connectionId,
localGitOptions
)
const pr = JSON.parse(prStdout) as {
maintainer_can_modify?: boolean
head?: {
@@ -1123,8 +1126,8 @@ async function resolvePrWorkItemSource(
localGitOptions: LocalGitExecOptions = {}
): Promise<ResolvedPrWorkItemSource> {
const [originCandidate, upstreamCandidate] = await Promise.all([
getOwnerRepo(repoPath, connectionId, localGitOptions),
getOwnerRepoForRemote(repoPath, 'upstream', connectionId, localGitOptions)
getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions),
getGitHubApiRepositoryForRemote(repoPath, 'upstream', connectionId, localGitOptions)
])
const source =
preference === 'upstream' ? (upstreamCandidate ?? originCandidate) : originCandidate
@@ -1195,12 +1198,15 @@ async function listRecentWorkItems(
// side, matching the parent design doc's partial-failure rule (§2).
const [issuesSettled, prsSettled] = await Promise.allSettled([
issueOwnerRepo
? ghExecFileAsync(issueRequest.args, ghOptions)
? ghExecFileAsync(issueRequest.args, {
...ghOptions,
...githubHostExecOptions(issueOwnerRepo)
})
: requiresExplicitRepo
? Promise.resolve({ stdout: '[]' })
: ghCwdResolvedExec(repoContext, issueRequest.args, ghOptions),
prOwnerRepo
? ghExecFileAsync(prRequest.args, ghOptions)
? ghExecFileAsync(prRequest.args, { ...ghOptions, ...githubHostExecOptions(prOwnerRepo) })
: requiresExplicitRepo
? Promise.resolve({ stdout: '[]' })
: ghCwdResolvedExec(repoContext, prRequest.args, ghOptions)
@@ -1228,7 +1234,10 @@ async function listRecentWorkItems(
prs = (JSON.parse(prsSettled.value.stdout) as Record<string, unknown>[])
.slice(prRequest.offset, prRequest.offset + limit)
.map((item) => mapPullRequestWorkItem(item, prOwnerRepo))
prs = await hydrateWorkItemRepositoryMergeMetadata(prs, prOwnerRepo, ghOptions)
prs = await hydrateWorkItemRepositoryMergeMetadata(prs, prOwnerRepo, {
...ghOptions,
...githubHostExecOptions(prOwnerRepo)
})
} else {
// Why: PR-side failures must preserve the pre-diff behavior of
// Promise.all by re-throwing so the rejection propagates up through
@@ -1320,7 +1329,10 @@ async function listQueriedWorkItems(
})
try {
const { stdout } = issueOwnerRepo
? await ghExecFileAsync(request.args, ghOptions)
? await ghExecFileAsync(request.args, {
...ghOptions,
...githubHostExecOptions(issueOwnerRepo)
})
: await ghCwdResolvedExec(repoContext, request.args, ghOptions)
return {
items: (JSON.parse(stdout) as Record<string, unknown>[])
@@ -1349,12 +1361,18 @@ async function listQueriedWorkItems(
})
try {
const { stdout } = prOwnerRepo
? await ghExecFileAsync(request.args, ghOptions)
? await ghExecFileAsync(request.args, {
...ghOptions,
...githubHostExecOptions(prOwnerRepo)
})
: await ghCwdResolvedExec(repoContext, request.args, ghOptions)
const mapped = (JSON.parse(stdout) as Record<string, unknown>[])
.slice(request.offset, request.offset + limit)
.map((item) => mapPullRequestWorkItem(item, prOwnerRepo))
const hydrated = await hydrateWorkItemRepositoryMergeMetadata(mapped, prOwnerRepo, ghOptions)
const hydrated = await hydrateWorkItemRepositoryMergeMetadata(mapped, prOwnerRepo, {
...ghOptions,
...githubHostExecOptions(prOwnerRepo)
})
if (query.state === 'closed') {
return hydrated.filter((item) => item.state !== 'merged')
}
@@ -1396,7 +1414,7 @@ export async function listWorkItems(
}
}
const [issueResolved, prResolved] = await Promise.all([
resolveIssueSource(repoPath, preference, connectionId, localGitOptions),
resolveIssueGitHubApiRepositorySource(repoPath, preference, connectionId, localGitOptions),
resolvePrWorkItemSource(repoPath, preference, connectionId, localGitOptions)
])
const issueOwnerRepo = issueResolved.source
@@ -1504,7 +1522,10 @@ async function countWorkItemsForQuery(
localGitOptions: LocalGitExecOptions = {}
): Promise<number> {
const searchQ = buildSearchQueryString(ownerRepo, query)
const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions))
const ghOptions = {
...ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)),
...githubHostExecOptions(ownerRepo)
}
const { stdout } = await ghExecFileAsync(
[
'api',
@@ -1561,7 +1582,7 @@ export async function countWorkItems(
return 0
}
const [issueResolved, prResolved] = await Promise.all([
resolveIssueSource(repoPath, preference, connectionId, localGitOptions),
resolveIssueGitHubApiRepositorySource(repoPath, preference, connectionId, localGitOptions),
resolvePrWorkItemSource(repoPath, preference, connectionId, localGitOptions)
])
const issueOwnerRepo = issueResolved.source
@@ -1658,8 +1679,12 @@ export async function getRepoSlug(
repoPath: string,
connectionId?: string | null,
options: HostedReviewExecutionOptions = {}
): Promise<{ owner: string; repo: string } | null> {
return getOwnerRepo(repoPath, connectionId, ...hostedReviewLocalGitOptionArgs(options))
): Promise<GitHubApiRepository | null> {
return getOriginGitHubApiRepository(
repoPath,
connectionId,
getHostedReviewLocalGitOptions(options)
)
}
/**
@@ -1677,27 +1702,30 @@ export async function getRepoUpstream(
): Promise<OwnerRepo | null> {
const localGitArgs = hostedReviewLocalGitOptionArgs(options)
const localGitOptions = localGitArgs[0] ?? {}
const origin = await getOwnerRepo(repoPath, connectionId, ...localGitArgs)
const origin = await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions)
if (!origin) {
return null
}
const upstreamRemote = await getOwnerRepoForRemote(
const upstreamRemote = await getGitHubApiRepositoryForRemote(
repoPath,
'upstream',
connectionId,
...localGitArgs
localGitOptions
)
if (upstreamRemote && !sameOwnerRepo(upstreamRemote, origin)) {
return upstreamRemote
}
await acquire()
try {
// Why: positional slugs bypass the runner's --repo qualifier, so the slug
// itself must carry the Enterprise host.
const { stdout } = await ghExecFileAsync(
['repo', 'view', `${origin.owner}/${origin.repo}`, '--json', 'isFork,parent'],
['repo', 'view', githubRepositorySlugArg(origin), '--json', 'isFork,parent'],
// Why: best-effort fork lookup runs at add-time; cap latency so a stalled
// gh process can't hold up repo creation.
{
...ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)),
...githubHostExecOptions(origin),
timeout: 10_000
}
)
@@ -1707,7 +1735,10 @@ export async function getRepoUpstream(
}
const owner = data.parent?.owner?.login
const repo = data.parent?.name
return data.isFork && owner && repo ? { owner, repo } : null
// Why: a fork parent lives on the same server as the fork.
return data.isFork && owner && repo
? { owner, repo, ...(origin.host ? { host: origin.host } : {}) }
: null
} catch {
return null
} finally {
@@ -2015,7 +2046,7 @@ export async function getWorkItem(
if (type === 'issue') {
return await fetchIssueWorkItem(
repoPath,
await getIssueOwnerRepo(repoPath, connectionId, localGitOptions),
await getIssueGitHubApiRepository(repoPath, connectionId, localGitOptions),
number,
connectionId,
localGitOptions
@@ -2024,7 +2055,7 @@ export async function getWorkItem(
if (type === 'pr') {
return await fetchPullRequestWorkItem(
repoPath,
await getOwnerRepo(repoPath, connectionId, localGitOptions),
await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions),
number,
connectionId,
localGitOptions
@@ -2034,7 +2065,7 @@ export async function getWorkItem(
try {
const issue = await fetchIssueWorkItem(
repoPath,
await getIssueOwnerRepo(repoPath, connectionId, localGitOptions),
await getIssueGitHubApiRepository(repoPath, connectionId, localGitOptions),
number,
connectionId,
localGitOptions
@@ -2056,7 +2087,7 @@ export async function getWorkItem(
}
return await fetchPullRequestWorkItem(
repoPath,
await getOwnerRepo(repoPath, connectionId, localGitOptions),
await getOriginGitHubApiRepository(repoPath, connectionId, localGitOptions),
number,
connectionId,
localGitOptions
@@ -2960,10 +2991,11 @@ export async function getPRForBranch(
// Why: the exact-linked fallback (`gh pr view` with no resolved repo candidates)
// returns dataRepo=null, which would leave the merged-PR membership probe unable
// to run. Derive the PR's own repo from its web URL so a diverged merged linked
// PR can still be confirmed and cleared. Host-agnostic to cover GitHub Enterprise.
// PR can still be confirmed and cleared. The URL's hostname carries the identity
// for GitHub Enterprise, so the probe targets the PR's actual server.
function ownerRepoFromPullRequestUrl(url: string): OwnerRepo | null {
const match = url.match(/^https?:\/\/[^/\s]+\/([^/\s]+)\/([^/\s]+)\/pull\/\d+/)
return match ? { owner: match[1], repo: match[2] } : null
const match = url.match(/^https?:\/\/([^/\s]+)\/([^/\s]+)\/([^/\s]+)\/pull\/\d+/)
return match ? { owner: match[2], repo: match[3], host: match[1] } : null
}
export async function getPRForBranchOutcome(
@@ -2988,10 +3020,10 @@ export async function getPRForBranchOutcome(
await acquire()
try {
const { candidates, headRepo } = await resolvePRRepositoryCandidates(
const { candidates, headRepo } = await resolveGitHubApiRepositoryCandidates(
repoPath,
connectionId,
...localGitArgs
localGitOptions
)
let data: PullRequestLookupData | null = null
let dataRepo: OwnerRepo | null = null
@@ -3110,11 +3142,11 @@ export async function getPRForBranchOutcome(
)
if (upstreamBranch) {
const upstreamHeadRepo =
(await getOwnerRepoForRemote(
(await getGitHubApiRepositoryForRemote(
repoPath,
upstreamBranch.remoteName,
connectionId,
...localGitArgs
localGitOptions
)) ?? headRepo
if (
upstreamHeadRepo &&