support --workflow in run list

This commit is contained in:
vilmibm 2021-03-28 14:02:26 -05:00
parent 6dba073a23
commit a225173551
4 changed files with 62 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/run/shared"
workflowShared "github.com/cli/cli/pkg/cmd/workflow/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/utils"
@ -24,7 +25,8 @@ type ListOptions struct {
PlainOutput bool
Limit int
Limit int
WorkflowSelector string
}
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
}
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of runs to fetch")
cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow")
return cmd
}
@ -74,8 +77,20 @@ func listRun(opts *ListOptions) error {
}
client := api.NewClientFromHTTP(c)
var runs []shared.Run
var workflow *workflowShared.Workflow
opts.IO.StartProgressIndicator()
runs, err := shared.GetRuns(client, baseRepo, opts.Limit)
if opts.WorkflowSelector != "" {
states := []workflowShared.WorkflowState{workflowShared.Active}
workflow, err = workflowShared.ResolveWorkflow(
opts.IO, client, baseRepo, false, opts.WorkflowSelector, states)
if err == nil {
runs, err = shared.GetRunsByWorkflow(client, baseRepo, opts.Limit, workflow.ID)
}
} else {
runs, err = shared.GetRuns(client, baseRepo, opts.Limit)
}
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)

View file

@ -9,6 +9,7 @@ import (
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/run/shared"
workflowShared "github.com/cli/cli/pkg/cmd/workflow/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/httpmock"
"github.com/cli/cli/pkg/iostreams"
@ -42,6 +43,14 @@ func TestNewCmdList(t *testing.T) {
cli: "--limit hi",
wantsErr: true,
},
{
name: "workflow",
cli: "--workflow foo.yml",
wants: ListOptions{
Limit: defaultLimit,
WorkflowSelector: "foo.yml",
},
},
}
for _, tt := range tests {
@ -171,6 +180,24 @@ func TestListRun(t *testing.T) {
wantOut: "",
wantErrOut: "No runs found\n",
},
{
name: "workflow selector",
opts: &ListOptions{
Limit: defaultLimit,
WorkflowSelector: "flow.yml",
},
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/flow.yml"),
httpmock.JSONResponse(workflowShared.AWorkflow))
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123/runs"),
httpmock.JSONResponse(shared.RunsPayload{
WorkflowRuns: shared.WorkflowRuns,
}))
},
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",
},
}
for _, tt := range tests {

View file

@ -150,7 +150,17 @@ type RunsPayload struct {
WorkflowRuns []Run `json:"workflow_runs"`
}
func GetRunsByWorkflow(client *api.Client, repo ghrepo.Interface, limit, workflowID int) ([]Run, error) {
path := fmt.Sprintf("repos/%s/actions/workflows/%d/runs", ghrepo.FullName(repo), workflowID)
return getRuns(client, repo, path, limit)
}
func GetRuns(client *api.Client, repo ghrepo.Interface, limit int) ([]Run, error) {
path := fmt.Sprintf("repos/%s/actions/runs", ghrepo.FullName(repo))
return getRuns(client, repo, path, limit)
}
func getRuns(client *api.Client, repo ghrepo.Interface, path string, limit int) ([]Run, error) {
perPage := limit
page := 1
if limit > 100 {
@ -162,9 +172,9 @@ func GetRuns(client *api.Client, repo ghrepo.Interface, limit int) ([]Run, error
for len(runs) < limit {
var result RunsPayload
path := fmt.Sprintf("repos/%s/actions/runs?per_page=%d&page=%d", ghrepo.FullName(repo), perPage, page)
pagedPath := fmt.Sprintf("%s?per_page=%d&page=%d", path, perPage, page)
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
err := client.REST(repo.RepoHost(), "GET", pagedPath, nil, &result)
if err != nil {
return nil, err
}

View file

@ -55,6 +55,12 @@ var TestRuns []Run = []Run{
TestRun("stale", 10, Completed, Stale),
}
var WorkflowRuns []Run = []Run{
TestRun("in progress", 2, InProgress, ""),
SuccessfulRun,
FailedRun,
}
var SuccessfulJob Job = Job{
ID: 10,
Status: Completed,