Skip to content

Commit

Permalink
ci: Use better default branch and remote for fetching jobs
Browse files Browse the repository at this point in the history
We always want to use the remote branch name rather than the local name,
in case the two differ.

And as non-detached (read: non-merge-request) pipelines are executed on
the source remote, we should default to that instead of the target remote.
  • Loading branch information
fmuellner committed Dec 10, 2020
1 parent d939764 commit d6b8490
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 40 deletions.
9 changes: 1 addition & 8 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand All @@ -33,16 +32,10 @@ lab ci status --wait`,
err error
)

rn, refName, err = parseArgsRemoteAndProject(args)
rn, refName, err = parseArgsRemoteAndBranch(args)
if err != nil {
log.Fatal(err)
}
if refName == "" {
refName, err = git.CurrentBranch()
if err != nil {
log.Fatal(err)
}
}

pid := rn
commit, err := lab.GetCommit(pid, refName)
Expand Down
40 changes: 30 additions & 10 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,12 @@ var ciTraceCmd = &cobra.Command{
jobName string
err error
)
rn, jobName, err = parseArgsRemoteAndProject(args)
jobName, branchArgs, err := filterJobArg(args)
if err != nil {
log.Fatal(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)
}
}
rn, refName, err = parseArgsRemoteAndBranch(branchArgs)

project, err := lab.FindProject(rn)
if err != nil {
Expand Down Expand Up @@ -108,6 +100,34 @@ func doTrace(ctx context.Context, w io.Writer, pid interface{}, sha, name string
return nil
}

func filterJobArg(args []string) (string, []string, error) {
branchArgs := []string{}
jobName := ""

if len(args) == 1 {
ok, err := git.IsRemote(args[0])
if err != nil {
return "", branchArgs, err
}
if ok {
branchArgs = append(branchArgs, args[0])
} else {
jobName = args[0]
}
} else if len(args) > 1 {
branchArgs = append(branchArgs, args[0])
jobName = args[1]
}

if strings.Contains(jobName, ":") {
ps := strings.Split(jobName, ":")
branchArgs = append(branchArgs, ps[0])
jobName = ps[1]
}

return jobName, branchArgs, nil
}

func init() {
ciCmd.AddCommand(ciTraceCmd)
carapace.Gen(ciTraceCmd).PositionalCompletion(
Expand Down
9 changes: 1 addition & 8 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
gitlab "github.com/xanzy/go-gitlab"

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

Expand Down Expand Up @@ -54,16 +53,10 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`,
err error
)

rn, refName, err = parseArgsRemoteAndProject(args)
rn, refName, err = parseArgsRemoteAndBranch(args)
if err != nil {
log.Fatal(err)
}
if refName == "" {
refName, err = git.CurrentBranch()
if err != nil {
log.Fatal(err)
}
}

project, err := lab.FindProject(rn)
if err != nil {
Expand Down
75 changes: 61 additions & 14 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,56 @@ func parseArgsRemoteAndProject(args []string) (string, string, error) {
return "", "", nil
}

remote, str := forkedFromRemote, ""
remote, str, err := parseArgsRemoteAndString(args)
if err != nil {
return "", "", nil
}

if len(args) == 1 {
ok, err := git.IsRemote(args[0])
if err != nil {
return "", "", err
}
if ok {
remote = args[0]
} else {
str = args[0]
}
} else if len(args) > 1 {
remote, str = args[0], args[1]
if remote == "" {
remote = forkedFromRemote
}

remote, err := getRemoteName(remote)
remote, err = getRemoteName(remote)
if err != nil {
return "", "", err
}
return remote, str, nil
}

// parseArgsRemoteAndBranch is used by commands to parse command line
// arguments. This function returns a remote name and a branch name.
// If no branch name is given, the function returns the upstream of
// the current branch and the corresponding remote.
func parseArgsRemoteAndBranch(args []string) (string, string, error) {
if !git.InsideGitRepo() {
return "", "", nil
}

remote, branch, err := parseArgsRemoteAndString(args)
if branch == "" && err == nil {
branch, err = git.CurrentBranch()
}

if err != nil {
return "", "", err
}

if remote == "" {
remote = determineSourceRemote(branch)
}

remoteBranch, _ := git.UpstreamBranch(branch)
if remoteBranch != "" {
branch = remoteBranch
}

remote, err = getRemoteName(remote)
if err != nil {
return "", "", err
}
return remote, branch, nil
}

func getRemoteName(remote string) (string, error) {
ok, err := git.IsRemote(remote)
if err != nil {
Expand Down Expand Up @@ -205,6 +232,26 @@ func parseArgsStringAndID(args []string) (string, int64, error) {
return "", 0, nil
}

func parseArgsRemoteAndString(args []string) (string, string, error) {
remote, str := "", ""

if len(args) == 1 {
ok, err := git.IsRemote(args[0])
if err != nil {
return "", "", err
}
if ok {
remote = args[0]
} else {
str = args[0]
}
} else if len(args) > 1 {
remote, str = args[0], args[1]
}

return remote, str, nil
}

// parseArgsWithGitBranchMR returns a remote name and a number if parsed.
// If no number is specified, the MR id associated with the current
// branch is returned.
Expand Down

0 comments on commit d6b8490

Please sign in to comment.