Add repository-scoped auth mapping and repo-aware status#12681
Add repository-scoped auth mapping and repo-aware status#12681awakecoding wants to merge 2 commits intocli:trunkfrom
Conversation
|
Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message. |
There was a problem hiding this comment.
Pull request overview
This pull request adds repository-scoped account selection to GitHub CLI, enabling users to work with multiple GitHub accounts on the same host without manually switching the global active account. The implementation includes a new gh auth map command group for managing repository-to-account mappings, dynamic token selection during API requests based on repository context, and enhanced gh auth status output that displays the effective account for the current repository.
Changes:
- Added
gh auth map {set,list,remove}commands to configure per-repository or per-owner account mappings - Implemented dynamic token getter that resolves repository context from git remotes and applies mappings transparently during API requests
- Enhanced
gh auth statusto display current repository authentication information when available
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/cmd/auth/map/map.go |
New command implementation for managing repository account mappings with support for owner/* and owner/repo scopes |
pkg/cmd/auth/map/map_test.go |
Test coverage for map command parsing and CRUD operations |
pkg/cmd/auth/auth.go |
Integration of new map command into auth command group |
pkg/cmd/auth/status/status.go |
Added repository-aware status display showing effective account and source |
pkg/cmd/auth/status/status_test.go |
Test coverage for repository status display with mapped and unmapped scenarios |
pkg/cmd/factory/dynamic_token_getter.go |
Token selection logic that applies repository mappings when resolving API credentials |
pkg/cmd/factory/dynamic_token_getter_test.go |
Tests verifying mapping-based token selection and environment token precedence |
pkg/cmd/factory/default.go |
HTTP client factory integration to use dynamic token getter with repository context |
internal/config/config.go |
Config layer methods for storing/retrieving repository-to-user mappings with case-insensitive owner/repo matching |
internal/config/auth_config_test.go |
Config layer test coverage for mapping CRUD operations and lookup priority |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
This PR adds repository-scoped account selection to GitHub CLI so users can work across multiple accounts on the same host without repeatedly running a global
gh auth switch.It also updates
gh auth statusto show the effective account for the current repository while preserving existing global host/account status output.Fixes #12459 and #12628 in a slightly different way than suggested. I prefer the current approach as it does not rely on external SSH configuration. The auth mapping mechanism is simple and works at the GitHub CLI level. A few simple mapping rules should do the trick to eliminate issues when using multiple GitHub accounts at the same time.
Motivation
This addresses multi-account workflow friction described in:
In short: git/SSH can already route identities per repo/org, but
ghpreviously required manual global account switching.What changed
1) Repository account mappings
Added a new command group:
gh auth map set <scope> --user <login>gh auth map list [--hostname <host>]gh auth map remove <scope>Supported scopes:
OWNER/*OWNER/REPOHOST/OWNER/*HOST/OWNER/REPOMatching behavior:
owner/repo)owner/*)Environment token precedence is preserved (
GH_TOKEN,GITHUB_TOKEN, etc.).2) Dynamic token selection during API requests
ghnow resolves a repository context from remotes and applies repository mapping when selecting a token for API requests, instead of requiring global active-user mutation.3) Repository-aware auth status
gh auth statusnow prints a concise current-repository summary:repository mapping,active host account, orenvironment token)Global host/account sections remain unchanged below the summary.
Example
Validation
go test ./internal/config ./pkg/cmd/factory ./pkg/cmd/auth/map ./pkg/cmd/auth/statusgo build ./...Notes
gh auth switchbehavior is unchanged.