Skip to content

Commit

Permalink
feat(cdsctl): search by commit instead of run number (#5751)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Mar 8, 2021
1 parent 7adfafa commit a6d8fe4
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions cli/cdsctl/workflow_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)

var workflowStatusCmd = cli.Command{
Expand All @@ -31,6 +32,11 @@ var workflowStatusCmd = cli.Command{
Type: cli.FlagBool,
Usage: "Wait the workflow to be over",
},
{
Name: "commit",
Type: cli.FlagString,
Usage: "Specify the git commit of the run you want",
},
},
}

Expand Down Expand Up @@ -131,32 +137,60 @@ func workflowRunFormatDisplay(run *sdk.WorkflowRun, commit repo.Commit, currentD
}

func workflowStatusRunWithoutTrack(v cli.Values) (interface{}, error) {
var runNumber int64
var errRunNumber error
// if no run number, get the latest
runNumberStr := v.GetString("run-number")
if runNumberStr != "" {
runNumber, errRunNumber = strconv.ParseInt(runNumberStr, 10, 64)
} else {
runNumber, errRunNumber = workflowNodeForCurrentRepo(v.GetString(_ProjectKey), v.GetString(_WorkflowName))
}
if errRunNumber != nil {
return nil, errRunNumber
}
if runNumber == 0 {
runs, err := client.WorkflowRunList(v.GetString(_ProjectKey), v.GetString(_WorkflowName), 0, 1)
var run *sdk.WorkflowRun

// Search by commit if provided
gitHash := v.GetString("commit")
if gitHash != "" {
filters := []cdsclient.Filter{
{
Name: "workflow",
Value: v.GetString(_WorkflowName),
},
{
Name: "git.hash",
Value: gitHash,
},
}
//Searching workflow
runs, err := client.WorkflowRunSearch(v.GetString(_ProjectKey), 0, 0, filters...)
if err != nil {
return nil, err
}
if len(runs) != 1 {
if len(runs) < 1 {
return 0, fmt.Errorf("workflow run not found")
}
runNumber = runs[0].Number
}
run = &runs[0]
} else {
// else search by run number
var runNumber int64
var errRunNumber error
// if no run number, get the latest
runNumberStr := v.GetString("run-number")
if runNumberStr != "" {
runNumber, errRunNumber = strconv.ParseInt(runNumberStr, 10, 64)
} else {
runNumber, errRunNumber = workflowNodeForCurrentRepo(v.GetString(_ProjectKey), v.GetString(_WorkflowName))
}
if errRunNumber != nil {
return nil, errRunNumber
}
if runNumber == 0 {
runs, err := client.WorkflowRunList(v.GetString(_ProjectKey), v.GetString(_WorkflowName), 0, 1)
if err != nil {
return nil, err
}
if len(runs) != 1 {
return 0, fmt.Errorf("workflow run not found")
}
runNumber = runs[0].Number
}

run, err := client.WorkflowRunGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), runNumber)
if err != nil {
return nil, err
var err error
run, err = client.WorkflowRunGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), runNumber)
if err != nil {
return nil, err
}
}

var tags []string
Expand Down

0 comments on commit a6d8fe4

Please sign in to comment.