Skip to content

Commit

Permalink
Merge pull request #19 from caioeverest/issue-14/add-set-considering-…
Browse files Browse the repository at this point in the history
…config-file

Merged by gomerge CLI.
  • Loading branch information
Cian911 authored Oct 16, 2021
2 parents d5b51b2 + fc6df32 commit 66d4927
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/cli/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var (
mergeMethod = "merge"
)

const (
TokenEnvVar = "GITHUB_TOKEN"
)

// TODO: Refactor NewCommnd
func NewCommand() (c *cobra.Command) {
c = &cobra.Command{
Expand All @@ -33,10 +37,10 @@ func NewCommand() (c *cobra.Command) {
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
orgRepo := viper.GetString("repo")
token := viper.GetString("token")
configFile := viper.GetString("config")
approveOnly = viper.GetBool("approve")
mergeMethod := viper.GetString("merge-method")
flagToken := viper.GetString("token")

if len(configFile) > 0 {
utils.ReadConfigFile(configFile)
Expand All @@ -46,6 +50,12 @@ func NewCommand() (c *cobra.Command) {
if !configPresent && len(orgRepo) <= 0 {
log.Fatal("You must pass either a config file or repository as argument to continue.")
}
configToken := viper.GetString("token")

token, err := getToken(flagToken, configToken)
if err != nil {
log.Fatal(err)
}

ghClient := gitclient.Client(token, ctx)
pullRequestsArray := []*github.PullRequest{}
Expand Down Expand Up @@ -183,6 +193,21 @@ func parsePrId(prId string) []string {
return str
}

func getToken(flag, config string) (str string, err error) {
if flag != str {
return flag, nil
}
if config != str {
return config, nil
}
if env, ok := os.LookupEnv(TokenEnvVar); ok {
return env, nil
}

err = fmt.Errorf("you must pass a github token to continue")
return
}

func selectPrIds(prIds []string) (*survey.MultiSelect, []string) {
selectedIds := []string{}
msg := ""
Expand Down
72 changes: 72 additions & 0 deletions pkg/cli/list/list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package list

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -81,3 +82,74 @@ func TestFormatTable(t *testing.T) {
assert.Equal(t, got, want)
})
}

func TestListGetToken(t *testing.T) {
var (
flag = "flag@token"
config = "config@token"
envVar = "env@token"
)

t.Run("When a given token is set by flag, it should return token as the flag value", func(t *testing.T) {
got, err := getToken(flag, "")
want := flag
assert.Nil(t, err)
assert.Equal(t, want, got)
})

t.Run("When a given token is set by config, it should return token as defined on the configuration file", func(t *testing.T) {
got, err := getToken("", config)
want := config
assert.Nil(t, err)
assert.Equal(t, want, got)
})

t.Run("When a given token is set by environment variable, it should return token as defined on the environment", func(t *testing.T) {
os.Setenv(TokenEnvVar, envVar)
got, err := getToken("", "")
want := envVar
assert.Nil(t, err)
assert.Equal(t, want, got)
os.Unsetenv(TokenEnvVar)
})

t.Run("When a given token is set on flag and config file, it should return the value set on flag", func(t *testing.T) {
got, err := getToken(flag, config)
want := flag
assert.Nil(t, err)
assert.Equal(t, want, got)
})

t.Run("When a given token is set on flag and environment, it should return the value set on the flag", func(t *testing.T) {
os.Setenv(TokenEnvVar, envVar)
got, err := getToken(flag, "")
want := flag
assert.Nil(t, err)
assert.Equal(t, want, got)
os.Unsetenv(TokenEnvVar)
})

t.Run("When a given token is set on config file and environment, it should return the value set on the config file", func(t *testing.T) {
os.Setenv(TokenEnvVar, envVar)
got, err := getToken("", config)
want := config
assert.Nil(t, err)
assert.Equal(t, want, got)
os.Unsetenv(TokenEnvVar)
})

t.Run("When a given token is set on flag, config file, and environment, it should return the value set on flag", func(t *testing.T) {
os.Setenv(TokenEnvVar, envVar)
got, err := getToken(flag, config)
want := flag
assert.Nil(t, err)
assert.Equal(t, want, got)
os.Unsetenv(TokenEnvVar)
})

t.Run("When no token is passed should return error", func(t *testing.T) {
got, err := getToken("", "")
assert.Equal(t, "", got)
assert.Error(t, err)
})
}
1 change: 1 addition & 0 deletions pkg/utils/config_test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
organization: Cian911
token: 1234test@gh*token
repositories:
- pr-test
- syncwave
7 changes: 7 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func TestReadConfigFile(t *testing.T) {
if got != want {
t.Errorf("got %v want %v", got, want)
}

got1 := viper.Get("token")
want1 := "1234test@gh*token"

if got1 != want1 {
t.Errorf("got %v want %v", got, want)
}
})

t.Run("It extracts filename and ext from path", func(t *testing.T) {
Expand Down

0 comments on commit 66d4927

Please sign in to comment.