Skip to content

Commit a225173

Browse files
committed
support --workflow in run list
1 parent 6dba073 commit a225173

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

pkg/cmd/run/list/list.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cli/cli/api"
88
"github.com/cli/cli/internal/ghrepo"
99
"github.com/cli/cli/pkg/cmd/run/shared"
10+
workflowShared "github.com/cli/cli/pkg/cmd/workflow/shared"
1011
"github.com/cli/cli/pkg/cmdutil"
1112
"github.com/cli/cli/pkg/iostreams"
1213
"github.com/cli/cli/utils"
@@ -24,7 +25,8 @@ type ListOptions struct {
2425

2526
PlainOutput bool
2627

27-
Limit int
28+
Limit int
29+
WorkflowSelector string
2830
}
2931

3032
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
@@ -58,6 +60,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
5860
}
5961

6062
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of runs to fetch")
63+
cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow")
6164

6265
return cmd
6366
}
@@ -74,8 +77,20 @@ func listRun(opts *ListOptions) error {
7477
}
7578
client := api.NewClientFromHTTP(c)
7679

80+
var runs []shared.Run
81+
var workflow *workflowShared.Workflow
82+
7783
opts.IO.StartProgressIndicator()
78-
runs, err := shared.GetRuns(client, baseRepo, opts.Limit)
84+
if opts.WorkflowSelector != "" {
85+
states := []workflowShared.WorkflowState{workflowShared.Active}
86+
workflow, err = workflowShared.ResolveWorkflow(
87+
opts.IO, client, baseRepo, false, opts.WorkflowSelector, states)
88+
if err == nil {
89+
runs, err = shared.GetRunsByWorkflow(client, baseRepo, opts.Limit, workflow.ID)
90+
}
91+
} else {
92+
runs, err = shared.GetRuns(client, baseRepo, opts.Limit)
93+
}
7994
opts.IO.StopProgressIndicator()
8095
if err != nil {
8196
return fmt.Errorf("failed to get runs: %w", err)

pkg/cmd/run/list/list_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/cli/cli/internal/ghrepo"
1111
"github.com/cli/cli/pkg/cmd/run/shared"
12+
workflowShared "github.com/cli/cli/pkg/cmd/workflow/shared"
1213
"github.com/cli/cli/pkg/cmdutil"
1314
"github.com/cli/cli/pkg/httpmock"
1415
"github.com/cli/cli/pkg/iostreams"
@@ -42,6 +43,14 @@ func TestNewCmdList(t *testing.T) {
4243
cli: "--limit hi",
4344
wantsErr: true,
4445
},
46+
{
47+
name: "workflow",
48+
cli: "--workflow foo.yml",
49+
wants: ListOptions{
50+
Limit: defaultLimit,
51+
WorkflowSelector: "foo.yml",
52+
},
53+
},
4554
}
4655

4756
for _, tt := range tests {
@@ -171,6 +180,24 @@ func TestListRun(t *testing.T) {
171180
wantOut: "",
172181
wantErrOut: "No runs found\n",
173182
},
183+
{
184+
name: "workflow selector",
185+
opts: &ListOptions{
186+
Limit: defaultLimit,
187+
WorkflowSelector: "flow.yml",
188+
},
189+
stubs: func(reg *httpmock.Registry) {
190+
reg.Register(
191+
httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/flow.yml"),
192+
httpmock.JSONResponse(workflowShared.AWorkflow))
193+
reg.Register(
194+
httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123/runs"),
195+
httpmock.JSONResponse(shared.RunsPayload{
196+
WorkflowRuns: shared.WorkflowRuns,
197+
}))
198+
},
199+
wantOut: "- cool commit in progress trunk push 2\n✓ cool commit successful trunk push 3\nX cool commit failed trunk push 1234\n\nFor details on a run, try: gh run view <run-id>\n",
200+
},
174201
}
175202

176203
for _, tt := range tests {

pkg/cmd/run/shared/shared.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,17 @@ type RunsPayload struct {
150150
WorkflowRuns []Run `json:"workflow_runs"`
151151
}
152152

153+
func GetRunsByWorkflow(client *api.Client, repo ghrepo.Interface, limit, workflowID int) ([]Run, error) {
154+
path := fmt.Sprintf("repos/%s/actions/workflows/%d/runs", ghrepo.FullName(repo), workflowID)
155+
return getRuns(client, repo, path, limit)
156+
}
157+
153158
func GetRuns(client *api.Client, repo ghrepo.Interface, limit int) ([]Run, error) {
159+
path := fmt.Sprintf("repos/%s/actions/runs", ghrepo.FullName(repo))
160+
return getRuns(client, repo, path, limit)
161+
}
162+
163+
func getRuns(client *api.Client, repo ghrepo.Interface, path string, limit int) ([]Run, error) {
154164
perPage := limit
155165
page := 1
156166
if limit > 100 {
@@ -162,9 +172,9 @@ func GetRuns(client *api.Client, repo ghrepo.Interface, limit int) ([]Run, error
162172
for len(runs) < limit {
163173
var result RunsPayload
164174

165-
path := fmt.Sprintf("repos/%s/actions/runs?per_page=%d&page=%d", ghrepo.FullName(repo), perPage, page)
175+
pagedPath := fmt.Sprintf("%s?per_page=%d&page=%d", path, perPage, page)
166176

167-
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
177+
err := client.REST(repo.RepoHost(), "GET", pagedPath, nil, &result)
168178
if err != nil {
169179
return nil, err
170180
}

pkg/cmd/run/shared/test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ var TestRuns []Run = []Run{
5555
TestRun("stale", 10, Completed, Stale),
5656
}
5757

58+
var WorkflowRuns []Run = []Run{
59+
TestRun("in progress", 2, InProgress, ""),
60+
SuccessfulRun,
61+
FailedRun,
62+
}
63+
5864
var SuccessfulJob Job = Job{
5965
ID: 10,
6066
Status: Completed,

0 commit comments

Comments
 (0)