Bound GHES identity caches and preserve non-default ports in host identity
Cap the origin-repo and host-auth caches like ownerRepoCache; keep ports from remote/link URLs so GHES on a non-default port is a distinct identity; make positional github.com slugs explicit against GH_HOST; compare work-item sources by host-aware identity key; bail cwd-less branch lookups when no repository candidate resolved; thread host through the renderer work-item slug lookup. Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -3614,7 +3614,9 @@ describe('getPRForBranch', () => {
|
||||
await expect(getRepoUpstream('/repo-root')).resolves.toBeNull()
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
|
||||
['repo', 'view', 'StablyAI/Orca', '--json', 'isFork,parent'],
|
||||
// Why: positional slugs are explicit about github.com too, so GH_HOST
|
||||
// cannot redirect them.
|
||||
['repo', 'view', 'github.com/StablyAI/Orca', '--json', 'isFork,parent'],
|
||||
{ cwd: '/repo-root', host: 'github.com', timeout: 10_000 }
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1539,12 +1539,9 @@ async function countWorkItemsForQuery(
|
||||
|
||||
function sameOwnerRepo(left: OwnerRepo | null, right: OwnerRepo | null): boolean {
|
||||
// Why: GitHub treats owner and repo names as case-insensitive, so remotes
|
||||
// with different casing (StablyAI/Orca vs stablyai/orca) point at the same
|
||||
// repo and should not split into two search queries.
|
||||
return (
|
||||
left?.owner.toLowerCase() === right?.owner.toLowerCase() &&
|
||||
left?.repo.toLowerCase() === right?.repo.toLowerCase()
|
||||
)
|
||||
// with different casing point at the same repo, but identical slugs on
|
||||
// github.com and GHES are independent repositories.
|
||||
return Boolean(left && right && githubRepoIdentityKey(left) === githubRepoIdentityKey(right))
|
||||
}
|
||||
|
||||
function defaultOpenWorkItemQuery(): ParsedTaskQuery {
|
||||
@@ -3017,6 +3014,11 @@ export async function getPRForBranchOutcome(
|
||||
connectionId,
|
||||
localGitOptions
|
||||
)
|
||||
// Why: connection-backed gh runs without a repository cwd. A bare lookup
|
||||
// here can honor process GH_REPO/GH_HOST and return an unrelated PR.
|
||||
if (connectionId && candidates.length === 0) {
|
||||
return { kind: 'no-pr', fetchedAt: Date.now() }
|
||||
}
|
||||
let data: PullRequestLookupData | null = null
|
||||
let dataRepo: OwnerRepo | null = null
|
||||
let dataHeadRepo: OwnerRepo | null = headRepo
|
||||
|
||||
@@ -15,6 +15,7 @@ export type GitHubApiRepository = GitHubOwnerRepo
|
||||
// round trip on connection-backed repos) — hot paths like per-file contents
|
||||
// and viewed-state toggles resolve per call, so cache like ownerRepoCache does.
|
||||
const ORIGIN_REPO_CACHE_TTL_MS = 30_000
|
||||
const ORIGIN_REPO_CACHE_MAX_ENTRIES = 512
|
||||
const originRepoCache = new Map<string, { value: GitHubApiRepository | null; expiresAt: number }>()
|
||||
const originRepoInFlight = new Map<string, Promise<GitHubApiRepository | null>>()
|
||||
|
||||
@@ -33,6 +34,26 @@ export function _resetOriginGitHubApiRepositoryCache(): void {
|
||||
originRepoInFlight.clear()
|
||||
}
|
||||
|
||||
/** @internal - exposed for cache-bound tests only */
|
||||
export function _getOriginGitHubApiRepositoryCacheSize(): number {
|
||||
return originRepoCache.size
|
||||
}
|
||||
|
||||
function pruneOriginRepoCache(now: number): void {
|
||||
for (const [key, entry] of originRepoCache) {
|
||||
if (entry.expiresAt <= now) {
|
||||
originRepoCache.delete(key)
|
||||
}
|
||||
}
|
||||
while (originRepoCache.size > ORIGIN_REPO_CACHE_MAX_ENTRIES) {
|
||||
const oldestKey = originRepoCache.keys().next().value
|
||||
if (oldestKey === undefined) {
|
||||
return
|
||||
}
|
||||
originRepoCache.delete(oldestKey)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Host-qualified repository identity for one remote: github.com remotes come
|
||||
* from the cached slug parser; any other GitHub-shaped host is auth-gated so a
|
||||
@@ -56,8 +77,10 @@ export async function getGitHubApiRepositoryForRemote(
|
||||
return { ...ownerRepo, host: 'github.com' }
|
||||
}
|
||||
const cacheKey = originRepoCacheKey(repoPath, remoteName, connectionId, localGitOptions)
|
||||
const now = Date.now()
|
||||
pruneOriginRepoCache(now)
|
||||
const cached = originRepoCache.get(cacheKey)
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
if (cached && cached.expiresAt > now) {
|
||||
return cached.value
|
||||
}
|
||||
const inFlight = originRepoInFlight.get(cacheKey)
|
||||
@@ -77,6 +100,7 @@ export async function getGitHubApiRepositoryForRemote(
|
||||
enterpriseOptions
|
||||
)
|
||||
originRepoCache.set(cacheKey, { value: slug, expiresAt: Date.now() + ORIGIN_REPO_CACHE_TTL_MS })
|
||||
pruneOriginRepoCache(Date.now())
|
||||
return slug
|
||||
})()
|
||||
originRepoInFlight.set(cacheKey, probe)
|
||||
@@ -241,11 +265,11 @@ export function githubRepositoryWebHost(repository: GitHubApiRepository): string
|
||||
/**
|
||||
* Positional `HOST/OWNER/REPO` argv value (e.g. `gh repo view <slug>`).
|
||||
* Positional slugs bypass the runner's `--repo` qualifier, so they must be
|
||||
* qualified here; github.com stays bare for compatibility with gh's default.
|
||||
* qualified here whenever the host is known.
|
||||
*/
|
||||
export function githubRepositorySlugArg(repository: GitHubApiRepository): string {
|
||||
const slug = `${repository.owner}/${repository.repo}`
|
||||
return repository.host && !isDefaultGitHubHost(repository.host)
|
||||
? `${repository.host}/${slug}`
|
||||
: slug
|
||||
// Why: github.com must be explicit too; otherwise process-level GH_HOST can
|
||||
// redirect positional `gh repo view OWNER/REPO` calls to an Enterprise host.
|
||||
return repository.host ? `${repository.host}/${slug}` : slug
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ export type GitHubEnterpriseRepoSlug = GitHubOwnerRepo & { host: string }
|
||||
// mirrors the `glab auth status` signal GitLab self-hosted detection uses, so a
|
||||
// GHES remote is not left to fall through to Gitea (#8312).
|
||||
const HOST_AUTH_TTL_MS = 60_000
|
||||
const HOST_AUTH_CACHE_MAX_ENTRIES = 512
|
||||
|
||||
type HostAuthCacheEntry = {
|
||||
authenticated: boolean
|
||||
@@ -43,6 +44,26 @@ export function _resetGitHubHostAuthCache(): void {
|
||||
hostAuthInFlight.clear()
|
||||
}
|
||||
|
||||
/** @internal - exposed for cache-bound tests only */
|
||||
export function _getGitHubHostAuthCacheSize(): number {
|
||||
return hostAuthCache.size
|
||||
}
|
||||
|
||||
function pruneHostAuthCache(now: number): void {
|
||||
for (const [key, entry] of hostAuthCache) {
|
||||
if (entry.expiresAt <= now) {
|
||||
hostAuthCache.delete(key)
|
||||
}
|
||||
}
|
||||
while (hostAuthCache.size > HOST_AUTH_CACHE_MAX_ENTRIES) {
|
||||
const oldestKey = hostAuthCache.keys().next().value
|
||||
if (oldestKey === undefined) {
|
||||
return
|
||||
}
|
||||
hostAuthCache.delete(oldestKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Only gh's own stdout/stderr — not the Error.message — counts as an
|
||||
// authoritative answer. A spawn failure (gh missing, ENOENT) carries just a
|
||||
// message and no command output, and must stay indeterminate rather than be
|
||||
@@ -72,6 +93,7 @@ export async function isGitHubHostAuthenticated(
|
||||
const normalizedHost = host.toLowerCase()
|
||||
const cacheKey = `${runtimeCacheKey(connectionId, localGitOptions.wslDistro)}\0${normalizedHost}`
|
||||
const now = Date.now()
|
||||
pruneHostAuthCache(now)
|
||||
const cached = hostAuthCache.get(cacheKey)
|
||||
if (cached && cached.expiresAt > now) {
|
||||
return cached.authenticated
|
||||
@@ -107,6 +129,7 @@ export async function isGitHubHostAuthenticated(
|
||||
)
|
||||
}
|
||||
hostAuthCache.set(cacheKey, { authenticated, expiresAt: Date.now() + HOST_AUTH_TTL_MS })
|
||||
pruneHostAuthCache(Date.now())
|
||||
return authenticated
|
||||
})()
|
||||
hostAuthInFlight.set(cacheKey, probe)
|
||||
|
||||
@@ -5,7 +5,9 @@ export type GitHubRemoteIdentity = GitHubOwnerRepo & { host: string }
|
||||
function normalizeGitHubRemoteHost(host: string): string {
|
||||
const normalizedHost = host.toLowerCase()
|
||||
// Why: GitHub documents ssh.github.com:443 as SSH-over-HTTPS for github.com repos.
|
||||
return normalizedHost === 'ssh.github.com' ? 'github.com' : normalizedHost
|
||||
return normalizedHost === 'ssh.github.com' || normalizedHost === 'ssh.github.com:443'
|
||||
? 'github.com'
|
||||
: normalizedHost
|
||||
}
|
||||
|
||||
function parseGitHubRemotePath(path: string): Pick<GitHubRemoteIdentity, 'owner' | 'repo'> | null {
|
||||
@@ -34,7 +36,9 @@ export function parseGitHubRemoteIdentity(remoteUrl: string): GitHubRemoteIdenti
|
||||
return null
|
||||
}
|
||||
const path = parseGitHubRemotePath(url.pathname)
|
||||
return path ? { host: normalizeGitHubRemoteHost(url.hostname), ...path } : null
|
||||
// Why: GHES may be served on a non-default port. `hostname` silently drops
|
||||
// it, sending authenticated API calls to a different endpoint on port 443.
|
||||
return path ? { host: normalizeGitHubRemoteHost(url.host), ...path } : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user