add --event flag to gh run ls to filter runs by their trigger

This commit is contained in:
cawfeecake 2023-04-24 20:50:20 -07:00
parent 350011162a
commit d7512bb2e7
No known key found for this signature in database
GPG key ID: 75550832E7BBC274
3 changed files with 35 additions and 0 deletions

View file

@ -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()

View file

@ -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 {

View file

@ -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