diff --git a/.drone.yml b/.drone.yml index 2ee6ec2..951ba95 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,7 +17,7 @@ steps: environment: GOPROXY: https://goproxy.cn commands: - - go test -race + - make test - go build - name: check diff --git a/cmd/cmd.go b/cmd/cmd.go index 17e4d2e..38b1e57 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2,11 +2,13 @@ package cmd import ( "code.gitea.io/sdk/gitea" + "errors" "fmt" "gitea.com/jolheiser/sip/modules/config" "gitea.com/jolheiser/sip/modules/git" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver/color" "sync" ) @@ -56,6 +58,9 @@ func requireToken(ctx *cli.Context) (string, error) { if ctx.IsSet("token") { return getToken(ctx.String("token")), nil } + if len(config.Tokens) == 0 { + return "", errors.New(color.Error.Wrap("No tokens found! Add one with #{sip token create}", color.New(color.FgMagenta))) + } tokenMap := make(map[string]config.Token) opts := make([]string, len(config.Tokens)) for idx, token := range config.Tokens { @@ -87,11 +92,19 @@ func getClient(ctx *cli.Context) *gitea.Client { return gitea.NewClient(ctx.String("url"), getToken(ctx.String("token"))) } +func defRemote(remote, def string) string { + if remote == "" { + return def + } + return remote +} + func getUpstreamRepo() []string { upstreamOnce.Do(func() { - upstreamRepo = git.GetRepo(config.Upstream) - if upstreamRepo == nil { - upstreamRepo = git.GetRepo(config.Origin) + var err error + upstreamRepo, err = git.GetRepo(defRemote(config.Upstream, "upstream")) + if err != nil { + upstreamRepo = getOriginRepo() } }) return upstreamRepo @@ -99,7 +112,11 @@ func getUpstreamRepo() []string { func getOriginRepo() []string { originOnce.Do(func() { - originRepo = git.GetRepo(config.Origin) + var err error + originRepo, err = git.GetRepo(defRemote(config.Origin, "origin")) + if err != nil { + originRepo = []string{"https://gitea.com", "jolheiser", "sip"} + } }) return originRepo } diff --git a/cmd/config.go b/cmd/config.go index 50ae7e3..58477f3 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,10 +1,10 @@ package cmd import ( - "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/sip/modules/config" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" ) var Config = cli.Command{ diff --git a/cmd/issues.go b/cmd/issues.go index 9bd6407..55576dd 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -4,12 +4,12 @@ import ( "code.gitea.io/sdk/gitea" "errors" "fmt" - "gitea.com/jolheiser/beaver" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/markdown" "gitea.com/jolheiser/sip/modules/sdk" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" + "go.jolheiser.com/beaver/color" "strconv" "strings" ) @@ -115,10 +115,8 @@ func queryIssues(ctx *cli.Context, client *gitea.Client, pulls bool) ([]*gitea.I filtered := make([]*gitea.Issue, 0) for _, issue := range issues { - if pulls { - if issue.PullRequest != nil { - filtered = append(filtered, issue) - } + if (pulls && issue.PullRequest == nil) || + (!pulls && issue.PullRequest != nil) { continue } filtered = append(filtered, issue) diff --git a/cmd/issues_create.go b/cmd/issues_create.go index 9f3a2e8..d2c6977 100644 --- a/cmd/issues_create.go +++ b/cmd/issues_create.go @@ -3,16 +3,17 @@ package cmd import ( "code.gitea.io/sdk/gitea" "fmt" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/markdown" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver/color" ) var IssuesCreate = cli.Command{ - Name: "create", - Usage: "Create a new issue", - Action: doIssueCreate, + Name: "create", + Aliases: []string{"new"}, + Usage: "Create a new issue", + Action: doIssueCreate, } func doIssueCreate(ctx *cli.Context) error { diff --git a/cmd/pulls_checkout.go b/cmd/pulls_checkout.go index 7b65634..1e55d04 100644 --- a/cmd/pulls_checkout.go +++ b/cmd/pulls_checkout.go @@ -4,11 +4,11 @@ import ( "code.gitea.io/sdk/gitea" "errors" "fmt" - "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/sip/modules/config" "github.com/AlecAivazis/survey/v2" "github.com/huandu/xstrings" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" "os" "os/exec" "strconv" diff --git a/cmd/pulls_create.go b/cmd/pulls_create.go index 5ca71d5..bd5d70f 100644 --- a/cmd/pulls_create.go +++ b/cmd/pulls_create.go @@ -3,17 +3,18 @@ package cmd import ( "code.gitea.io/sdk/gitea" "fmt" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/git" "gitea.com/jolheiser/sip/modules/markdown" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver/color" ) var PullsCreate = cli.Command{ - Name: "create", - Usage: "Create a new pull request", - Action: doPullCreate, + Name: "create", + Aliases: []string{"new"}, + Usage: "Create a new pull request", + Action: doPullCreate, } func doPullCreate(ctx *cli.Context) error { @@ -48,10 +49,14 @@ func doPullCreate(ctx *cli.Context) error { heads := make([]string, len(origins)) defOrigin := origins[0].Name for idx, origin := range origins { - if origin.Name == git.Branch() { - defOrigin = getOriginRepo()[1] + ":" + origin.Name + originName := origin.Name + if ctx.String("owner") != getOriginRepo()[1] { + originName = getOriginRepo()[1] + ":" + originName } - heads[idx] = getOriginRepo()[1] + ":" + origin.Name + if origin.Name == git.Branch() { + defOrigin = originName + } + heads[idx] = originName } var confirmed bool diff --git a/cmd/pulls_status.go b/cmd/pulls_status.go index 4d4e396..b4348b1 100644 --- a/cmd/pulls_status.go +++ b/cmd/pulls_status.go @@ -3,10 +3,10 @@ package cmd import ( "code.gitea.io/sdk/gitea" "fmt" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/git" "gitea.com/jolheiser/sip/modules/sdk" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver/color" "strconv" ) diff --git a/cmd/repo.go b/cmd/repo.go index e8890a4..5122b64 100644 --- a/cmd/repo.go +++ b/cmd/repo.go @@ -2,10 +2,10 @@ package cmd import ( "code.gitea.io/sdk/gitea" - "gitea.com/jolheiser/beaver" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/sdk" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" + "go.jolheiser.com/beaver/color" "strconv" ) diff --git a/cmd/token.go b/cmd/token.go index f79e0d1..14e2aa1 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -1,10 +1,10 @@ package cmd import ( - "gitea.com/jolheiser/beaver" - "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/config" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" + "go.jolheiser.com/beaver/color" ) var Tokens = cli.Command{ @@ -20,7 +20,7 @@ var Tokens = cli.Command{ func doTokenList(ctx *cli.Context) error { if len(config.Tokens) == 0 { - beaver.Errorf("No tokens found! Add one with %s", color.New(color.FgMagenta).Format("sip token create")) + beaver.Errorf("No tokens found! Add one with %s", color.FgMagenta.Format("sip token create")) } for idx, token := range config.Tokens { beaver.Infof("%d. %s (%s)", idx+1, token.Name, token.URL) diff --git a/cmd/token_add.go b/cmd/token_add.go index b937875..02d2f60 100644 --- a/cmd/token_add.go +++ b/cmd/token_add.go @@ -3,10 +3,10 @@ package cmd import ( "code.gitea.io/sdk/gitea" "errors" - "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/sip/modules/config" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" ) var TokensAdd = cli.Command{ diff --git a/cmd/token_remove.go b/cmd/token_remove.go index 3a43c3a..faad8ce 100644 --- a/cmd/token_remove.go +++ b/cmd/token_remove.go @@ -2,10 +2,10 @@ package cmd import ( "fmt" - "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/sip/modules/config" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" ) var TokensRemove = cli.Command{ diff --git a/go.mod b/go.mod index a0045ad..c23face 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.13 require ( code.gitea.io/sdk/gitea v0.11.0 - gitea.com/jolheiser/beaver v1.0.0 github.com/AlecAivazis/survey/v2 v2.0.5 github.com/BurntSushi/toml v0.3.1 github.com/charmbracelet/glamour v0.1.0 @@ -15,7 +14,8 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/sergi/go-diff v1.1.0 // indirect github.com/urfave/cli/v2 v2.1.1 + go.jolheiser.com/beaver v1.0.2 golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 // indirect golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect - golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect + golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect ) diff --git a/go.sum b/go.sum index 56bdc4a..05d5532 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ code.gitea.io/sdk/gitea v0.11.0 h1:XgZtmImZsjMC+Z1WBfO6bYTCOJiGp+7w0HKmfhTwytw= code.gitea.io/sdk/gitea v0.11.0/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY= -gitea.com/jolheiser/beaver v1.0.0 h1:IfNMhp7+DUaM0kaNwho4RWfuebCsa8A/kxtZBngFjHk= -gitea.com/jolheiser/beaver v1.0.0/go.mod h1:2mUGl6ZGKY/Y9u36iR4bqOPrHhr4C22cxkR8ei2G06I= +go.jolheiser.com/beaver v1.0.0 h1:IfNMhp7+DUaM0kaNwho4RWfuebCsa8A/kxtZBngFjHk= +go.jolheiser.com/beaver v1.0.0/go.mod h1:2mUGl6ZGKY/Y9u36iR4bqOPrHhr4C22cxkR8ei2G06I= github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34CltJjOoPM= github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= @@ -111,6 +111,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/yuin/goldmark v1.1.19 h1:0s2/60x0XsFCXHeFut+F3azDVAAyIMyUfJRbRexiTYs= github.com/yuin/goldmark v1.1.19/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk= +go.jolheiser.com/beaver v1.0.2/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg= @@ -127,6 +129,8 @@ golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= diff --git a/main.go b/main.go index deaf50c..f9d0b02 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( - "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/sip/cmd" "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" "os" ) diff --git a/modules/config/config.go b/modules/config/config.go index 257e358..5079002 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -2,9 +2,9 @@ package config import ( "fmt" - "gitea.com/jolheiser/beaver" "github.com/BurntSushi/toml" "github.com/mitchellh/go-homedir" + "go.jolheiser.com/beaver" "os" "path" ) diff --git a/modules/git/git.go b/modules/git/git.go index 41d0b7b..5ab74fc 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -6,11 +6,11 @@ import ( ) // GetRepo returns a repositories parts -func GetRepo(remoteName string) []string { +func GetRepo(remoteName string) ([]string, error) { cmd := exec.Command("git", "remote", "get-url", remoteName) out, err := cmd.Output() if err != nil { - return []string{"https://gitea.com", "jolheiser", "sip"} + return nil, err } remote := strings.TrimSpace(string(out)) @@ -21,14 +21,14 @@ func GetRepo(remoteName string) []string { parts := strings.Split(remote, ":") domain := "https://" + parts[0] ownerRepo := strings.Split(parts[1], "/") - return []string{domain, ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")} + return []string{domain, ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")}, nil } // HTTP(S) parts := strings.Split(remote, "/") domain := parts[:len(parts)-2] ownerRepo := parts[len(parts)-2:] - return []string{strings.Join(domain, "/"), ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")} + return []string{strings.Join(domain, "/"), ownerRepo[0], strings.TrimSuffix(ownerRepo[1], ".git")}, nil } // Branches returns current branch