Skip to content

Commit

Permalink
add parseArgsRemoteRef - base unification + test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmdo committed Jul 6, 2020
1 parent 468bb69 commit 496b0f3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 70 deletions.
28 changes: 7 additions & 21 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand All @@ -24,29 +23,16 @@ var ciStatusCmd = &cobra.Command{
lab ci status --wait`,
RunE: nil,
Run: func(cmd *cobra.Command, args []string) {
var err error
branchName, err = git.CurrentBranch()
if err != nil {
log.Fatal(err)
}

if len(args) > 1 {
branchName = args[1]
}
remote := determineSourceRemote(branchName)
if len(args) > 0 {
ok, err := git.IsRemote(args[0])
if err != nil || !ok {
log.Fatal(args[0], " is not a remote:", err)
}
remote = args[0]
}
rn, err := git.PathWithNameSpace(remote)
var (
rn string
err error
)
rn, refName, err = parseArgsRemoteRef(args)
if err != nil {
log.Fatal(err)
}
pid := rn
commit, err := lab.GetCommit(pid, branchName)
commit, err := lab.GetCommit(pid, refName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -73,7 +59,7 @@ lab ci status --wait`,
jobs = latestJobs(jobs)

if len(jobs) == 0 {
log.Fatal("no CI jobs found for branch ", branch, " on remote ", remote)
log.Fatal("no CI jobs found for branch ", refName, " on remote ", rn)
return
}

Expand Down
34 changes: 12 additions & 22 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,37 @@ var ciTraceCmd = &cobra.Command{
Long: `If a job is not specified the latest running job or last job in the pipeline is used`,
Run: func(cmd *cobra.Command, args []string) {
var (
remote string
rn string
jobName string
err error
)

branchName, err = git.CurrentBranch()
rn, jobName, err = parseArgsRemoteString(args)
if err != nil {
log.Fatal(err)
}
if len(args) > 1 {
jobName = args[1]
if strings.Contains(args[1], ":") {
ps := strings.Split(args[1], ":")
branchName, jobName = ps[0], ps[1]
}
}
remote = determineSourceRemote(branchName)
if len(args) > 0 {
ok, err := git.IsRemote(args[0])
if err != nil || !ok {
log.Fatal(args[0], " is not a remote:", err)

if strings.Contains(jobName, ":") {
ps := strings.Split(jobName, ":")
refName, jobName = ps[0], ps[1]
} else {
refName, err = git.CurrentBranch()
if err != nil {
log.Fatal(err)
}
remote = args[0]
}

rn, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}
project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}
projectID = project.ID
commit, err := lab.GetCommit(projectID, branchName)
commit, err := lab.GetCommit(projectID, refName)
if err != nil {
log.Fatal(err)
}
commitSHA = commit.ID

err = doTrace(context.Background(), os.Stdout, project.ID, commitSHA, jobName)
err = doTrace(context.Background(), os.Stdout, projectID, commitSHA, jobName)
if err != nil {
log.Fatal(err)
}
Expand Down
35 changes: 8 additions & 27 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import (
"github.com/lunixbochs/vtclean"
gitlab "github.com/xanzy/go-gitlab"

"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var (
projectID int
branchName string
commitSHA string
projectID int
refName string
commitSHA string
)

// ciViewCmd represents the ci command
Expand All @@ -47,39 +46,21 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`,
a := tview.NewApplication()
defer recoverPanic(a)
var (
remote string
err error
rn string
err error
)
branchName, err = git.CurrentBranch()
if err != nil {
log.Fatal(err)
}

if len(args) > 1 {
// branchName may also be a tag
branchName = args[1]
}
remote = determineSourceRemote(branchName)
if len(args) > 0 {
ok, err := git.IsRemote(args[0])
if err != nil || !ok {
log.Fatal(args[0], " is not a remote:", err)
}
remote = args[0]
}

// See if we're in a git repo or if global is set to determine
// if this should be a personal snippet
rn, err := git.PathWithNameSpace(remote)
rn, refName, err = parseArgsRemoteRef(args)
if err != nil {
log.Fatal(err)
}

project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}
projectID = project.ID
commit, err := lab.GetCommit(projectID, branchName)
commit, err := lab.GetCommit(projectID, refName)
if err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ func parseArgsRemoteString(args []string) (string, string, error) {
return remote, str, nil
}

// parseArgsRemoteRef returns a remote name and a ref name (default: current branch).
// Like parseArgsRemoteString where second argument defaults to current branch.
func parseArgsRemoteRef(args []string) (string, string, error) {
rn, name, err := parseArgsRemoteString(args)
if err != nil {
return "", "", err
}
if name == "" {
name, err = git.CurrentBranch()
if err != nil {
return "", "", err
}
}

return rn, name, nil
}

var (
// Will be updated to upstream in Execute() if "upstream" remote exists
forkedFromRemote = "origin"
Expand Down
67 changes: 67 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,73 @@ func Test_parseArgsRemoteString(t *testing.T) {
}
}

func Test_parseArgsRemoteRef(t *testing.T) {
tests := []struct {
Name string
Args []string
ExpectedRemote string
ExpectedRef string
ExpectedErr string
}{
{
Name: "No Args",
Args: nil,
ExpectedRemote: "zaquestion/test",
ExpectedRef: "master",
ExpectedErr: "",
},
{
Name: "1 arg remote",
Args: []string{"lab-testing"},
ExpectedRemote: "lab-testing/test",
ExpectedRef: "master",
ExpectedErr: "",
},
{
Name: "1 arg non remote",
Args: []string{"foo123"},
ExpectedRemote: "zaquestion/test",
ExpectedRef: "foo123",
ExpectedErr: "",
},
{
Name: "1 arg num",
Args: []string{"100"},
ExpectedRemote: "zaquestion/test",
ExpectedRef: "100",
ExpectedErr: "",
},
{
Name: "2 arg remote and string",
Args: []string{"origin", "foo123"},
ExpectedRemote: "zaquestion/test",
ExpectedRef: "foo123",
ExpectedErr: "",
},
{
Name: "2 arg invalid remote and string",
Args: []string{"foo", "string123"},
ExpectedRemote: "",
ExpectedRef: "",
ExpectedErr: "foo is not a valid remote",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
r, s, err := parseArgsRemoteRef(test.Args)
if test.ExpectedErr != "" {
assert.EqualError(t, err, test.ExpectedErr)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.ExpectedRemote, r)
assert.Equal(t, test.ExpectedRef, s)
})
}
}

type fatalLogger interface {
Fatal(...interface{})
}
Expand Down

0 comments on commit 496b0f3

Please sign in to comment.