Skip to content

Commit

Permalink
feat (cdsctl): update run number (#2109)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Jan 31, 2018
1 parent 89cd527 commit 457c15b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/cdsctl/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ var (
cli.NewListCommand(workflowHistoryCmd, workflowHistoryRun, nil, withAllCommandModifiers()...),
cli.NewGetCommand(workflowShowCmd, workflowShowRun, nil, withAllCommandModifiers()...),
cli.NewGetCommand(workflowStatusCmd, workflowStatusRun, nil, withAllCommandModifiers()...),
cli.NewDeleteCommand(workflowDeleteCmd, workflowDeleteRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowRunManualCmd, workflowRunManualRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowStopCmd, workflowStopRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowExportCmd, workflowExportRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowImportCmd, workflowImportRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowPullCmd, workflowPullRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowPushCmd, workflowPushRun, nil, withAllCommandModifiers()...),
workflowArtifact,
workflowAdvanced,
})
)

Expand Down
74 changes: 74 additions & 0 deletions cli/cdsctl/workflow_advanced.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"fmt"
"strconv"

"github.com/spf13/cobra"

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

var (
workflowAdvancedCmd = cli.Command{
Name: "advanced",
Short: "Manage Workflow Advanced",
}

workflowAdvanced = cli.NewCommand(workflowAdvancedCmd, nil,
[]*cobra.Command{
cli.NewDeleteCommand(workflowDeleteCmd, workflowDeleteRun, nil, withAllCommandModifiers()...),
workflowAdvancedRunNumber,
})

workflowAdvancedRunNumberCmd = cli.Command{
Name: "number",
Short: "Manage Workflow Run Number",
}

workflowAdvancedRunNumber = cli.NewCommand(workflowAdvancedRunNumberCmd, nil,
[]*cobra.Command{
cli.NewGetCommand(workflowRunNumberShowCmd, workflowRunNumberShowRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowRunNumberSetCmd, workflowRunNumberSetRun, nil, withAllCommandModifiers()...),
})
)

var workflowRunNumberShowCmd = cli.Command{
Name: "show",
Short: "Show a Workflow Run Number",
Example: `cdsctl workflow advanced number show MYPROJECT my-workflow`,
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
},
}

var workflowRunNumberSetCmd = cli.Command{
Name: "set",
Short: "Set a Workflow Run Number",
Example: `cdsctl workflow advanced number set MYPROJECT my-workflow 22`,
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
},
Args: []cli.Arg{
{Name: "number"},
},
}

func workflowRunNumberShowRun(v cli.Values) (interface{}, error) {
return client.WorkflowRunNumberGet(v[_ProjectKey], v[_WorkflowName])
}

func workflowRunNumberSetRun(v cli.Values) error {
number, err := strconv.ParseInt(v["number"], 10, 64)
if err != nil {
return fmt.Errorf("number parameter have to be an integer")
}

if err := client.WorkflowRunNumberSet(v[_ProjectKey], v[_WorkflowName], number); err != nil {
return err
}

return nil
}
9 changes: 3 additions & 6 deletions engine/api/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (api *API) getWorkflowRunsHandler() Handler {
}
}

// getWorkflowRunNumHandler returns the last run number for the given workflow
func (api *API) getWorkflowRunNumHandler() Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
Expand All @@ -177,15 +178,11 @@ func (api *API) getWorkflowRunNumHandler() Handler {
return sdk.WrapError(err, "getWorkflowRunNumHandler> Cannot load current run num")
}

m := struct {
Num int64 `json:"num"`
}{
Num: num,
}
return WriteJSON(w, r, m, http.StatusOK)
return WriteJSON(w, r, sdk.WorkflowRunNumber{Num: num}, http.StatusOK)
}
}

// postWorkflowRunNumHandler updates the current run number for the given workflow
func (api *API) postWorkflowRunNumHandler() Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
Expand Down
22 changes: 22 additions & 0 deletions sdk/cdsclient/client_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ func (c *client) WorkflowNodeRun(projectKey string, workflowName string, number
return &run, nil
}

func (c *client) WorkflowRunNumberGet(projectKey string, workflowName string) (*sdk.WorkflowRunNumber, error) {
url := fmt.Sprintf("/project/%s/workflows/%s/runs/num", projectKey, workflowName)
runNumber := sdk.WorkflowRunNumber{}
if _, err := c.GetJSON(url, &runNumber); err != nil {
return nil, err
}
return &runNumber, nil
}

func (c *client) WorkflowRunNumberSet(projectKey string, workflowName string, number int64) error {
url := fmt.Sprintf("/project/%s/workflows/%s/runs/num", projectKey, workflowName)
runNumber := sdk.WorkflowRunNumber{Num: number}
code, err := c.PostJSON(url, runNumber, nil)
if err != nil {
return err
}
if code >= 300 {
return fmt.Errorf("Cannot update workflow run number. HTTP code error : %d", code)
}
return nil
}

func (c *client) WorkflowNodeRunJobStep(projectKey string, workflowName string, number int64, nodeRunID, job int64, step int) (*sdk.BuildState, error) {
url := fmt.Sprintf("/project/%s/workflows/%s/runs/%d/nodes/%d/job/%d/step/%d", projectKey, workflowName, number, nodeRunID, job, step)
buildState := sdk.BuildState{}
Expand Down
2 changes: 2 additions & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ type WorkflowClient interface {
WorkflowRunArtifacts(projectKey string, name string, number int64) ([]sdk.WorkflowNodeRunArtifact, error)
WorkflowRunFromHook(projectKey string, workflowName string, hook sdk.WorkflowNodeRunHookEvent) (*sdk.WorkflowRun, error)
WorkflowRunFromManual(projectKey string, workflowName string, manual sdk.WorkflowNodeRunManual, number, fromNodeID int64) (*sdk.WorkflowRun, error)
WorkflowRunNumberGet(projectKey string, workflowName string) (*sdk.WorkflowRunNumber, error)
WorkflowRunNumberSet(projectKey string, workflowName string, number int64) error
WorkflowStop(projectKey string, workflowName string, number int64) (*sdk.WorkflowRun, error)
WorkflowNodeStop(projectKey string, workflowName string, number, fromNodeID int64) (*sdk.WorkflowNodeRun, error)
WorkflowNodeRun(projectKey string, name string, number int64, nodeRunID int64) (*sdk.WorkflowNodeRun, error)
Expand Down
5 changes: 5 additions & 0 deletions sdk/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type WorkflowRunPostHandlerOption struct {
FromNodeIDs []int64 `json:"from_nodes,omitempty"`
}

//WorkflowRunNumber contains a workflow run number
type WorkflowRunNumber struct {
Num int64 `json:"num" cli:"run-number"`
}

// Translate translates messages in WorkflowNodeRun
func (r *WorkflowRun) Translate(lang string) {
for ki, info := range r.Infos {
Expand Down

0 comments on commit 457c15b

Please sign in to comment.