Skip to content

Commit 9596fd5

Browse files
authored
Skip checking keyring for token in certain scenarios (#7169)
1 parent d905165 commit 9596fd5

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

internal/config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ func (c *AuthConfig) Token(hostname string) (string, string) {
140140
return token, source
141141
}
142142

143+
// HasEnvToken checks whether the current env or config contains a token
144+
func (c *AuthConfig) HasEnvToken() bool {
145+
// This will check if there are any environment variable
146+
// authentication tokens set for enterprise hosts.
147+
// Any non-github.com hostname is fine here
148+
hostname := "example.com"
149+
if c.tokenOverride != nil {
150+
token, _ := c.tokenOverride(hostname)
151+
if token != "" {
152+
return true
153+
}
154+
}
155+
token, _ := ghAuth.TokenFromEnvOrConfig(hostname)
156+
return token != ""
157+
}
158+
143159
// SetToken will override any token resolution and return the given
144160
// token and source for all calls to Token. Use for testing purposes only.
145161
func (c *AuthConfig) SetToken(token, source string) {

pkg/cmd/factory/remote_resolver.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) {
8282
}
8383

8484
if len(cachedRemotes) == 0 {
85-
// Any non-github.com hostname is fine here
86-
dummyHostname := "example.com"
8785
if isHostEnv(src) {
8886
return nil, fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable.", src)
89-
} else if v, _ := cfg.Authentication().Token(dummyHostname); v != "" {
87+
} else if cfg.Authentication().HasEnvToken() {
9088
return nil, errors.New("set the GH_HOST environment variable to specify which GitHub host to use")
9189
}
9290
return nil, errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`")

pkg/cmdutil/auth_check.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ func DisableAuthCheck(cmd *cobra.Command) {
1414
}
1515

1616
func CheckAuth(cfg config.Config) bool {
17-
// This will check if there are any environment variable
18-
// authentication tokens set for enterprise hosts.
19-
// Any non-github.com hostname is fine here
20-
dummyHostname := "example.com"
21-
token, _ := cfg.Authentication().Token(dummyHostname)
22-
if token != "" {
17+
if cfg.Authentication().HasEnvToken() {
2318
return true
2419
}
2520

pkg/cmdutil/auth_check_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func Test_CheckAuth(t *testing.T) {
3636
},
3737
expected: true,
3838
},
39+
{
40+
name: "enterprise token",
41+
cfgStubs: func(c *config.ConfigMock) {
42+
t.Setenv("GH_ENTERPRISE_TOKEN", "token")
43+
},
44+
expected: true,
45+
},
3946
}
4047

4148
for _, tt := range tests {

0 commit comments

Comments
 (0)