Skip to content

Print gh auth refresh for 401 returns#13068

Merged
BagToad merged 2 commits into
cli:trunkfrom
333fred:print-refresh-for-401s
May 5, 2026
Merged

Print gh auth refresh for 401 returns#13068
BagToad merged 2 commits into
cli:trunkfrom
333fred:print-refresh-for-401s

Conversation

@333fred

@333fred 333fred commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

gh auth refresh exists to make it simpler for users to refresh their tokens on expiration/scope mismatch, but help messages only suggest using it in limited scenarios, and not in a common case of a token expiring and the user receiving a 401 error. Now, the auth flow will detect this case, and for refreshable tokens (namely, tokens created by logging in with gh auth login in the first place), it will suggest using gh auth refresh for these cases.

Fixes #13066.

@333fred 333fred requested a review from a team as a code owner March 30, 2026 19:54
@333fred 333fred requested review from babakks and Copilot March 30, 2026 19:54
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed labels Mar 30, 2026

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

Updates CLI authentication guidance on HTTP 401 errors to prefer gh auth refresh when the user’s stored credentials are refreshable (i.e., created via gh auth login), reducing friction compared to always suggesting gh auth login.

Changes:

  • Add token “refreshable” detection and use it to suggest gh auth refresh for invalid/expired stored OAuth tokens.
  • Update gh top-level 401 error recovery message to dynamically choose gh auth refresh -h <host> vs gh auth login.
  • Add/adjust tests for the new recovery-command selection and updated auth status output.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pkg/cmd/auth/status/status.go Switches re-auth guidance to gh auth refresh when the invalid token is refreshable.
pkg/cmd/auth/status/status_test.go Updates expected output to reflect refresh guidance for refreshable tokens.
pkg/cmd/auth/shared/writeable.go Introduces AuthTokenRefreshable helper for identifying refreshable stored OAuth tokens.
internal/ghcmd/cmd.go On 401, determines whether to print gh auth refresh -h <host> or fallback to gh auth login.
internal/ghcmd/cmd_test.go Adds unit tests covering authRecoveryCommand behavior across token/source/requestURL cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

`gh auth refresh` exists to make it simpler for users to refresh their tokens on expiration/scope mismatch, but help messages only suggest using it in limited scenarios, and not in a common case of a token expiring and the user receiving a 401 error. Now, the auth flow will detect this case, and for refreshable tokens (namely, tokens created by logging in with `gh auth login` in the first place), it will suggest using `gh auth refresh` for these cases.
@BagToad BagToad self-requested a review April 20, 2026 18:09
@BagToad BagToad force-pushed the print-refresh-for-401s branch from 5ac48b6 to a656271 Compare April 20, 2026 18:10
@BagToad

BagToad commented Apr 20, 2026

Copy link
Copy Markdown
Member

Apologies for the delay @333fred - I rebased and force pushed because there was some conflicts with trunk.

Comment thread internal/ghcmd/cmd.go Outdated

@BagToad BagToad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apologies this took so long @333fred, but this LGTM. Appreciate the efforts here.

@BagToad BagToad enabled auto-merge May 5, 2026 17:35
@BagToad BagToad merged commit 7c43919 into cli:trunk May 5, 2026
11 checks passed
@333fred 333fred deleted the print-refresh-for-401s branch May 5, 2026 18:25
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 needs-triage needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Print gh auth refresh instead of gh auth login when the user encounters a 401

3 participants