Skip to content

Commit 1ddb33b

Browse files
committed
add GetRunsWithFilter
1 parent df58831 commit 1ddb33b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pkg/cmd/run/shared/shared.go

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

153+
func GetRunsWithFilter(client *api.Client, repo ghrepo.Interface, limit int, f func(Run) bool) ([]Run, error) {
154+
path := fmt.Sprintf("repos/%s/actions/runs", ghrepo.FullName(repo))
155+
runs, err := getRuns(client, repo, path, 50)
156+
if err != nil {
157+
return nil, err
158+
}
159+
filtered := []Run{}
160+
for _, run := range runs {
161+
if f(run) {
162+
filtered = append(filtered, run)
163+
}
164+
if len(filtered) == limit {
165+
break
166+
}
167+
}
168+
169+
return filtered, nil
170+
}
171+
153172
func GetRunsByWorkflow(client *api.Client, repo ghrepo.Interface, limit, workflowID int) ([]Run, error) {
154173
path := fmt.Sprintf("repos/%s/actions/workflows/%d/runs", ghrepo.FullName(repo), workflowID)
155174
return getRuns(client, repo, path, limit)
@@ -172,9 +191,14 @@ func getRuns(client *api.Client, repo ghrepo.Interface, path string, limit int)
172191
for len(runs) < limit {
173192
var result RunsPayload
174193

175-
pagedPath := fmt.Sprintf("%s?per_page=%d&page=%d", path, perPage, page)
194+
parsed, err := url.Parse(path)
195+
query := parsed.Query()
196+
query.Set("per_page", fmt.Sprintf("%d", perPage))
197+
query.Set("page", fmt.Sprintf("%d", page))
198+
parsed.RawQuery = query.Encode()
199+
pagedPath := parsed.String()
176200

177-
err := client.REST(repo.RepoHost(), "GET", pagedPath, nil, &result)
201+
err = client.REST(repo.RepoHost(), "GET", pagedPath, nil, &result)
178202
if err != nil {
179203
return nil, err
180204
}

0 commit comments

Comments
 (0)