Skip to content

Commit

Permalink
Support custom env var GitHub Token name
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <[email protected]>
  • Loading branch information
KeisukeYamashita committed Feb 18, 2022
1 parent 3fee50b commit ec68a58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 4 additions & 3 deletions notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 17 additions & 1 deletion notifier/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -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{},
Expand All @@ -61,7 +74,10 @@ 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)
}
_, err := NewClient(testCase.config)
if err == nil {
continue
Expand Down

0 comments on commit ec68a58

Please sign in to comment.