Skip to content

fix: support GitHub Enterprise authentication in Copilot CLI agent#314724

Open
fighterii wants to merge 3 commits intomicrosoft:mainfrom
fighterii:fix/copilot-cli-ghe-auth
Open

fix: support GitHub Enterprise authentication in Copilot CLI agent#314724
fighterii wants to merge 3 commits intomicrosoft:mainfrom
fighterii:fix/copilot-cli-ghe-auth

Conversation

@fighterii
Copy link
Copy Markdown

@fighterii fighterii commented May 6, 2026

Problem

The Copilot CLI Agent does not work with GitHub Enterprise (GHE) deployments. Two issues prevent GHE users from using the CLI agent:

  1. Wrong API URL: The SDK defaults to copilot-api.github.com but GHE deployments use a different Copilot API endpoint (typically copilot-api.<ghe-host>).
  2. Wrong token type: The SDK sends the raw GitHub OAuth token, but the GHE Copilot API requires a Copilot JWT token obtained via token exchange.

Fixes #304075

Solution

Changes are limited to extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts:

resolveGitHubHost()

  • Normalizes the github-enterprise.uri setting via URI.parse() to extract only scheme://authority, stripping any trailing slash, path, or query parameters.

resolveCopilotApiUrl()

  • Preserves the scheme from the configured GHE URI (instead of hardcoding https), deriving {scheme}://copilot-api.{authority} as a fallback URL.

getAuthInfo() (GHE path)

  • Gates the getCopilotToken() call to only run for GHE auth (AuthProviderId.GitHubEnterprise), avoiding unnecessary network calls for github.com users.
  • Wraps getCopilotToken() in try/catch so OAuth fallback is reachable if token minting fails.
  • Uses the Copilot JWT token (from token exchange) for GHE instead of the raw OAuth token.
  • Sets process.env['COPILOT_API_URL'] only for GHE; clears it otherwise to prevent stale state on provider switch.
  • Passes the resolved API URL via copilotUser.endpoints.api in the TokenAuthInfo return value (not through the session's copilotUrl which would incorrectly trigger hmac/proxy mode).

Testing

Added 5 new unit tests in copilotCliAuth.spec.ts:

  • GHE auth uses Copilot JWT token and normalized host
  • Token endpoint takes precedence over derived URL
  • Falls back to derived copilot-api.{host} URL when token has no endpoints
  • Falls back to OAuth when getCopilotToken() throws
  • Proxy mode always uses 'https://github.com' host per SDK type constraint

Copilot AI review requested due to automatic review settings May 6, 2026 13:12
@fighterii
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make Copilot CLI agent sessions work against GitHub Enterprise (GHE) by using the GHE-aware Copilot API endpoint returned during token exchange and (for GHE) using the Copilot JWT rather than the raw OAuth token.

Changes:

  • Update CLI SDK auth resolution to use getCopilotToken() and prefer copilotToken.endpoints.api for the effective Copilot API URL.
  • Plumb the effective Copilot API URL into session options (copilotUrl) so sessions target the correct API.
  • Add unit tests covering GHE host/token selection and endpoint fallback behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts Adds GHE host/API URL resolution and switches GHE auth to Copilot JWT + endpoint-based API URL.
extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotcliSessionService.ts Sets copilotUrl using the SDK’s resolved effective Copilot API URL (in addition to proxy override).
extensions/copilot/src/extension/chatSessions/copilotcli/node/test/copilotCliAuth.spec.ts Adds tests for GHE host/token usage, endpoint preference, and fallback behavior.

Comment thread extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts Outdated
The Copilot CLI SDK did not work with GitHub Enterprise (GHE) deployments
because of two issues:

1. The API URL was not correctly resolved for GHE instances. The SDK needs
   the Copilot API URL from the token exchange response (endpoints.api)
   rather than assuming a URL pattern.

2. The SDK received the GitHub OAuth token for authentication, but GHE's
   Copilot API validates model entitlements using the Copilot JWT token
   obtained during token exchange.

Changes:
- getAuthInfo() now resolves the Copilot API URL from the token's
  endpoints.api field, falling back to a derived URL based on the GHE host.
- For GHE, use the Copilot JWT token instead of the OAuth token.
- resolveGitHubHost() returns the configured GHE URI as the auth host.
- resolveEffectiveCopilotApiUrl() exposed on the interface for session
  options to use the token-based URL resolution.
- The session service now uses the async token-aware URL resolution for
  copilotUrl in session options.

Fixes microsoft#304075
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

Comment thread extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts Outdated
Comment thread extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts Outdated
Comment thread extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCli.ts Outdated
@fighterii fighterii force-pushed the fix/copilot-cli-ghe-auth branch 2 times, most recently from 4273989 to 83286ce Compare May 6, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copilot CLI Session (Agent Mode) fails with GHE

3 participants