Skip to content

Add repository-scoped auth mapping and repo-aware status - #12681

Closed
awakecoding wants to merge 2 commits into
cli:trunkfrom
awakecoding:dynamic-account-selection
Closed

awakecoding wants to merge 2 commits into
cli:trunkfrom
awakecoding:dynamic-account-selection

Conversation

@awakecoding

@awakecoding awakecoding commented Feb 13, 2026 •

Copy link
Copy Markdown

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 status to 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 gh previously 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/REPO
  • HOST/OWNER/*
  • HOST/OWNER/REPO

Matching behavior:

  1. Exact repo match (owner/repo)
  2. Owner wildcard match (owner/*)
  3. Fallback to existing host-active account behavior

Environment token precedence is preserved (GH_TOKEN, GITHUB_TOKEN, etc.).

2) Dynamic token selection during API requests

gh now 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 status now prints a concise current-repository summary:

  • repository
  • effective account
  • source (repository mapping, active host account, or environment token)

Global host/account sections remain unchanged below the summary.

Example

gh auth map set github.com/awakecoding/* --user awakecoding
gh auth map set github.com/Devolutions/* --user mamoreau-devolutions
gh auth status --active

Validation

  • go test ./internal/config ./pkg/cmd/factory ./pkg/cmd/auth/map ./pkg/cmd/auth/status
  • go build ./...

Notes

  • This keeps backward compatibility for users who do not configure mappings.
  • Global gh auth switch behavior is unchanged.

@awakecoding
awakecoding requested a review from a team as a code owner February 13, 2026 21:30
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Feb 13, 2026
@cliAutomation

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 status to 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.

Comment thread pkg/cmd/factory/dynamic_token_getter.go
Comment thread pkg/cmd/auth/status/status.go Outdated
@BagToad

BagToad commented Feb 15, 2026

Copy link
Copy Markdown
Member

Thank you for your contribution! Unfortunately, this pull request is not linked to a help wanted issue, so we are unable to accept it at this time.

We encourage community contributions on issues labeled help wanted Contributions welcome . If you'd like to contribute, please find an open help wanted Contributions welcome issue and link your PR to it.

If you believe this was done in error, please let us know.

@BagToad BagToad closed this Feb 15, 2026
@mamoreau-devolutions

Copy link
Copy Markdown

@BagToad I understand the need to link pull requests to issues, but I did link it to #12459

There's a real need for automatic account selection. The other issue suggests mapping it to the git configuration which I'd be happy to implement as well, even though my implementation does the mapping independently from git.

I'm just looking for the best place to discuss the issue to be addressed and come up with an accepted change to implement it, regardless of the chosen implementation.

The lack of dynamic account selection in GitHub CLI is a huge pain point when using multiple accounts concurrently, and apparently I'm not the only one facing this issue.

I'd appreciate guidance on the right process to open the right issue to describe the problem, and then discuss the right way to implement the feature. Should I open a new issue if I think it should not necessarily be tied to the underlying SSH configuration? One doesn't prevent the other, so maybe we can do both.

@awakecoding

Copy link
Copy Markdown
Author

Argh. Unrelated to GithHub CLI, but I obviously forgot to switch accounts on the GitHub mobile app before replying. If you have to think of switching, you're repeated going to forget to do it.

@williammartin

williammartin commented Feb 16, 2026 •

Copy link
Copy Markdown
Member

Hey @awakecoding, I saw this come in over the weekend and I had a glance at your blog post. It looks like you'd done a lot of investigation already, so I think it would be great to try and keep some momentum here. I've really wanted to continue working on better multi account support since I first introduced auth switch a couple of years ago. I think what I'd like to see right now is a discussion where we can continue this conversation, and then that can result in an issue where we all agree on the direction. Could you create one?

It's fine if that discussion's starting point is really just "hey, I have these issues (see my blog post), and I created this PR, let's continue the discussion" - I just want a place for us to continue.

@awakecoding

Copy link
Copy Markdown
Author

@williammartin I'm willing to do the required work to come up with a decent solution that satisfies everyone, as this is a huge pain point for me (and apparently others). I opened a new discussion with a summary of my approach with an extension to include leveraging git ssh configuration if we want to include that improvement done by others as well: #12697

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Auto-switch accounts based on git config

6 participants