Skip to content

Commit

Permalink
feat(worker): worker download from latest workflow run (#4104)
Browse files Browse the repository at this point in the history
close #4095
  • Loading branch information
fsamin authored and yesnault committed Mar 29, 2019
1 parent feceb12 commit eb6c007
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/content/docs/components/worker/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ worker download [flags]
## Options

```
--number string Workflow Number to download from. Optional, default: current workflow run
--number string Workflow Number to download from. Optional, default: if workflow is the current workflow: current run, else latest run
--pattern string Pattern matching files to download. Optional, default: *
--tag string Tag matching files to download. Optional
--workflow string Workflow name to download from. Optional, default: current workflow
Expand Down
39 changes: 31 additions & 8 deletions engine/worker/cmd_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"

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

var (
Expand Down Expand Up @@ -139,18 +140,40 @@ func (wk *currentWorker) downloadHandler(w http.ResponseWriter, r *http.Request)

sendLog := getLogger(wk, wk.currentJob.wJob.ID, wk.currentJob.currentStep)

currentProject := sdk.ParameterValue(wk.currentJob.params, "cds.project")
currentWorkflow := sdk.ParameterValue(wk.currentJob.params, "cds.workflow")
if reqArgs.Workflow == "" {
reqArgs.Workflow = sdk.ParameterValue(wk.currentJob.params, "cds.workflow")
reqArgs.Workflow = currentWorkflow
}

// If the reqArgs.Number is empty and if the reqArgs.Workflow is the current workflow, take the current build number
if reqArgs.Number == 0 {
var errN error
buildNumberString := sdk.ParameterValue(wk.currentJob.params, "cds.run.number")
reqArgs.Number, errN = strconv.ParseInt(buildNumberString, 10, 64)
if errN != nil {
newError := sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("Cannot parse '%s' as run number: %s", buildNumberString, errN))
writeError(w, r, newError)
return
if reqArgs.Workflow == currentWorkflow {
var errN error
buildNumberString := sdk.ParameterValue(wk.currentJob.params, "cds.run.number")
reqArgs.Number, errN = strconv.ParseInt(buildNumberString, 10, 64)
if errN != nil {
newError := sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("Cannot parse '%s' as run number: %s", buildNumberString, errN))
writeError(w, r, newError)
return
}
} else { // If this is another workflow, check the latest run
filters := []cdsclient.Filter{
{
Name: "workflow",
Value: reqArgs.Workflow,
},
}
runs, err := wk.client.WorkflowRunSearch(currentProject, 0, 0, filters...)
if err != nil {
writeError(w, r, err)
return
}
if len(runs) < 1 {
writeError(w, r, fmt.Errorf("workflow run not found"))
return
}
reqArgs.Number = runs[0].Number
}
}

Expand Down

0 comments on commit eb6c007

Please sign in to comment.