Repository navigation
Allow git operations when remote host differs from API host - #12180
fotoetienne wants to merge 1 commit into
Conversation
|
Thank you for your pull request! 🎉 This PR appears to fix the following issues that are not labeled with
help wanted
As outlined in our Contributing Guidelines, we expect that PRs are only created for issues that have been labeled While we appreciate your initiative, please note that:
What happens next:
Thank you for your understanding and contribution to the project! 🙏 This comment was automatically generated by cliAutomation. |
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #12179 by allowing git operations to work when the git remote hostname differs from the GitHub API hostname, which is common in enterprise environments using git proxies or SSH bastions.
Key Changes:
- Modified remote resolver to fall back to all remotes instead of failing when no remotes match authenticated hosts
- Removed error paths that prevented legitimate enterprise configurations from working
- Deleted unused
fmtimport after removing error formatting code
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Fall back to all remotes for commands that only need owner/repo matching. | ||
| // Commands performing API operations will still fail with clear errors when | ||
| // the API host is wrong. This allows git operations through proxies/bastions | ||
| // where the git remote host differs from the GitHub API host. | ||
| rr.cachedRemotes = resolvedRemotes |
There was a problem hiding this comment.
Falling back to all remotes without validation could allow commands to operate on unintended repositories. Consider adding a warning log or requiring explicit opt-in via configuration when operating in this fallback mode to make the behavior visible to users and prevent silent failures when configuration is genuinely incorrect.
Currently, gh rejects git remotes when their hostname doesn't match the authenticated GitHub host, even though this is a legitimate enterprise configuration. This prevents commands like `gh pr checkout` from working in environments with git proxies or SSH bastions. The validation is unnecessarily strict because: - Git operations only need owner/repo matching, not hostname matching - Git commands use the remote name (e.g., "origin"), not the URL - API operations already validate the host separately via GH_HOST This change allows all remotes to be considered when no remotes match authenticated hosts. Commands performing API operations will still fail appropriately with clear errors when the API host is wrong. Fixes cli#12179 Real-world scenario this fixes: - Git remote: [email protected]:corp/repo.git - API endpoint: github.company.com - Previously: "none of the git remotes configured" error - Now: Works correctly for git operations Changes: - Remove strict hostname validation in remote resolver - Fall back to all remotes for git operations - Remove unused fmt import
78008cc to
4d7b893
Compare
|
Thanks for the PR and the detailed write-up; the enterprise proxy use case is legitimate and I can see how this is frustrating. However, I don't think we can merge this as-is because it removes important diagnostic error messages that help users troubleshoot real configuration problems. Here's what I think gets lost: 1. if isHostEnv(src) {
rr.remotesError = fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable", src)
return nil, rr.remotesError
}If a user sets 2. Env token without } else if cfg.Authentication().HasEnvToken() {
rr.remotesError = errors.New("set the GH_HOST environment variable to specify which GitHub host to use")
return nil, rr.remotesError
}When 3. Unknown host (line 90-91): rr.remotesError = errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`")When remotes point to a self-hosted instance the user never authenticated to, this tells them exactly what to do. The fallback would proceed with unauthenticated remotes and fail later with opaque errors. I think we need a more targeted approach to support the proxy use case without losing these diagnostics for everyone else. |
|
Thanks for taking a look! |
Summary
Fixes #12179 - Allows
gh pr checkoutand related commands to work when git remote hostname differs from GitHub API hostname.Problem
Commands like
gh pr checkout,gh pr create, andgh repo forkfail in enterprise environments where:git.company.com(proxy/bastion)github.company.com(GitHub Enterprise)Error encountered:
This affects legitimate enterprise configurations using git proxies, SSH bastions, or git mirrors.
Root Cause
The remote resolver in
pkg/cmd/factory/remote_resolver.goreturns an error when no git remotes match authenticated hosts, even though:origin), not the URLGH_HOSTThe git remote hostname and API hostname are separate concerns.
Solution
Changed the remote resolver to fall back to all remotes when no remotes match authenticated hosts, rather than failing with an error.
Before:
After:
Changes Made
pkg/cmd/factory/remote_resolver.go:fmtimportTesting
Test scenario:
Verified:
gh pr checkoutworks with mismatched hostnamesgh pr view,gh pr liststill work (API operations)Real-world testing:
Impact
Benefits:
Who this helps:
Security Considerations
GH_HOSTand auth configAlternative Considered
Created a separate
GitRemotes()factory method for git-only operations, but the fallback approach is simpler and achieves the same goal with less code.