From 9596fd5368cdbd30d08555266890a2312e22eba9 Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Fri, 17 Mar 2023 22:59:43 +0100 Subject: [PATCH] Skip checking keyring for token in certain scenarios (#7169) --- internal/config/config.go | 16 ++++++++++++++++ pkg/cmd/factory/remote_resolver.go | 4 +--- pkg/cmdutil/auth_check.go | 7 +------ pkg/cmdutil/auth_check_test.go | 7 +++++++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index ce5796c9e..96d6b5ed0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -140,6 +140,22 @@ func (c *AuthConfig) Token(hostname string) (string, string) { return token, source } +// HasEnvToken checks whether the current env or config contains a token +func (c *AuthConfig) HasEnvToken() bool { + // This will check if there are any environment variable + // authentication tokens set for enterprise hosts. + // Any non-github.com hostname is fine here + hostname := "example.com" + if c.tokenOverride != nil { + token, _ := c.tokenOverride(hostname) + if token != "" { + return true + } + } + token, _ := ghAuth.TokenFromEnvOrConfig(hostname) + return token != "" +} + // SetToken will override any token resolution and return the given // token and source for all calls to Token. Use for testing purposes only. func (c *AuthConfig) SetToken(token, source string) { diff --git a/pkg/cmd/factory/remote_resolver.go b/pkg/cmd/factory/remote_resolver.go index a672ba5e6..08ff297c6 100644 --- a/pkg/cmd/factory/remote_resolver.go +++ b/pkg/cmd/factory/remote_resolver.go @@ -82,11 +82,9 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) { } if len(cachedRemotes) == 0 { - // Any non-github.com hostname is fine here - dummyHostname := "example.com" if isHostEnv(src) { 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) - } else if v, _ := cfg.Authentication().Token(dummyHostname); v != "" { + } else if cfg.Authentication().HasEnvToken() { return nil, errors.New("set the GH_HOST environment variable to specify which GitHub host to use") } 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`") diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index ea18d1502..56ebb0c4d 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -14,12 +14,7 @@ func DisableAuthCheck(cmd *cobra.Command) { } func CheckAuth(cfg config.Config) bool { - // This will check if there are any environment variable - // authentication tokens set for enterprise hosts. - // Any non-github.com hostname is fine here - dummyHostname := "example.com" - token, _ := cfg.Authentication().Token(dummyHostname) - if token != "" { + if cfg.Authentication().HasEnvToken() { return true } diff --git a/pkg/cmdutil/auth_check_test.go b/pkg/cmdutil/auth_check_test.go index 6bef0d413..fa8bd80e2 100644 --- a/pkg/cmdutil/auth_check_test.go +++ b/pkg/cmdutil/auth_check_test.go @@ -36,6 +36,13 @@ func Test_CheckAuth(t *testing.T) { }, expected: true, }, + { + name: "enterprise token", + cfgStubs: func(c *config.ConfigMock) { + t.Setenv("GH_ENTERPRISE_TOKEN", "token") + }, + expected: true, + }, } for _, tt := range tests {