Skip to content

Commit 1bef09f

Browse files
committed
[ND-7649] - add pipeline jobs command
1 parent 1f17852 commit 1bef09f

4 files changed

Lines changed: 166 additions & 0 deletions

File tree

cmd/pipeline/jobs.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package pipeline
2+
3+
import (
4+
"bunnyshell.com/cli/pkg/api/workflow_job"
5+
"bunnyshell.com/cli/pkg/lib"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
listOptions := workflow_job.NewListOptions()
11+
12+
var pipelineID string
13+
var jobStatuses []string
14+
15+
command := &cobra.Command{
16+
Use: "jobs",
17+
18+
Short: "List jobs in a pipeline",
19+
20+
ValidArgsFunction: cobra.NoFileCompletions,
21+
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
listOptions.Workflow = pipelineID
24+
listOptions.Status = jobStatuses
25+
26+
return lib.ShowCollection(cmd, listOptions, func() (lib.ModelWithPagination, error) {
27+
return workflow_job.List(listOptions)
28+
})
29+
},
30+
}
31+
32+
flags := command.Flags()
33+
34+
flags.AddFlag(getIDOption(&pipelineID).GetRequiredFlag("id"))
35+
flags.StringArrayVar(&jobStatuses, "jobStatus", jobStatuses, "Filter by Job Status (repeatable)")
36+
37+
listOptions.UpdateFlagSet(flags)
38+
39+
mainCmd.AddCommand(command)
40+
}

pkg/api/workflow_job/list.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package workflow_job
2+
3+
import (
4+
"net/http"
5+
6+
"bunnyshell.com/cli/pkg/api"
7+
"bunnyshell.com/cli/pkg/api/common"
8+
"bunnyshell.com/cli/pkg/lib"
9+
"bunnyshell.com/sdk"
10+
"github.com/spf13/pflag"
11+
)
12+
13+
type ListOptions struct {
14+
common.ListOptions
15+
16+
Workflow string
17+
Status []string
18+
}
19+
20+
func NewListOptions() *ListOptions {
21+
return &ListOptions{
22+
ListOptions: *common.NewListOptions(),
23+
}
24+
}
25+
26+
func (lo *ListOptions) UpdateFlagSet(flags *pflag.FlagSet) {
27+
lo.ListOptions.UpdateFlagSet(flags)
28+
}
29+
30+
func List(options *ListOptions) (*sdk.PaginatedWorkflowJobCollection, error) {
31+
model, resp, err := ListRaw(options)
32+
if err != nil {
33+
return nil, api.ParseError(resp, err)
34+
}
35+
36+
return model, nil
37+
}
38+
39+
func ListRaw(options *ListOptions) (*sdk.PaginatedWorkflowJobCollection, *http.Response, error) {
40+
profile := options.GetProfile()
41+
42+
ctx, cancel := lib.GetContextFromProfile(profile)
43+
defer cancel()
44+
45+
request := lib.GetAPIFromProfile(profile).WorkflowJobAPI.WorkflowJobList(ctx)
46+
47+
return applyOptions(request, options).Execute()
48+
}
49+
50+
func applyOptions(request sdk.ApiWorkflowJobListRequest, options *ListOptions) sdk.ApiWorkflowJobListRequest {
51+
if options == nil {
52+
return request
53+
}
54+
55+
if options.Workflow != "" {
56+
request = request.Workflow(options.Workflow)
57+
}
58+
59+
if options.Page > 1 {
60+
request = request.Page(options.Page)
61+
}
62+
63+
if len(options.Status) > 0 {
64+
request = request.Status(options.Status)
65+
}
66+
67+
return request
68+
}

pkg/formatter/stylish.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func stylish(data interface{}) ([]byte, error) {
3838
tabulateKubernetesCollection(writer, dataType)
3939
case *sdk.PaginatedPipelineCollection:
4040
tabulatePipelineCollection(writer, dataType)
41+
case *sdk.PaginatedWorkflowJobCollection:
42+
tabulateWorkflowJobCollection(writer, dataType)
4143
case *sdk.PaginatedComponentGitCollection:
4244
tabulateComponentGitCollection(writer, dataType)
4345
case []sdk.ComponentGitCollection:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package formatter
2+
3+
import (
4+
"fmt"
5+
"text/tabwriter"
6+
"time"
7+
8+
"bunnyshell.com/sdk"
9+
)
10+
11+
func tabulateWorkflowJobCollection(writer *tabwriter.Writer, data *sdk.PaginatedWorkflowJobCollection) {
12+
fmt.Fprintf(
13+
writer,
14+
"%v\t %v\t %v\t %v\t %v\t %v\t %v\t %v\n",
15+
"JobID",
16+
"PipelineID",
17+
"Name",
18+
"Type",
19+
"Status",
20+
"StartedAt",
21+
"Duration",
22+
"AllowedToFail",
23+
)
24+
25+
if data.Embedded != nil {
26+
for _, item := range data.Embedded.Item {
27+
duration := ""
28+
if value, ok := item.GetDurationOk(); ok && value != nil {
29+
duration = (time.Duration(*value) * time.Second).String()
30+
}
31+
32+
allowedToFail := ""
33+
if value, ok := item.GetAllowedToFailOk(); ok && value != nil {
34+
allowedToFail = fmt.Sprintf("%t", *value)
35+
}
36+
37+
startedAt := ""
38+
if value, ok := item.GetStartedAtOk(); ok && value != nil {
39+
startedAt = value.Format(time.RFC3339)
40+
}
41+
42+
fmt.Fprintf(
43+
writer,
44+
"%v\t %v\t %v\t %v\t %v\t %v\t %v\t %v\n",
45+
item.GetId(),
46+
item.GetWorkflow(),
47+
item.GetName(),
48+
item.GetType(),
49+
item.GetStatus(),
50+
startedAt,
51+
duration,
52+
allowedToFail,
53+
)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)