Skip to content

Commit 02d71c3

Browse files
authored
fix(cli): exclude GITHUB_API_URL when api-proxy is enabled (#1303)
* Initial plan * fix(cli): exclude GITHUB_API_URL when api-proxy is enabled On GHES, workflows set GITHUB_API_URL to the GHES API endpoint (e.g., https://api.ghes-host). When api-proxy is enabled, this variable should NOT be passed to the agent container, because Copilot CLI would use it for Copilot API requests, which don't exist on GHES API. Instead, the agent should use COPILOT_API_URL pointing to the proxy, which correctly routes Copilot API requests to api.enterprise.githubcopilot.com (not the GHES API which lacks Copilot endpoints). This fix ensures: - GITHUB_API_URL is excluded from agent env when --enable-api-proxy is set - COPILOT_API_URL takes precedence for Copilot API routing - The API proxy's deriveCopilotApiTarget() correctly determines the endpoint Fixes: github/gh-aw#20875 --------- Co-authored-by: anthropic-code-agent[bot] <242468646+Claude@users.noreply.github.com>
1 parent 8cd7451 commit 02d71c3

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/docker-manager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,13 @@ export function generateDockerCompose(
483483
if (process.env.XDG_CONFIG_HOME) environment.XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME;
484484
// Enterprise environment variables — needed for GHEC/GHES Copilot authentication
485485
if (process.env.GITHUB_SERVER_URL) environment.GITHUB_SERVER_URL = process.env.GITHUB_SERVER_URL;
486-
if (process.env.GITHUB_API_URL) environment.GITHUB_API_URL = process.env.GITHUB_API_URL;
486+
// GITHUB_API_URL — only pass when api-proxy is NOT enabled.
487+
// On GHES, workflows set GITHUB_API_URL to the GHES API endpoint (e.g., https://api.ghes-host).
488+
// When api-proxy is enabled, Copilot CLI must use COPILOT_API_URL (pointing to the proxy)
489+
// instead of GITHUB_API_URL, because the proxy correctly routes Copilot API requests to
490+
// api.enterprise.githubcopilot.com (not the GHES API which lacks Copilot endpoints).
491+
// See: github/gh-aw#20875
492+
if (process.env.GITHUB_API_URL && !config.enableApiProxy) environment.GITHUB_API_URL = process.env.GITHUB_API_URL;
487493
}
488494

489495
// Forward one-shot-token debug flag if set (used for testing/debugging)

tests/integration/api-proxy.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,54 @@ describe('API Proxy Sidecar', () => {
250250
expect(result).toSucceed();
251251
expect(result.stdout).toContain('"copilot":true');
252252
}, 180000);
253+
254+
test('should exclude GITHUB_API_URL from agent when api-proxy is enabled (GHES fix)', async () => {
255+
// On GHES, workflows set GITHUB_API_URL to the GHES API endpoint (e.g., https://api.ghes-host).
256+
// When api-proxy is enabled, GITHUB_API_URL should NOT be passed to the agent container,
257+
// because Copilot CLI would use it for Copilot API requests, which don't exist on GHES API.
258+
// Instead, the agent should use COPILOT_API_URL pointing to the proxy, which correctly
259+
// routes to api.enterprise.githubcopilot.com.
260+
// See: github/gh-aw#20875
261+
const result = await runner.runWithSudo(
262+
'bash -c "if [ -z \\"$GITHUB_API_URL\\" ]; then echo GITHUB_API_URL_NOT_SET; else echo GITHUB_API_URL=$GITHUB_API_URL; fi"',
263+
{
264+
allowDomains: ['api.githubcopilot.com'],
265+
enableApiProxy: true,
266+
buildLocal: true,
267+
logLevel: 'debug',
268+
timeout: 120000,
269+
env: {
270+
COPILOT_GITHUB_TOKEN: 'ghp_fake-test-token-12345',
271+
// Simulate GHES workflow passing GITHUB_API_URL
272+
GITHUB_API_URL: 'https://api.ghes-host.example.com',
273+
},
274+
}
275+
);
276+
277+
expect(result).toSucceed();
278+
// GITHUB_API_URL should NOT be set in agent container when api-proxy is enabled
279+
expect(result.stdout).toContain('GITHUB_API_URL_NOT_SET');
280+
// COPILOT_API_URL should point to the proxy instead
281+
expect(result.stdout).toContain(`COPILOT_API_URL=http://${API_PROXY_IP}:10002`);
282+
}, 180000);
283+
284+
test('should pass GITHUB_API_URL to agent when api-proxy is NOT enabled', async () => {
285+
// When api-proxy is disabled, GITHUB_API_URL should be passed through normally
286+
const result = await runner.runWithSudo(
287+
'bash -c "echo GITHUB_API_URL=$GITHUB_API_URL"',
288+
{
289+
allowDomains: ['api.githubcopilot.com'],
290+
enableApiProxy: false,
291+
buildLocal: true,
292+
logLevel: 'debug',
293+
timeout: 120000,
294+
env: {
295+
GITHUB_API_URL: 'https://api.github.com',
296+
},
297+
}
298+
);
299+
300+
expect(result).toSucceed();
301+
expect(result.stdout).toContain('GITHUB_API_URL=https://api.github.com');
302+
}, 180000);
253303
});

0 commit comments

Comments
 (0)