Skip to content

Commit

Permalink
feat(cdsctl): manage workflow label (#5028)
Browse files Browse the repository at this point in the history
* feat(cdsctl): manage workflow label

close #5023

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Mar 3, 2020
1 parent 1de9f1f commit 449ef4d
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 32 deletions.
5 changes: 3 additions & 2 deletions cli/cdsctl/admin_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

var adminServicesCmd = cli.Command{
Name: "services",
Short: "Manage CDS services",
Name: "services",
Aliases: []string{"service"},
Short: "Manage CDS services",
}

func adminServices() *cobra.Command {
Expand Down
1 change: 1 addition & 0 deletions cli/cdsctl/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func workflow() *cobra.Command {
cli.NewCommand(workflowPushCmd, workflowPushRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowFavoriteCmd, workflowFavoriteRun, nil, withAllCommandModifiers()...),
cli.NewGetCommand(workflowTransformAsCodeCmd, workflowTransformAsCodeRun, nil, withAllCommandModifiers()...),
workflowLabel(),
workflowArtifact(),
workflowLog(),
workflowAdvanced(),
Expand Down
84 changes: 84 additions & 0 deletions cli/cdsctl/workflow_label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"github.com/spf13/cobra"

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

var workflowLabelCmd = cli.Command{
Name: "label",
Aliases: []string{"labels"},
Short: "Manage Workflow Label",
}

func workflowLabel() *cobra.Command {
return cli.NewCommand(workflowLabelCmd, nil, []*cobra.Command{
cli.NewListCommand(workflowLabelListCmd, workflowLabelListRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowLabelAddCmd, workflowLabelAddRun, nil, withAllCommandModifiers()...),
cli.NewCommand(workflowLabelDeleteCmd, workflowLabelDeleteRun, nil, withAllCommandModifiers()...),
})
}

var workflowLabelListCmd = cli.Command{
Name: "list",
Short: "List labels of one workflow",
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
},
}

func workflowLabelListRun(v cli.Values) (cli.ListResult, error) {
wf, err := client.WorkflowGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), cdsclient.WithLabels())
if err != nil {
return nil, err
}
return cli.AsListResult(wf.Labels), nil
}

var workflowLabelAddCmd = cli.Command{
Name: "add",
Short: "Add label on one workflow",
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
{Name: "label"},
},
}

func workflowLabelAddRun(v cli.Values) error {
labelName := v.GetString("label")
err := client.WorkflowLabelAdd(v.GetString(_ProjectKey), v.GetString(_WorkflowName), labelName)
return err
}

var workflowLabelDeleteCmd = cli.Command{
Name: "delete",
Aliases: []string{"rm"},
Short: "Delete label from one workflow",
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
{Name: "label"},
},
}

func workflowLabelDeleteRun(v cli.Values) error {
labelName := v.GetString("label")
wf, err := client.WorkflowGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), cdsclient.WithLabels())
if err != nil {
return err
}

var labelID int64
for _, v := range wf.Labels {
if v.Name == labelName {
labelID = v.ID
break
}
}

return client.WorkflowLabelDelete(v.GetString(_ProjectKey), v.GetString(_WorkflowName), labelID)
}
2 changes: 1 addition & 1 deletion engine/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (api *API) putProjectLabelsHandler() service.Handler {
return err
}

