From f30ff23d820aec029f9bae6c8d1f375f38da64a3 Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Thu, 3 Aug 2023 21:31:01 -0700 Subject: [PATCH] Clear all possible token env vars for token accessor test. (#3347) Signed-off-by: Spencer Schrock --- clients/githubrepo/roundtripper/tokens/accessor.go | 8 +++++--- clients/githubrepo/roundtripper/tokens/accessor_test.go | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/clients/githubrepo/roundtripper/tokens/accessor.go b/clients/githubrepo/roundtripper/tokens/accessor.go index 12f86ec734b..bd9a779458a 100644 --- a/clients/githubrepo/roundtripper/tokens/accessor.go +++ b/clients/githubrepo/roundtripper/tokens/accessor.go @@ -23,6 +23,9 @@ import ( // githubAuthServer is the RPC URL for the token server. const githubAuthServer = "GITHUB_AUTH_SERVER" +// env variables from which GitHub auth tokens are read, in order of precedence. +var githubAuthTokenEnvVars = []string{"GITHUB_AUTH_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "GH_AUTH_TOKEN"} + // TokenAccessor interface defines a `retrieve-once` data structure. // Implementations of this interface must be thread-safe. type TokenAccessor interface { @@ -31,8 +34,7 @@ type TokenAccessor interface { } func readGitHubTokens() (string, bool) { - githubAuthTokens := []string{"GITHUB_AUTH_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "GH_AUTH_TOKEN"} - for _, name := range githubAuthTokens { + for _, name := range githubAuthTokenEnvVars { if token, exists := os.LookupEnv(name); exists && token != "" { return token, exists } @@ -45,7 +47,7 @@ func MakeTokenAccessor() TokenAccessor { if value, exists := readGitHubTokens(); exists { return makeRoundRobinAccessor(strings.Split(value, ",")) } - if value, exists := os.LookupEnv(githubAuthServer); exists { + if value, exists := os.LookupEnv(githubAuthServer); exists && value != "" { return makeRPCAccessor(value) } return nil diff --git a/clients/githubrepo/roundtripper/tokens/accessor_test.go b/clients/githubrepo/roundtripper/tokens/accessor_test.go index 9904a39fa66..1736eb5a4d7 100644 --- a/clients/githubrepo/roundtripper/tokens/accessor_test.go +++ b/clients/githubrepo/roundtripper/tokens/accessor_test.go @@ -40,16 +40,17 @@ func TestMakeTokenAccessor(t *testing.T) { useServer: true, }, } - t.Setenv("GITHUB_AUTH_TOKEN", "") - t.Setenv("GITHUB_TOKEN", "") + // clear all env variables devs may have defined, or the test will fail locally + for _, envVar := range githubAuthTokenEnvVars { + t.Setenv(envVar, "") + } + t.Setenv(githubAuthServer, "") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { switch { case tt.useGitHubToken: - t.Helper() testToken(t) case tt.useServer: - t.Helper() testServer(t) default: got := MakeTokenAccessor()