fix(source-control): route GitHub Enterprise Server remotes to the GitHub provider for PR creation (#8312) (#8603)

* fix(source-control): route GHES remotes to the GitHub provider for PR creation

A GitHub Enterprise Server user could not submit a PR — Orca demanded
ORCA_GITEA_TOKEN — while issue sync worked fine (#8312).

Root cause: GitHub owner/repo resolution (parseGitHubOwnerRepo) hard-rejects
any host that is not literally github.com. A GHES remote lives on a custom
host, so GitHub's forge resolveRepository returned null and provider detection
fell through the list to Gitea, whose KNOWN_NON_GITEA_HOSTS denylist cannot
enumerate arbitrary GHES domains. Issue sync was unaffected because gh
issue/pr list run with cwd=repoPath and let gh resolve the GHES host natively.

Fix mirrors GitLab self-hosted detection (getGlabKnownHosts): a new
getEnterpriseGitHubRepoSlug resolves a custom-host origin to owner/repo only
when gh is authenticated to that host — gh only ever manages GitHub/GHES
credentials, so a logged-in host is definitively GitHub. Wired into:
- forge-provider GitHub resolveRepository (fallback after github.com miss),
  so detection claims GHES before Gitea is consulted;
- createGitHubPullRequest owner/repo resolution;
- isGitHubAuthenticated, which now probes the repo's real host instead of a
  hardcoded --hostname github.com.

github.com repos keep the cached getRepoSlug fast path and never spawn the
extra gh auth probe.

* fix(github): host-qualify GHES gh commands and probe auth in the repo runtime

Addresses two correctness issues found in review of the #8312 fix.

1. GHES host was discarded before `gh pr create`. `--repo owner/repo` shorthand
   resolves against gh's default host (usually github.com), so for a user
   authed to both github.com and GHES it could target a same-named github.com
   repo or fail — deterministic for SSH repos, which run gh with no cwd. Now
   `createGitHubPullRequest` and the `findOpenPRByHeadBase` fallback pass a
   host-qualified `HOST/owner/repo` for GHES (github.com keeps the shorthand).
   Also generalize `parseCreatePRPayload`'s URL regex off github.com so a GHES
   PR URL parses directly instead of limping through the list fallback.

2. GHES auth was probed on the wrong gh runtime. `getAuthenticatedGitHubHosts`
   ran a global `gh auth status` with no cwd/WSL/SSH context and cached every
   runtime under one "local" key, so a GHES login present only in the repo's
   WSL distro was missed and the repo fell back to Gitea. Replaced with
   `isGitHubHostAuthenticated`, which runs `gh auth status --hostname <host>`
   with the repository's execution options (cwd/WSL distro, or SSH-local like
   the create path) and caches per runtime+host — mirroring GitLab's
   isGlabConfiguredForRemoteHost. This also honors GH_ENTERPRISE_TOKEN inferred
   from repo context. Spawn failures stay indeterminate (uncached).

Adds createGitHubPullRequest-level tests asserting the actual gh `--repo`
arguments (create + fallback) and the WSL/SSH runtime of the auth probe.

* perf(source-control): drop redundant GHES gh auth probe in eligibility

Review follow-up. Detection only routes a GHES remote to the GitHub provider
after getEnterpriseGitHubRepoSlug has confirmed gh is authenticated to its
host, so isGitHubAuthenticated can trust a non-null slug as authenticated and
skip a second, rate-limited `gh auth status` spawn per eligibility poll.
Reaching the github.com probe now implies the remote is github.com. Tests
assert the enterprise path fires no redundant gh probe.
This commit is contained in:
Brennan Benson
2026-07-13 18:06:06 -07:00
committed by GitHub
parent 73d83a9fb4
commit 2f660a6028
8 changed files with 569 additions and 18 deletions
+48 -2
View File
@@ -11,7 +11,8 @@ const {
getMergeRequestForBranchMock,
getProjectSlugMock,
getPRForBranchOutcomeMock,
getRepoSlugMock
getRepoSlugMock,
getEnterpriseGitHubRepoSlugMock
} = vi.hoisted(() => ({
createGitHubPullRequestMock: vi.fn(),
createGitLabMergeRequestMock: vi.fn(),
@@ -23,7 +24,8 @@ const {
getMergeRequestForBranchMock: vi.fn(),
getProjectSlugMock: vi.fn(),
getPRForBranchOutcomeMock: vi.fn(),
getRepoSlugMock: vi.fn()
getRepoSlugMock: vi.fn(),
getEnterpriseGitHubRepoSlugMock: vi.fn()
}))
vi.mock('../gitlab/client', () => ({
@@ -42,6 +44,10 @@ vi.mock('../github/client', () => ({
getPRForBranchOutcome: getPRForBranchOutcomeMock
}))
vi.mock('../github/github-enterprise-repository', () => ({
getEnterpriseGitHubRepoSlug: getEnterpriseGitHubRepoSlugMock
}))
vi.mock('../bitbucket/client', () => ({
getBitbucketRepoSlug: getBitbucketRepoSlugMock,
getBitbucketPullRequestForBranch: vi.fn(),
@@ -88,6 +94,7 @@ describe('forge provider interface', () => {
getProjectSlugMock.mockReset()
getPRForBranchOutcomeMock.mockReset()
getRepoSlugMock.mockReset()
getEnterpriseGitHubRepoSlugMock.mockReset()
})
it('preserves the existing hosted provider detection order', async () => {
@@ -101,6 +108,45 @@ describe('forge provider interface', () => {
expect(getRepoSlugMock).not.toHaveBeenCalled()
})
it('detects a GitHub Enterprise Server remote as the GitHub provider, not Gitea', async () => {
// Regression for #8312: a GHES host is not github.com, so github.com-only
// slug parsing returns null. Detection must claim it via the enterprise
// resolver instead of falling through to Gitea's demand for ORCA_GITEA_TOKEN.
getProjectSlugMock.mockResolvedValue(null)
getRepoSlugMock.mockResolvedValue(null)
getEnterpriseGitHubRepoSlugMock.mockResolvedValue({
owner: 'team',
repo: 'orca',
host: 'github.acme-corp.com'
})
await expect(detectHostedReviewProvider({ repoPath: '/repo' })).resolves.toBe('github')
await expect(getForgeProviderForRepository({ repoPath: '/repo' })).resolves.toMatchObject({
id: 'github'
})
// Gitea must never be consulted once GitHub claims the enterprise host.
expect(getGiteaRepoSlugMock).not.toHaveBeenCalled()
})
it('leaves a genuinely non-GitHub remote for later providers when gh is not authenticated', async () => {
getProjectSlugMock.mockResolvedValue(null)
getRepoSlugMock.mockResolvedValue(null)
// gh is not logged in to this host, so the enterprise resolver declines and
// the Gitea provider is free to claim its own self-hosted remote.
getEnterpriseGitHubRepoSlugMock.mockResolvedValue(null)
getBitbucketRepoSlugMock.mockResolvedValue(null)
getAzureDevOpsRepoSlugMock.mockResolvedValue(null)
getGiteaRepoSlugMock.mockResolvedValue({
host: 'gitea.example.com',
owner: 'team',
repo: 'orca',
apiBaseUrl: 'https://gitea.example.com/api/v1',
webBaseUrl: 'https://gitea.example.com'
})
await expect(detectHostedReviewProvider({ repoPath: '/repo' })).resolves.toBe('gitea')
})
it('keeps review creation capability scoped to providers with creation support', async () => {
expect(
FORGE_PROVIDERS.map((provider) => [provider.id, provider.supportsReviewCreation])
+20 -2
View File
@@ -22,6 +22,7 @@ import {
} from '../gitea/client'
import { createGiteaPullRequest } from '../gitea/pull-request-creation'
import { createGitHubPullRequest, getPRForBranchOutcome, getRepoSlug } from '../github/client'
import { getEnterpriseGitHubRepoSlug } from '../github/github-enterprise-repository'
import { getMergeRequest, getMergeRequestForBranch, getProjectSlug } from '../gitlab/client'
import { createGitLabMergeRequest } from '../gitlab/merge-request-creation'
import {
@@ -122,8 +123,25 @@ function unwrapGitHubPRForBranchOutcome(
const gitHubForgeProvider = {
id: 'github',
supportsReviewCreation: true,
resolveRepository: (context) =>
getRepoSlug(context.repoPath, context.connectionId, ...hostedReviewExecutionArgs(context)),
resolveRepository: async (context) => {
const slug = await getRepoSlug(
context.repoPath,
context.connectionId,
...hostedReviewExecutionArgs(context)
)
if (slug) {
return slug
}
// Why: GHES remotes live on a custom host, so github.com-only slug parsing
// misses them and detection would otherwise fall through to Gitea (#8312).
// Claim the repo when gh is authenticated to its host — the same signal
// GitLab uses for self-hosted instances.
return getEnterpriseGitHubRepoSlug(
context.repoPath,
context.connectionId,
...hostedReviewExecutionArgs(context)
)
},
async getReviewForBranch(input) {
const fallbackReviewNumber =
input.linkedReviewNumber == null ? (input.fallbackReviewNumber ?? null) : null
@@ -17,7 +17,8 @@ const {
glabExecFileAsyncMock,
gitExecFileAsyncMock,
getUpstreamStatusMock,
getSshGitProviderMock
getSshGitProviderMock,
getEnterpriseGitHubRepoSlugMock
} = vi.hoisted(() => ({
createGitHubPullRequestMock: vi.fn(),
createGitLabMergeRequestMock: vi.fn(),
@@ -35,7 +36,8 @@ const {
glabExecFileAsyncMock: vi.fn(),
gitExecFileAsyncMock: vi.fn(),
getUpstreamStatusMock: vi.fn(),
getSshGitProviderMock: vi.fn()
getSshGitProviderMock: vi.fn(),
getEnterpriseGitHubRepoSlugMock: vi.fn()
}))
vi.mock('../github/client', () => ({
@@ -44,6 +46,10 @@ vi.mock('../github/client', () => ({
getPRForBranch: vi.fn()
}))
vi.mock('../github/github-enterprise-repository', () => ({
getEnterpriseGitHubRepoSlug: getEnterpriseGitHubRepoSlugMock
}))
vi.mock('../gitlab/client', () => ({
getProjectSlug: getProjectSlugMock,
getMergeRequestForBranch: vi.fn(),
@@ -129,7 +135,8 @@ function resetMocks(): void {
glabExecFileAsyncMock,
gitExecFileAsyncMock,
getUpstreamStatusMock,
getSshGitProviderMock
getSshGitProviderMock,
getEnterpriseGitHubRepoSlugMock
]) {
mock.mockReset()
}
@@ -141,6 +148,22 @@ function mockGitHubProvider(): void {
getBitbucketRepoSlugMock.mockResolvedValue(null)
getAzureDevOpsRepoSlugMock.mockResolvedValue(null)
getGiteaRepoSlugMock.mockResolvedValue(null)
getEnterpriseGitHubRepoSlugMock.mockResolvedValue(null)
}
// GHES: github.com-only slug parsing misses the custom host, so the enterprise
// resolver claims the repo and reports the host for the gh auth probe (#8312).
function mockGitHubEnterpriseProvider(): void {
getProjectSlugMock.mockResolvedValue(null)
getRepoSlugMock.mockResolvedValue(null)
getBitbucketRepoSlugMock.mockResolvedValue(null)
getAzureDevOpsRepoSlugMock.mockResolvedValue(null)
getGiteaRepoSlugMock.mockResolvedValue(null)
getEnterpriseGitHubRepoSlugMock.mockResolvedValue({
owner: 'acme',
repo: 'orca',
host: 'github.acme-corp.com'
})
}
function mockGitLabProvider(): void {
@@ -349,6 +372,29 @@ describe('createHostedReview', () => {
)
})
it('creates a pull request on a GitHub Enterprise Server remote (#8312)', async () => {
mockGitHubEnterpriseProvider()
await expect(
createHostedReview('/repo', {
provider: 'github',
base: 'main',
head: 'feature',
title: 'Feature'
})
).resolves.toEqual({
ok: true,
number: 12,
url: 'https://github.com/acme/orca/pull/12'
})
// Detection already confirmed gh is authed to the GHES host, so the auth
// gate must not fire a second (rate-limited) gh probe.
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
expect(createGitHubPullRequestMock).toHaveBeenCalled()
expect(createGiteaPullRequestMock).not.toHaveBeenCalled()
})
it('creates a GitLab merge request after fresh main-process validation passes', async () => {
mockGitLabProvider()
@@ -636,6 +682,31 @@ describe('getHostedReviewCreationEligibility', () => {
})
})
it('detects a GitHub Enterprise Server branch as the GitHub provider (#8312)', async () => {
mockGitHubEnterpriseProvider()
await expect(
getHostedReviewCreationEligibility({
repoPath: '/repo',
branch: 'feature/create-pr',
base: 'origin/main',
hasUncommittedChanges: false,
hasUpstream: true,
ahead: 0,
behind: 0
})
).resolves.toMatchObject({
provider: 'github',
canCreate: true,
blockedReason: null,
nextAction: null
})
// Enterprise auth was already confirmed during detection; the gate must not
// fire a redundant gh probe.
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
})
it('resolves remote eligibility through SSH repo metadata without generating PR copy', async () => {
const remoteGit = {
exec: vi.fn(async () => ({ stdout: '', stderr: '' }))
@@ -18,6 +18,7 @@ import {
} from '../../shared/hosted-review-creation-providers'
import { isAzureDevOpsReviewCreationAuthenticated } from '../azure-devops/pull-request-creation'
import { isGiteaReviewCreationAuthenticated } from '../gitea/pull-request-creation'
import { getEnterpriseGitHubRepoSlug } from '../github/github-enterprise-repository'
import { acquire, ghExecFileAsync, gitExecFileAsync, release } from '../github/gh-utils'
import { isNoUpstreamError, normalizeGitErrorMessage } from '../../shared/git-remote-error'
import type { GitUpstreamStatus } from '../../shared/types'
@@ -59,6 +60,14 @@ async function isGitHubAuthenticated(
connectionId?: string | null,
options: HostedReviewExecutionOptions = {}
): Promise<boolean> {
// Why: a GHES remote is only routed to the GitHub provider once detection has
// confirmed gh is authenticated to its enterprise host, so a non-null slug
// already means authenticated — skip a redundant, rate-limited gh probe.
// Reaching the github.com check below therefore means the remote is github.com
// (its own custom host would have resolved above) (#8312).
if (await getEnterpriseGitHubRepoSlug(repoPath, connectionId, options)) {
return true
}
await acquire()
try {
await ghExecFileAsync(