Skip to content

Commit

Permalink
[#269] remove need for configuring username during setup
Browse files Browse the repository at this point in the history
we can use the token to look it up
  • Loading branch information
zaquestion committed Dec 27, 2018
1 parent be24c68 commit 4cea8d4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ before_script:
- ssh-add ~/.ssh/id_rsa

script:
- GO111MODULE=on make test
- make test

after_success:
- |
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ VERSION ?= $(shell git describe --long --tags)
GOURL ?= github.com/zaquestion/lab

build:
go build -ldflags "-X \"main.version=$(VERSION)\"" $(GOURL)
GO111MODULE=on go build -ldflags "-X \"main.version=$(VERSION)\"" $(GOURL)

install: build
go install $(GOURL)
GO111MODULE=on go install $(GOURL)

test:
bash -c "trap 'trap - SIGINT SIGTERM ERR; mv testdata/.git testdata/test.git; rm coverage-* 2>&1 > /dev/null; exit 1' SIGINT SIGTERM ERR; $(MAKE) internal-test"

internal-test:
rm coverage-* 2>&1 > /dev/null || true
mv testdata/test.git testdata/.git
go test -coverprofile=coverage-main.out -covermode=count -coverpkg ./... -run=$(run) $(GOURL)/cmd $(GOURL)/internal/...
GO111MODULE=on go test -coverprofile=coverage-main.out -covermode=count -coverpkg ./... -run=$(run) $(GOURL)/cmd $(GOURL)/internal/...
mv testdata/.git testdata/test.git
go get github.com/wadey/gocovmerge
gocovmerge coverage-*.out > coverage.txt && rm coverage-*.out
Expand Down
1 change: 0 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestMain(m *testing.M) {
config := c.([]map[string]interface{})[0]
lab.Init(
config["host"].(string),
config["user"].(string),
config["token"].(string))

code := m.Run()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/cobra v0.0.0-20180412120829-615425954c3b
github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/spf13/pflag v1.0.1
github.com/spf13/viper v0.0.0-20180507071007-15738813a09d
github.com/stretchr/testify v1.2.1
github.com/tcnksm/go-gitconfig v0.1.2
Expand Down
30 changes: 7 additions & 23 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"bufio"
"errors"
"fmt"
"io"
"net/url"
Expand All @@ -20,9 +19,9 @@ const defaultGitLabHost = "https://gitlab.com"
// them to the provided confpath (default: ~/.config/lab.hcl)
func New(confpath string, r io.Reader) error {
var (
reader = bufio.NewReader(r)
host, user, token string
err error
reader = bufio.NewReader(r)
host, token string
err error
)
fmt.Printf("Enter default GitLab host (default: %s): ", defaultGitLabHost)
host, err = reader.ReadString('\n')
Expand All @@ -34,16 +33,6 @@ func New(confpath string, r io.Reader) error {
host = defaultGitLabHost
}

fmt.Print("Enter default GitLab user: ")
user, err = reader.ReadString('\n')
user = strings.TrimSpace(user)
if err != nil {
return err
}
if user == "" {
return errors.New("lab.hcl config core.user must be set")
}

tokenURL, err := url.Parse(host)
if err != nil {
return err
Expand All @@ -57,7 +46,6 @@ func New(confpath string, r io.Reader) error {
}

viper.Set("core.host", host)
viper.Set("core.user", user)
viper.Set("core.token", token)
if err := viper.WriteConfigAs(confpath); err != nil {
return err
Expand All @@ -76,18 +64,14 @@ var readPassword = func() (string, error) {

// CI returns credentials suitable for use within GitLab CI or empty strings if
// none found.
func CI() (string, string, string) {
func CI() (string, string) {
ciToken := os.Getenv("CI_JOB_TOKEN")
if ciToken == "" {
return "", "", ""
return "", ""
}
ciHost := strings.TrimSuffix(os.Getenv("CI_PROJECT_URL"), os.Getenv("CI_PROJECT_PATH"))
if ciHost == "" {
return "", "", ""
}
ciUser := os.Getenv("CI_REGISTRY_USER")
if ciUser == "" {
ciUser = "gitlab-ci-token"
return "", ""
}
return ciHost, ciUser, ciToken
return ciHost, ciToken
}
4 changes: 0 additions & 4 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestNewConfig(t *testing.T) {

var buf bytes.Buffer
fmt.Fprintln(&buf, "https://gitlab.zaquestion.io")
fmt.Fprintln(&buf, "zaq")

oldreadPassword := readPassword
readPassword = func() (string, error) {
Expand Down Expand Up @@ -54,7 +53,6 @@ func TestNewConfig(t *testing.T) {
out := <-outC

assert.Contains(t, out, "Enter default GitLab host (default: https://gitlab.com): ")
assert.Contains(t, out, "Enter default GitLab user:")
assert.Contains(t, out, "Create a token here: https://gitlab.zaquestion.io/profile/personal_access_tokens\nEnter default GitLab token (scope: api):")

cfg, err := os.Open(path.Join(testconf, "lab.hcl"))
Expand All @@ -70,8 +68,6 @@ func TestNewConfig(t *testing.T) {
"host" = "https://gitlab.zaquestion.io"
"token" = "abcde12345"
"user" = "zaq"
}`)
})
os.RemoveAll(testconf)
Expand Down
8 changes: 6 additions & 2 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ func User() string {
}

// Init initializes a gitlab client for use throughout lab.
func Init(_host, _user, _token string) {
func Init(_host, _token string) {
if len(_host) > 0 && _host[len(_host)-1 : len(_host)][0] == '/' {
_host = _host[0 : len(_host)-1]
}
host = _host
user = _user
token = _token
lab = gitlab.NewClient(nil, _token)
lab.SetBaseURL(host + "/api/v4")
u, _, err := lab.Users.CurrentUser()
if err != nil {
log.Fatal(err)
}
user = u.Username
}

// Defines filepath for default GitLab templates
Expand Down
10 changes: 7 additions & 3 deletions internal/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ func TestMain(m *testing.M) {

Init(
config["host"].(string),
config["user"].(string),
config["token"].(string))
os.Exit(m.Run())
}

func TestUser(t *testing.T) {
// Should get set by Init() after TestMain()
require.Equal(t, "lab-testing", User())
}

func TestLoadGitLabTmplMR(t *testing.T) {
mrTmpl := LoadGitLabTmpl(TmplMR)
require.Equal(t, mrTmpl, "I am the default merge request template for lab")
require.Equal(t, "I am the default merge request template for lab", mrTmpl)
}

func TestLoadGitLabTmplIssue(t *testing.T) {
issueTmpl := LoadGitLabTmpl(TmplIssue)
require.Equal(t, issueTmpl, "This is the default issue template for lab")
require.Equal(t, "This is the default issue template for lab", issueTmpl)
}

func TestLint(t *testing.T) {
Expand Down
21 changes: 9 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// version gets set on releases during build by goreleaser.
var version = "master"

func loadConfig() (string, string, string) {
func loadConfig() (string, string) {
var home string
switch runtime.GOOS {
case "windows":
Expand Down Expand Up @@ -45,15 +45,15 @@ func loadConfig() (string, string, string) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

host, user, token := viper.GetString("core.host"), viper.GetString("core.user"), viper.GetString("core.token")
if host != "" && user != "" && token != "" {
return host, user, token
host, token := viper.GetString("core.host"), viper.GetString("core.token")
if host != "" && token != "" {
return host, token
}

// Attempt to auto-configure for GitLab CI
host, user, token = config.CI()
if host != "" && user != "" && token != "" {
return host, user, token
host, token = config.CI()
if host != "" && token != "" {
return host, token
}

if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok {
Expand All @@ -78,7 +78,7 @@ func loadConfig() (string, string, string) {
cfg = v
}

for _, v := range []string{"host", "user", "token"} {
for _, v := range []string{"host", "token"} {
if cv, ok := cfg[v]; !ok {
log.Println(cv)
log.Fatalf("missing config value core.%s in %s", v, viper.ConfigFileUsed())
Expand All @@ -93,13 +93,10 @@ func loadConfig() (string, string, string) {
if v := viper.GetString("core.host"); v != "" {
cfg["host"] = v
}
if v := viper.GetString("core.user"); v != "" {
cfg["user"] = v
}
if v := viper.GetString("core.token"); v != "" {
cfg["token"] = v
}
return cfg["host"].(string), cfg["user"].(string), cfg["token"].(string)
return cfg["host"].(string), cfg["token"].(string)
}

func main() {
Expand Down
1 change: 0 additions & 1 deletion testdata/lab.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"core" = {
"host" = "https://gitlab.com"
"token" = "uDz6czYV412zK-xC5mUu"
"user" = "lab-testing"
}

0 comments on commit 4cea8d4

Please sign in to comment.