-
Notifications
You must be signed in to change notification settings - Fork 84
feat: GitHub CLI authentication support #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lox
wants to merge
7
commits into
cashapp:master
Choose a base branch
from
lox:lox/2025-02-23-gh-cli-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
357527b
feat: add GitHub CLI authentication support
lox 635e119
Remove token debug logging
lox baba0c8
auth.NewProvider or GetToken() failure should be fatal
lox 41875a1
Use constants for provider types
lox 9509fdf
Defer auth token fetching to when called
lox bd8cc2b
Test failed gh auth token call
lox 5b678ac
fix: restore cli setup before github auth
lox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/cashapp/hermit/errors" | ||
| "github.com/cashapp/hermit/ui" | ||
| ) | ||
|
|
||
| const ( | ||
| ProviderTypeEnv = "env" | ||
| ProviderTypeGHCli = "gh-cli" | ||
| ) | ||
|
|
||
| // Provider is an interface for GitHub token providers | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily in scope for this PR, but it might be good to make this more generalized beyond GitHub. |
||
| type Provider interface { | ||
| // GetToken returns a GitHub token or an error if one cannot be obtained | ||
| GetToken() (string, error) | ||
| } | ||
|
|
||
| // EnvProvider implements Provider using environment variables | ||
| type EnvProvider struct { | ||
| ui *ui.UI | ||
| } | ||
|
|
||
| // GetToken returns a token from environment variables | ||
| func (p *EnvProvider) GetToken() (string, error) { | ||
| p.ui.Debugf("Getting GitHub token from environment variables") | ||
| if token := os.Getenv("HERMIT_GITHUB_TOKEN"); token != "" { | ||
| p.ui.Tracef("Using HERMIT_GITHUB_TOKEN for GitHub authentication") | ||
| return token, nil | ||
| } | ||
| if token := os.Getenv("GITHUB_TOKEN"); token != "" { | ||
| p.ui.Tracef("Using GITHUB_TOKEN for GitHub authentication") | ||
| return token, nil | ||
| } | ||
| p.ui.Tracef("No GitHub token found in environment") | ||
| return "", errors.New("no GitHub token found in environment") | ||
| } | ||
|
|
||
| // GHCliProvider implements Provider using the gh CLI tool | ||
| type GHCliProvider struct { | ||
| // cache the token and only refresh when needed | ||
| token string | ||
| tokenLock sync.Mutex | ||
| ui *ui.UI | ||
| } | ||
|
|
||
| // GetToken returns a token from gh CLI | ||
| func (p *GHCliProvider) GetToken() (string, error) { | ||
| p.ui.Debugf("Getting GitHub token from gh") | ||
| p.tokenLock.Lock() | ||
| defer p.tokenLock.Unlock() | ||
|
|
||
| // Return cached token if available | ||
| if p.token != "" { | ||
| return p.token, nil | ||
| } | ||
|
|
||
| // Check if gh is installed | ||
| ghPath, err := exec.LookPath("gh") | ||
| if err != nil { | ||
| return "", errors.New("gh CLI not found in PATH") | ||
| } | ||
|
|
||
| p.ui.Tracef("gh found: %s", ghPath) | ||
|
|
||
| // Run gh auth token | ||
| cmd := exec.Command("gh", "auth", "token") | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| p.ui.Warnf("gh auth failed: %s", strings.TrimSpace(string(output))) | ||
| return "", errors.Wrap(err, "gh auth failed") | ||
| } | ||
|
|
||
| p.token = strings.TrimSpace(string(output)) | ||
| return p.token, nil | ||
| } | ||
|
|
||
| // NewProvider creates a new token provider based on the specified type | ||
| func NewProvider(providerType string, ui *ui.UI) (Provider, error) { | ||
| switch providerType { | ||
| case ProviderTypeEnv, "": | ||
| return &EnvProvider{ui: ui}, nil | ||
| case ProviderTypeGHCli: | ||
| return &GHCliProvider{ui: ui}, nil | ||
| default: | ||
| return nil, errors.Errorf("unknown GitHub token provider: %s", providerType) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/alecthomas/assert/v2" | ||
| "github.com/cashapp/hermit/ui" | ||
| ) | ||
|
|
||
| func TestEnvProvider(t *testing.T) { | ||
| ui, _ := ui.NewForTesting() | ||
| provider := &EnvProvider{ui: ui} | ||
|
|
||
| t.Run("no tokens set", func(t *testing.T) { | ||
| os.Unsetenv("HERMIT_GITHUB_TOKEN") | ||
| os.Unsetenv("GITHUB_TOKEN") | ||
| token, err := provider.GetToken() | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "", token) | ||
| }) | ||
|
|
||
| t.Run("HERMIT_GITHUB_TOKEN set", func(t *testing.T) { | ||
| t.Setenv("HERMIT_GITHUB_TOKEN", "hermit-token") | ||
| t.Setenv("GITHUB_TOKEN", "") | ||
| token, err := provider.GetToken() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "hermit-token", token) | ||
| }) | ||
|
|
||
| t.Run("GITHUB_TOKEN set", func(t *testing.T) { | ||
| t.Setenv("HERMIT_GITHUB_TOKEN", "") | ||
| t.Setenv("GITHUB_TOKEN", "github-token") | ||
| token, err := provider.GetToken() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "github-token", token) | ||
| }) | ||
|
|
||
| t.Run("both tokens set, HERMIT_GITHUB_TOKEN takes precedence", func(t *testing.T) { | ||
| t.Setenv("HERMIT_GITHUB_TOKEN", "hermit-token") | ||
| t.Setenv("GITHUB_TOKEN", "github-token") | ||
| token, err := provider.GetToken() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "hermit-token", token) | ||
| }) | ||
| } | ||
|
|
||
| func TestGHCliProvider(t *testing.T) { | ||
| ui, _ := ui.NewForTesting() | ||
| provider := &GHCliProvider{ui: ui} | ||
|
|
||
| t.Run("gh not installed", func(t *testing.T) { | ||
| t.Setenv("PATH", "") | ||
|
|
||
| token, err := provider.GetToken() | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "", token) | ||
| assert.Contains(t, err.Error(), "gh CLI not found") | ||
| }) | ||
|
|
||
| t.Run("token caching", func(t *testing.T) { | ||
| // Skip if gh not installed | ||
| if _, err := exec.LookPath("gh"); err != nil { | ||
| t.Skip("gh CLI not installed") | ||
| } | ||
|
|
||
| // First call should get a real token | ||
| token1, err := provider.GetToken() | ||
| if err != nil { | ||
| t.Skip("gh auth token failed, probably not authenticated") | ||
| } | ||
| assert.NotEqual(t, "", token1) | ||
|
|
||
| // Second call should return cached token | ||
| token2, err := provider.GetToken() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, token1, token2) | ||
| }) | ||
| } | ||
|
|
||
| func TestNewProvider(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| provider string | ||
| wantType Provider | ||
| wantErrText string | ||
| }{ | ||
| { | ||
| name: "env provider", | ||
| provider: "env", | ||
| wantType: &EnvProvider{}, | ||
| }, | ||
| { | ||
| name: "empty string defaults to env", | ||
| provider: "", | ||
| wantType: &EnvProvider{}, | ||
| }, | ||
| { | ||
| name: "gh-cli provider", | ||
| provider: "gh-cli", | ||
| wantType: &GHCliProvider{}, | ||
| }, | ||
| { | ||
| name: "unknown provider", | ||
| provider: "unknown", | ||
| wantErrText: "unknown GitHub token provider: unknown", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ui, _ := ui.NewForTesting() | ||
| got, err := NewProvider(tt.provider, ui) | ||
| if tt.wantErrText != "" { | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), tt.wantErrText) | ||
| return | ||
| } | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, fmt.Sprintf("%T", tt.wantType), fmt.Sprintf("%T", got)) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.