Repository navigation
feat(auth): Better multi-account gh account usage #13768 - #13781
platinummonkey wants to merge 3 commits into
Conversation
Automatically select which authenticated account gh acts as based on local, API-free context (working directory and base-repo owner) instead of the single global active account. This addresses cli#13768: multiple accounts on the same host (e.g. an EMU + a personal github.com identity) cannot be disambiguated by host, and `gh auth switch` mutates global state, which is unsafe for concurrent agents. New `account_rules` config key (gitdir + owner maps) and an explicit `--account` / `GH_ACCOUNT` override. Resolution wraps the HTTP client's token getter and never mutates persistent state. Token precedence: env token (GH_TOKEN etc.) > --account/GH_ACCOUNT > rules > globally active account. When no rules or override are configured, behavior is byte-identical to before. `gh auth status` now shows which account resolves for the current context, and GH_DEBUG traces resolution, so automatic switching is observable rather than silent. Co-Authored-By: Claude Opus 4.8 <[email protected]>
# Conflicts: # pkg/cmd/auth/status/status.go
jamesng-minhkhoi
left a comment
There was a problem hiding this comment.
Thanks for the draft — this is exactly the shape I'd want for #13768, and it resolves correctly for the concurrent-agents case (global state never touched). I checked out the branch locally and go test ./internal/accountrules/... ./pkg/cmd/factory/... ./internal/config/... passes on go1.27. Three findings, mostly edge cases:
1. A trailing @ account value silently disables the host check
parseAccount (internal/accountrules/accountrules.go:97-103) on "user@" yields Host: "", and the resolver skips its host check when acct.Host == "" (pkg/cmd/factory/account_resolver.go:81) — so a typo'd rule applies to every host, including enterprise hosts it was never meant for.
Suggested fix (fail loudly at parse/load):
func parseAccount(value, defaultHost string) Account {
value = strings.TrimSpace(value)
if i := strings.LastIndex(value, "@"); i >= 0 {
host := value[i+1:]
if host == "" {
host = defaultHost
}
return Account{User: value[:i], Host: host}
}
return Account{User: value, Host: defaultHost}
}
(or reject empty hosts with an error at config parse so users see the typo).
2. Nondeterministic winner on equal-length gitdir prefixes
longestPrefixMatch (accountrules.go:108-125) iterates over a map; rules like ~/work and ~/work/ both clean to the same path, and on equal len(p) the winner depends on map order — resolution becomes random per process. Widening ties deterministically (e.g. iterate sort.Strings(prefixes), later key wins, or reject same-length distinct keys at load) would make repeated runs stable.
|
Continuing the review — two more items: 3. 4. Nit: Verified locally on go1.27.3/darwin-arm64: |
FIXES #13768
Automatically select which authenticated account
ghacts as based on local, API-free context (working directory and base-repo owner) instead of the single global active account. This addresses #13768: multiple accounts on the same host (e.g. an EMU + a personal github.com identity) cannot be disambiguated by host, andgh auth switchmutates global state, which is unsafe for concurrent agents.New
account_rulesconfig key (gitdir + owner maps) and an explicit--account/GH_ACCOUNToverride. Resolution wraps the HTTP client's token getter and never mutates persistent state.Token precedence: env token (
GH_TOKENetc.) >--account/GH_ACCOUNT> rules >globally active account. When no rules or override are configured, behavior is byte-identical to before.
gh auth statusnow shows which account resolves for the current context, and GH_DEBUG traces resolution, so automatic switching is observable rather than silent.