X Tutup
Skip to content

Commit 5b3dfbf

Browse files
author
vilmibm
committed
fetch workflows
1 parent bad200d commit 5b3dfbf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

pkg/cmd/workflow/list/list.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66

7+
"github.com/cli/cli/api"
78
"github.com/cli/cli/internal/ghrepo"
89
"github.com/cli/cli/pkg/cmdutil"
910
"github.com/cli/cli/pkg/iostreams"
@@ -60,5 +61,75 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
6061
}
6162

6263
func listRun(opts *ListOptions) error {
64+
repo, err := opts.BaseRepo()
65+
if err != nil {
66+
return fmt.Errorf("could not determine base repo: %w", err)
67+
}
68+
69+
httpClient, err := opts.HttpClient()
70+
if err != nil {
71+
return fmt.Errorf("could not create http client: %w", err)
72+
}
73+
client := api.NewClientFromHTTP(httpClient)
74+
75+
workflows, err := getWorkflows(client, repo, opts.Limit)
76+
if err != nil {
77+
return fmt.Errorf("could not get workflows: %w", err)
78+
}
79+
80+
out := opts.IO.Out
81+
82+
for _, workflow := range workflows {
83+
fmt.Fprintf(out, "%s\n", workflow.Name)
84+
}
85+
6386
return nil
6487
}
88+
89+
type Workflow struct {
90+
Name string
91+
}
92+
93+
type WorkflowsPayload struct {
94+
Workflows []Workflow
95+
}
96+
97+
func getWorkflows(client *api.Client, repo ghrepo.Interface, limit int) ([]Workflow, error) {
98+
perPage := limit
99+
page := 1
100+
if limit > 100 {
101+
perPage = 100
102+
}
103+
104+
workflows := []Workflow{}
105+
106+
for len(workflows) < limit {
107+
var result WorkflowsPayload
108+
109+
path := fmt.Sprintf("repos/%s/actions/workflows?per_page=%d&page=%d", ghrepo.FullName(repo), perPage, page)
110+
111+
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
if len(result.Workflows) == 0 {
117+
break
118+
}
119+
120+
for _, workflow := range result.Workflows {
121+
workflows = append(workflows, workflow)
122+
if len(workflows) == limit {
123+
break
124+
}
125+
}
126+
127+
if len(result.Workflows) < perPage {
128+
break
129+
}
130+
131+
page++
132+
}
133+
134+
return workflows, nil
135+
}

0 commit comments

Comments
 (0)
X Tutup