From 13d4a8b5d7c530a29c5ac687b32b5be22b2b05c7 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 18 Feb 2022 21:51:16 +0900 Subject: [PATCH 1/2] Support custom env var GitHub Token name Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- notifier/github/client.go | 7 ++++--- notifier/github/client_test.go | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/notifier/github/client.go b/notifier/github/client.go index 1c62bca..ef1f288 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -69,10 +69,11 @@ type service struct { // NewClient returns Client initialized with Config func NewClient(cfg Config) (*Client, error) { token := cfg.Token - token = strings.TrimPrefix(token, "$") - if token == EnvToken { - token = os.Getenv(EnvToken) + + if strings.HasPrefix(token, "$") { + token = os.Getenv(strings.TrimPrefix(token, "$")) } + if token == "" { return &Client{}, errors.New("github token is missing") } diff --git a/notifier/github/client_test.go b/notifier/github/client_test.go index e84a117..d59c48c 100644 --- a/notifier/github/client_test.go +++ b/notifier/github/client_test.go @@ -2,6 +2,7 @@ package github import ( "os" + "strings" "testing" ) @@ -41,12 +42,24 @@ func TestNewClient(t *testing.T) { envToken: "", expect: "github token is missing", }, + { + // specify via env but not to be set env (part 3) + config: Config{Token: "$TFNOTIFY_GITHUB_TOKEN"}, + envToken: "", + expect: "github token is missing", + }, { // specify via env (part 2) config: Config{Token: "$GITHUB_TOKEN"}, envToken: "abcdefg", expect: "", }, + { + // specify via env (part 3) + config: Config{Token: "$TFNOTIFY_GITHUB_TOKEN"}, + envToken: "abcdefg", + expect: "", + }, { // no specification (part 1) config: Config{}, @@ -61,7 +74,13 @@ func TestNewClient(t *testing.T) { }, } for _, testCase := range testCases { - os.Setenv(EnvToken, testCase.envToken) + if strings.HasPrefix(testCase.config.Token, "$") { + key := strings.TrimPrefix(testCase.config.Token, "$") + os.Setenv(key, testCase.envToken) + } else { + os.Setenv(EnvToken, testCase.envToken) + } + _, err := NewClient(testCase.config) if err == nil { continue From e5e8b67178b2286088e63551c7bfa35b9d30006c Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Sun, 20 Feb 2022 14:48:51 +0900 Subject: [PATCH 2/2] Remove envToken Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- notifier/github/client.go | 3 --- notifier/github/client_test.go | 8 -------- 2 files changed, 11 deletions(-) diff --git a/notifier/github/client.go b/notifier/github/client.go index ef1f288..094e23d 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -10,9 +10,6 @@ import ( "golang.org/x/oauth2" ) -// EnvToken is GitHub API Token -const EnvToken = "GITHUB_TOKEN" - // EnvBaseURL is GitHub base URL. This can be set to a domain endpoint to use with GitHub Enterprise. const EnvBaseURL = "GITHUB_BASE_URL" diff --git a/notifier/github/client_test.go b/notifier/github/client_test.go index d59c48c..79036d3 100644 --- a/notifier/github/client_test.go +++ b/notifier/github/client_test.go @@ -7,12 +7,6 @@ import ( ) func TestNewClient(t *testing.T) { - githubToken := os.Getenv(EnvToken) - defer func() { - os.Setenv(EnvToken, githubToken) - }() - os.Setenv(EnvToken, "") - testCases := []struct { config Config envToken string @@ -77,8 +71,6 @@ func TestNewClient(t *testing.T) { if strings.HasPrefix(testCase.config.Token, "$") { key := strings.TrimPrefix(testCase.config.Token, "$") os.Setenv(key, testCase.envToken) - } else { - os.Setenv(EnvToken, testCase.envToken) } _, err := NewClient(testCase.config)