// Check is project exist
// Check if project exist
proj, err := project.Load(db, api.Cache, key, project.LoadOptions.WithLabels)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion engine/api/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func (api *API) postWorkflowLabelHandler() service.Handler {
key := vars["key"]
workflowName := vars["permWorkflowName"]
db := api.mustDB()
//u := getAPIConsumer(ctx)

var label sdk.Label
if err := service.UnmarshalBody(r, &label); err != nil {
Expand Down
16 changes: 1 addition & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ require (
github.com/circonus-labs/circonus-gometrics v2.2.4+incompatible // indirect
github.com/circonus-labs/circonusllhist v0.0.0-20180430145027-5eb751da55c6 // indirect
github.com/dancannon/gorethink v4.0.0+incompatible // indirect

github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dnaeon/go-vcr v0.0.0-20180920040454-5637cf3d8a31 // indirect
github.com/docker/distribution v2.7.0-rc.0+incompatible // indirect
Expand Down Expand Up @@ -62,7 +61,7 @@ require (
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/go-stomp/stomp v1.0.1 // indirect
github.com/gocql/gocql v0.0.0-20181018123354-22229812a83e // indirect
github.com/golang/mock v1.4.0
github.com/golang/mock v1.4.1
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.4.0 // indirect
github.com/googleapis/gnostic v0.1.0 // indirect
Expand All @@ -71,7 +70,6 @@ require (
github.com/gorhill/cronexpr v0.0.0-20161205141322-d520615e531a
github.com/gorilla/handlers v0.0.0-20160816184729-a5775781a543
github.com/gorilla/mux v1.6.2

github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.6 // indirect
github.com/hashicorp/consul v1.3.0 // indirect
Expand All @@ -95,22 +93,19 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/itsjamie/gin-cors v0.0.0-20160420130702-97b4a9da7933
github.com/jefferai/jsonx v0.0.0-20160721235117-9cc31c3135ee // indirect

github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 // indirect
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
github.com/juju/testing v0.0.0-20190429233213-dfc56b8c09fc // indirect
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect

github.com/keybase/go-crypto v0.0.0-20181127160227-255a5089e85a
github.com/keybase/go-keychain v0.0.0-20190828020956-aa639f275ae1
github.com/keybase/go.dbus v0.0.0-20190710215703-a33a09c8a604
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lib/pq v1.0.0

github.com/lytics/logrus v0.0.0-20170528191427-4389a17ed024
github.com/magiconair/properties v1.8.1 // indirect
github.com/mailru/easyjson v0.0.0-20171120080333-32fa128f234d
Expand All @@ -134,31 +129,25 @@ require (
github.com/mndrix/tap-go v0.0.0-20170113192335-56cca451570b // indirect
github.com/mum4k/termdash v0.10.0
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d

github.com/ncw/swift v0.0.0-20171019114456-c95c6e5c2d1a
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 // indirect
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.0-20160621093029-daf2955e742c
github.com/olivere/elastic v6.2.17+incompatible // indirect

github.com/opencontainers/go-digest v1.0.0-rc1 // indirect

github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/ory-am/common v0.4.0 // indirect
github.com/ovh/cds/sdk/interpolate v0.0.0-20190319104452-71125b036b25

github.com/ovh/cds/sdk/izanami v0.0.0-20190703081656-683453b50b2a
github.com/ovh/configstore v0.3.2
github.com/ovh/symmecrypt v0.4.0

github.com/ovh/venom v0.25.0
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
github.com/pelletier/go-toml v1.4.0
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pierrec/lz4 v2.3.0+incompatible // indirect

github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15
github.com/pkg/errors v0.8.1
github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1 // indirect
Expand Down Expand Up @@ -194,7 +183,6 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yesnault/go-toml v0.0.0-20191205182532-f5ef6cee7945
github.com/yesnault/gorp v2.0.0+incompatible // indirect

github.com/yuin/gluare v0.0.0-20170607022532-d7c94f1a80ed
github.com/yuin/gopher-lua v0.0.0-20170901023928-8c2befcd3908
github.com/ziutek/mymysql v1.5.4 // indirect
Expand All @@ -214,11 +202,9 @@ require (
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/gorethink/gorethink.v4 v4.1.0 // indirect

gopkg.in/gorp.v1 v1.7.1 // indirect
gopkg.in/h2non/gock.v1 v1.0.14
gopkg.in/ldap.v2 v2.5.1

gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/olivere/elastic.v6 v6.2.17
gopkg.in/ory-am/dockertest.v2 v2.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.0 h1:Rd1kQnQu0Hq3qvJppYSG0HtP+f5LPPUiDswTLiEegLg=
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1 h1:ocYkMQY5RrXTYgXl7ICpV0IXwlEQGwKIsery4gyXa1U=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand Down
19 changes: 19 additions & 0 deletions sdk/cdsclient/client_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ func (c *client) WorkflowUpdate(projectKey, name string, wf *sdk.Workflow) error
return nil
}

func (c *client) WorkflowLabelAdd(projectKey, name, labelName string) error {
lbl := sdk.Label{
Name: labelName,
}
url := fmt.Sprintf("/project/%s/workflows/%s/label", projectKey, name)
if _, err := c.PostJSON(context.Background(), url, lbl, nil); err != nil {
return err
}
return nil
}

func (c *client) WorkflowLabelDelete(projectKey, name string, labelID int64) error {
url := fmt.Sprintf("/project/%s/workflows/%s/label/%d", projectKey, name, labelID)
if _, err := c.DeleteJSON(context.Background(), url, nil); err != nil {
return err
}
return nil
}

func (c *client) WorkflowGroupAdd(projectKey, name, groupName string, permission int) error {
gp := sdk.GroupPermission{
Group: sdk.Group{Name: groupName},
Expand Down
11 changes: 11 additions & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ type WorkflowClient interface {
WorkflowGet(projectKey, name string, opts ...RequestModifier) (*sdk.Workflow, error)
WorkflowUpdate(projectKey, name string, wf *sdk.Workflow) error
WorkflowDelete(projectKey string, workflowName string) error
WorkflowLabelAdd(projectKey, name, labelName string) error
WorkflowLabelDelete(projectKey, name string, labelID int64) error
WorkflowGroupAdd(projectKey, name, groupName string, permission int) error
WorkflowGroupDelete(projectKey, name, groupName string) error
WorkflowRunGet(projectKey string, workflowName string, number int64) (*sdk.WorkflowRun, error)
Expand Down Expand Up @@ -467,6 +469,15 @@ func WithWorkflows() RequestModifier {
}
}

// WithLabels allow a provider to retrieve a workflow with its labels
func WithLabels() RequestModifier {
return func(r *http.Request) {
q := r.URL.Query()
q.Set("withLabels", "true")
r.URL.RawQuery = q.Encode()
}
}

// AuthClient is the interface for authentication management.
type AuthClient interface {
AuthDriverList() (sdk.AuthDriverResponse, error)
Expand Down
Loading

0 comments on commit 449ef4d

Please sign in to comment.