fix(agent-task): resolve Copilot API URL dynamically#12956
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes gh agent-task failures on ghe.com tenancy hosts by dynamically resolving the correct Copilot API base URL per authenticated host via a GraphQL lookup, instead of assuming https://api.githubcopilot.com.
Changes:
- Add Copilot API endpoint resolution (
viewer.copilotEndpoints.api) and wire it intoCapiClientFunc. - Refactor the CAPI client to accept a resolved base URL (removing hardcoded constants) and use instance-level URL construction.
- Update session/job CAPI calls and tests to use the new URL plumbing.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/cmd/agent-task/shared/capi.go | Resolves the Copilot API URL via GraphQL and passes it into the CAPI client constructor. |
| pkg/cmd/agent-task/shared/capi_test.go | Adds tests covering endpoint resolution and that CapiClientFunc triggers the GraphQL lookup. |
| pkg/cmd/agent-task/capi/client.go | Removes hardcoded Copilot base URL; adds capiURL to the client and derives host for conditional headers. |
| pkg/cmd/agent-task/capi/sessions.go | Switches session endpoints to build URLs from the instance capiURL. |
| pkg/cmd/agent-task/capi/sessions_test.go | Updates tests to construct the client with an explicit base URL parameter. |
| pkg/cmd/agent-task/capi/job.go | Switches jobs endpoints to use the instance capiURL via a helper method. |
| pkg/cmd/agent-task/capi/job_test.go | Updates tests to construct the client with an explicit base URL parameter. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
d71b1be to
15def46
Compare
Query viewer.copilotEndpoints.api to get the correct Copilot API URL for the user's host instead of hardcoding api.githubcopilot.com. This fixes 401 errors for ghe.com tenancy users whose Copilot API lives at a different endpoint. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
15def46 to
4da2abf
Compare
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
- Remove redundant assert on GraphQL request path in test - Rename capiURL to capiBaseURL for clarity Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
gh agent-taskcommands fail with401 Unauthorizedfor users on ghe.com tenancy hosts. The Copilot API URL was hardcoded toapi.githubcopilot.com, but ghe.com tenants use a different endpoint. The URL must be resolved dynamically per host.Key changes
shared/capi.go: AddedresolveCapiURLwhich queriesviewer.copilotEndpoints.apivia GraphQL to get the correct Copilot API URL for the user's host. Uses the cached HTTP client to avoid redundant lookups.capi/client.go: Removed hardcodedbaseCAPIURLandcapiHostconstants.NewCAPIClientnow accepts acapiURLparameter. The transport extracts the host from the resolved URL for conditional header matching.capi/sessions.go/capi/job.go: ReplacedbaseCAPIURLconstant references with the instance-levelcapiURLfield.Notes for Reviewers
api.githubcopilot.comfor some github.com users, depending on their plan.IsEnterpriseguard inagent_task.goalready allows ghe.com tenancy hosts through (they are not considered "enterprise" bygo-gh), so no changes were needed there.