Skip to content

Commit

Permalink
ci_trace: fix args filter wrt branch name
Browse files Browse the repository at this point in the history
The CI args handler is considering only the branch name when the user passes
it with the job name in the format `<remote> <branch>:<jobname>` or with an
empty `<jobname>` like `<remote> <branch>:`. In all other subcommands, the
branch name can be used after the remote name without any additional suffix
(the colom in this case ':'). This patch fixes it and adds the ability to
the user call CI subcommands with `<remote> <branch>`.

This patch also adds some comments to the code where this filtering happens,
since the code, even though small, is quite dense wrt the number of
possibilities.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Oct 8, 2021
1 parent 7540b64 commit 38280ef
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,21 @@ func doTrace(ctx context.Context, w io.Writer, pid interface{}, pipelineID int,
return nil
}

// filterJobArg might be a small function, but contain a lot of
// possibilities to be handled. It gets the remote, branch and jobname from
// the CLI args. These can be present in the following formats:
// 1. <remote> <branch>:<jobname>
// 2. <remote> :<jobname>
// 3. <remote> <branch>
// 4. <branch>:<jobname>
// 5. <remote>
// 6. :<jobname>
func filterJobArg(args []string) (string, []string, error) {
branchArgs := []string{}
jobName := ""

if len(args) == 1 {
// <remote> alone or :<jobname>?
ok, err := git.IsRemote(args[0])
if err != nil {
return "", branchArgs, err
Expand All @@ -151,14 +161,22 @@ func filterJobArg(args []string) (string, []string, error) {
jobName = args[0]
}
} else if len(args) > 1 {
// the first arg is always the remote, we just need to check
// later the jobName.
branchArgs = append(branchArgs, args[0])
jobName = args[1]
}

// <branch>:<jobname>, <branch> or :<jobname>?
if strings.Contains(jobName, ":") {
// check for <branch>:<jobname> and :<jobname>
ps := strings.SplitN(jobName, ":", 2)
branchArgs = append(branchArgs, ps[0])
jobName = ps[1]
} else {
// the jobName refers to a branch name
branchArgs = append(branchArgs, jobName)
jobName = ""
}

return jobName, branchArgs, nil
Expand Down

0 comments on commit 38280ef

Please sign in to comment.