Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom GitHub token environment variable name #112

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -69,10 +66,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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since now the EnvToken is used only in testing, we can remove the variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true. Let me remove it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review, removed → e5e8b67


if strings.HasPrefix(token, "$") {
token = os.Getenv(strings.TrimPrefix(token, "$"))
}

if token == "" {
return &Client{}, errors.New("github token is missing")
}
Expand Down
25 changes: 18 additions & 7 deletions notifier/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package github

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

func TestNewClient(t *testing.T) {
githubToken := os.Getenv(EnvToken)
defer func() {
os.Setenv(EnvToken, githubToken)
}()
os.Setenv(EnvToken, "")

testCases := []struct {
config Config
envToken string
Expand Down Expand Up @@ -41,12 +36,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 +68,11 @@ 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