Skip to content

Commit

Permalink
Refactor GetJobs to use RESTWithNext
Browse files Browse the repository at this point in the history
  • Loading branch information
harveysanders committed Sep 2, 2023
1 parent 656d838 commit a454748
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions pkg/cmd/run/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
Expand Down Expand Up @@ -432,25 +433,26 @@ func GetJobs(client *api.Client, repo ghrepo.Interface, run *Run) ([]Job, error)
if run.Jobs != nil {
return run.Jobs, nil
}
var result JobsPayload
perPage := 100

v := url.Values{}
v.Set("per_page", fmt.Sprintf("%d", perPage))

// Fetch all the pages until total fetched jobs == result.TotalCount
maxPages := 10
for p := 1; p < maxPages; p++ {
v.Set("page", fmt.Sprintf("%d", p))
jobsPath := fmt.Sprintf("%s?%s", run.JobsURL, v.Encode())
if err := client.REST(repo.RepoHost(), "GET", jobsPath, nil, &result); err != nil {
v.Set("per_page", "100")

jobsPath := fmt.Sprintf("%s?%s", run.JobsURL, v.Encode())

for jobsPath != "" {
var resp JobsPayload
var err error
jobsPath, err = client.RESTWithNext(repo.RepoHost(), http.MethodGet, jobsPath, nil, &resp)
if err != nil {
return run.Jobs, err
}
run.Jobs = append(run.Jobs, result.Jobs...)
if len(run.Jobs) >= result.TotalCount {
break

run.Jobs = append(run.Jobs, resp.Jobs...)
if len(run.Jobs) >= resp.TotalCount {
return run.Jobs, nil
}
}
return result.Jobs, nil
return run.Jobs, nil
}

func GetJob(client *api.Client, repo ghrepo.Interface, jobID string) (*Job, error) {
Expand Down

0 comments on commit a454748

Please sign in to comment.