diff --git a/pkg/cmd/run/list/list.go b/pkg/cmd/run/list/list.go index 4b085a1a1..b98a40f08 100644 --- a/pkg/cmd/run/list/list.go +++ b/pkg/cmd/run/list/list.go @@ -32,6 +32,7 @@ type ListOptions struct { Branch string Actor string Status string + Event string now time.Time } @@ -68,6 +69,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow") cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Filter runs by branch") cmd.Flags().StringVarP(&opts.Actor, "user", "u", "", "Filter runs by user who triggered the run") + cmd.Flags().StringVarP(&opts.Event, "event", "e", "", "Filter runs by which `event` triggered the run") cmdutil.StringEnumFlag(cmd, &opts.Status, "status", "s", "", shared.AllStatuses, "Filter runs by status") cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.RunFields) @@ -92,6 +94,7 @@ func listRun(opts *ListOptions) error { Branch: opts.Branch, Actor: opts.Actor, Status: opts.Status, + Event: opts.Event, } opts.IO.StartProgressIndicator() diff --git a/pkg/cmd/run/list/list_test.go b/pkg/cmd/run/list/list_test.go index d33af7167..116c1d829 100644 --- a/pkg/cmd/run/list/list_test.go +++ b/pkg/cmd/run/list/list_test.go @@ -77,6 +77,14 @@ func TestNewCmdList(t *testing.T) { Status: "completed", }, }, + { + name: "event", + cli: "--event push", + wants: ListOptions{ + Limit: defaultLimit, + Event: "push", + }, + }, } for _, tt := range tests { @@ -112,6 +120,8 @@ func TestNewCmdList(t *testing.T) { assert.Equal(t, tt.wants.WorkflowSelector, gotOpts.WorkflowSelector) assert.Equal(t, tt.wants.Branch, gotOpts.Branch) assert.Equal(t, tt.wants.Actor, gotOpts.Actor) + assert.Equal(t, tt.wants.Status, gotOpts.Status) + assert.Equal(t, tt.wants.Event, gotOpts.Event) }) } } @@ -424,6 +434,24 @@ func TestListRun(t *testing.T) { wantErr: true, wantErrMsg: "no runs found", }, + { + name: "event filter applied", + opts: &ListOptions{ + Limit: defaultLimit, + Event: "push", + }, + isTTY: true, + stubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "repos/OWNER/REPO/actions/runs", url.Values{ + "event": []string{"push"}, + }), + httpmock.JSONResponse(shared.RunsPayload{}), + ) + }, + wantErr: true, + wantErrMsg: "no runs found", + }, } for _, tt := range tests { diff --git a/pkg/cmd/run/shared/shared.go b/pkg/cmd/run/shared/shared.go index 94a5590a3..f5893b505 100644 --- a/pkg/cmd/run/shared/shared.go +++ b/pkg/cmd/run/shared/shared.go @@ -303,6 +303,7 @@ type FilterOptions struct { // avoid loading workflow name separately and use the provided one WorkflowName string Status string + Event string } // GetRunsWithFilter fetches 50 runs from the API and filters them in-memory @@ -348,6 +349,9 @@ func GetRuns(client *api.Client, repo ghrepo.Interface, opts *FilterOptions, lim if opts.Status != "" { path += fmt.Sprintf("&status=%s", url.QueryEscape(opts.Status)) } + if opts.Event != "" { + path += fmt.Sprintf("&event=%s", url.QueryEscape(opts.Event)) + } } var result *RunsPayload