Skip to content

Commit 320bf76

Browse files
authored
feat(cdsctl): action usage command (#5025)
1 parent 9eb9721 commit 320bf76

File tree

7 files changed

+137
-43
lines changed

7 files changed

+137
-43
lines changed

cli/cdsctl/action.go

+42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"path"
77
"time"
8+
"strings"
89

910
"github.com/spf13/cobra"
1011

@@ -28,6 +29,7 @@ var actionBuiltinCmd = cli.Command{
2829
func action() *cobra.Command {
2930
return cli.NewCommand(actionCmd, nil, []*cobra.Command{
3031
cli.NewListCommand(actionListCmd, actionListRun, nil),
32+
cli.NewListCommand(actionUsageCmd, actionUsageRun, nil),
3133
cli.NewGetCommand(actionShowCmd, actionShowRun, nil),
3234
cli.NewCommand(actionDeleteCmd, actionDeleteRun, nil),
3335
cli.NewCommand(actionDocCmd, actionDocRun, nil),
@@ -87,6 +89,46 @@ func actionListRun(v cli.Values) (cli.ListResult, error) {
8789
return cli.AsListResult(ads), nil
8890
}
8991

92+
var actionUsageCmd = cli.Command{
93+
Name: "usage",
94+
Short: "CDS action usage",
95+
Args: []cli.Arg{
96+
{Name: "action-path"},
97+
},
98+
}
99+
100+
func actionUsageRun(v cli.Values) (cli.ListResult, error) {
101+
groupName, actionName, err := cli.ParsePath(v.GetString("action-path"))
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
usages, err := client.ActionUsage(groupName, actionName)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
type ActionUsageDisplay struct {
112+
Type string `cli:"Type"`
113+
Path string `cli:"Path"`
114+
}
115+
116+
au := []ActionUsageDisplay{}
117+
for _, v := range usages.Pipelines {
118+
au = append(au, ActionUsageDisplay{
119+
Type: "pipeline",
120+
Path: strings.Replace(fmt.Sprintf("%s - %s - %s", v.ProjectName, v.PipelineName, v.ActionName)," "," ",-1),
121+
})
122+
}
123+
for _, v := range usages.Actions {
124+
au = append(au, ActionUsageDisplay{
125+
Type: "action",
126+
Path: fmt.Sprintf("%s/%s", v.GroupName, v.ParentActionName),
127+
})
128+
}
129+
return cli.AsListResult(au), nil
130+
}
131+
90132
var actionShowCmd = cli.Command{
91133
Name: "show",
92134
Short: "Show a CDS action",

engine/api/action.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ func (api *API) getActionBuiltinUsageHandler() service.Handler {
823823
}
824824
}
825825

826-
func getActionUsage(ctx context.Context, db gorp.SqlExecutor, store cache.Store, a *sdk.Action) (action.Usage, error) {
827-
var usage action.Usage
826+
func getActionUsage(ctx context.Context, db gorp.SqlExecutor, store cache.Store, a *sdk.Action) (sdk.ActionUsages, error) {
827+
var usage sdk.ActionUsages
828828
var err error
829829
usage.Pipelines, err = action.GetPipelineUsages(db, group.SharedInfraGroup.ID, a.ID)
830830
if err != nil {
@@ -848,7 +848,7 @@ func getActionUsage(ctx context.Context, db gorp.SqlExecutor, store cache.Store,
848848
mProjectIDs[ps[i].ID] = struct{}{}
849849
}
850850

851-
filteredPipelines := make([]action.UsagePipeline, 0, len(usage.Pipelines))
851+
filteredPipelines := make([]sdk.UsagePipeline, 0, len(usage.Pipelines))
852852
for i := range usage.Pipelines {
853853
if _, ok := mProjectIDs[usage.Pipelines[i].ProjectID]; ok {
854854
filteredPipelines = append(filteredPipelines, usage.Pipelines[i])
@@ -863,7 +863,7 @@ func getActionUsage(ctx context.Context, db gorp.SqlExecutor, store cache.Store,
863863
mGroupIDs[groupIDs[i]] = struct{}{}
864864
}
865865

866-
filteredActions := make([]action.UsageAction, 0, len(usage.Actions))
866+
filteredActions := make([]sdk.UsageAction, 0, len(usage.Actions))
867867
for i := range usage.Actions {
868868
if _, ok := mGroupIDs[usage.Actions[i].GroupID]; ok {
869869
filteredActions = append(filteredActions, usage.Actions[i])

engine/api/action/usage.go

+6-39
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,8 @@ import (
66
"github.com/ovh/cds/sdk"
77
)
88

9-
// Usage for action.
10-
type Usage struct {
11-
Pipelines []UsagePipeline `json:"pipelines"`
12-
Actions []UsageAction `json:"actions"`
13-
}
14-
15-
// UsagePipeline represent a pipeline using an action.
16-
type UsagePipeline struct {
17-
ProjectID int64 `json:"project_id"`
18-
ProjectKey string `json:"project_key"`
19-
ProjectName string `json:"project_name"`
20-
PipelineID int64 `json:"pipeline_id"`
21-
PipelineName string `json:"pipeline_name"`
22-
StageID int64 `json:"stage_id"`
23-
StageName string `json:"stage_name"`
24-
JobID int64 `json:"job_id"`
25-
JobName string `json:"job_name"`
26-
ActionID int64 `json:"action_id"`
27-
ActionName string `json:"action_name"`
28-
Warning bool `json:"warning"`
29-
}
30-
319
// GetPipelineUsages returns the list of pipelines using an action
32-
func GetPipelineUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64) ([]UsagePipeline, error) {
10+
func GetPipelineUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64) ([]sdk.UsagePipeline, error) {
3311
rows, err := db.Query(`
3412
SELECT DISTINCT
3513
project.id, project.projectKey, project.name,
@@ -54,9 +32,9 @@ func GetPipelineUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64)
5432
}
5533
defer rows.Close()
5634

57-
us := []UsagePipeline{}
35+
us := []sdk.UsagePipeline{}
5836
for rows.Next() {
59-
var u UsagePipeline
37+
var u sdk.UsagePipeline
6038
if err := rows.Scan(
6139
&u.ProjectID, &u.ProjectKey, &u.ProjectName,
6240
&u.PipelineID, &u.PipelineName,
@@ -73,19 +51,8 @@ func GetPipelineUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64)
7351
return us, nil
7452
}
7553

76-
// UsageAction represent a action using an action.
77-
type UsageAction struct {
78-
GroupID int64 `json:"group_id"`
79-
GroupName string `json:"group_name"`
80-
ParentActionID int64 `json:"parent_action_id"`
81-
ParentActionName string `json:"parent_action_name"`
82-
ActionID int64 `json:"action_id"`
83-
ActionName string `json:"action_name"`
84-
Warning bool `json:"warning"`
85-
}
86-
8754
// GetActionUsages returns the list of actions using an action
88-
func GetActionUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64) ([]UsageAction, error) {
55+
func GetActionUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64) ([]sdk.UsageAction, error) {
8956
rows, err := db.Query(`
9057
SELECT DISTINCT
9158
"group".id, "group".name,
@@ -104,9 +71,9 @@ func GetActionUsages(db gorp.SqlExecutor, sharedInfraGroupID, actionID int64) ([
10471
}
10572
defer rows.Close()
10673

107-
us := []UsageAction{}
74+
us := []sdk.UsageAction{}
10875
for rows.Next() {
109-
var u UsageAction
76+
var u sdk.UsageAction
11077
if err := rows.Scan(
11178
&u.GroupID, &u.GroupName,
11279
&u.ParentActionID, &u.ParentActionName,

sdk/action.go

+33
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ type Action struct {
6060
Editable bool `json:"editable,omitempty" db:"-"`
6161
}
6262

63+
// UsageAction represent a action using an action.
64+
type UsageAction struct {
65+
GroupID int64 `json:"group_id"`
66+
GroupName string `json:"group_name"`
67+
ParentActionID int64 `json:"parent_action_id"`
68+
ParentActionName string `json:"parent_action_name"`
69+
ActionID int64 `json:"action_id"`
70+
ActionName string `json:"action_name"`
71+
Warning bool `json:"warning"`
72+
}
73+
74+
// ActionUsages for action.
75+
type ActionUsages struct {
76+
Pipelines []UsagePipeline `json:"pipelines"`
77+
Actions []UsageAction `json:"actions"`
78+
}
79+
80+
// UsagePipeline represent a pipeline using an action.
81+
type UsagePipeline struct {
82+
ProjectID int64 `json:"project_id"`
83+
ProjectKey string `json:"project_key"`
84+
ProjectName string `json:"project_name"`
85+
PipelineID int64 `json:"pipeline_id"`
86+
PipelineName string `json:"pipeline_name"`
87+
StageID int64 `json:"stage_id"`
88+
StageName string `json:"stage_name"`
89+
JobID int64 `json:"job_id"`
90+
JobName string `json:"job_name"`
91+
ActionID int64 `json:"action_id"`
92+
ActionName string `json:"action_name"`
93+
Warning bool `json:"warning"`
94+
}
95+
6396
// Value returns driver.Value from action.
6497
func (a Action) Value() (driver.Value, error) {
6598
j, err := json.Marshal(a)

sdk/cdsclient/client_action.go

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ func (c *client) ActionGet(groupName, name string, mods ...RequestModifier) (*sd
3535
return &a, nil
3636
}
3737

38+
func (c *client) ActionUsage(groupName, name string, mods ...RequestModifier) (*sdk.ActionUsages, error) {
39+
var a sdk.ActionUsages
40+
41+
path := fmt.Sprintf("/action/%s/%s/usage", groupName, name)
42+
if _, err := c.GetJSON(context.Background(), path, &a, mods...); err != nil {
43+
return nil, err
44+
}
45+
46+
return &a, nil
47+
}
48+
3849
func (c *client) ActionList() ([]sdk.Action, error) {
3950
actions := []sdk.Action{}
4051
if _, err := c.GetJSON(context.Background(), "/action", &actions); err != nil {

sdk/cdsclient/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ type DownloadClient interface {
156156
type ActionClient interface {
157157
ActionDelete(groupName, name string) error
158158
ActionGet(groupName, name string, mods ...RequestModifier) (*sdk.Action, error)
159+
ActionUsage(groupName, name string, mods ...RequestModifier) (*sdk.ActionUsages, error)
159160
ActionList() ([]sdk.Action, error)
160161
ActionImport(content io.Reader, format string) error
161162
ActionExport(groupName, name string, format string) ([]byte, error)

sdk/cdsclient/mock_cdsclient/interface_mock.go

+40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)