Skip to content

Commit a8bfc16

Browse files
authored
chore: GetLabels to paginate the GitHub responses (#1928)
1 parent 6a16a13 commit a8bfc16

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

internal/github/github.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,24 @@ func (c *Client) CreatePullRequest(ctx context.Context, repo *Repository, remote
166166
// GetLabels fetches the labels for an issue.
167167
func (c *Client) GetLabels(ctx context.Context, number int) ([]string, error) {
168168
slog.Info("Getting labels", "number", number)
169-
labels, _, err := c.Issues.ListLabelsByIssue(ctx, c.repo.Owner, c.repo.Name, number, nil)
170-
if err != nil {
171-
return nil, err
169+
var allLabels []string
170+
opts := &github.ListOptions{
171+
PerPage: 100,
172172
}
173-
var labelNames []string
174-
for _, label := range labels {
175-
labelNames = append(labelNames, *label.Name)
173+
for {
174+
labels, resp, err := c.Issues.ListLabelsByIssue(ctx, c.repo.Owner, c.repo.Name, number, opts)
175+
if err != nil {
176+
return nil, err
177+
}
178+
for _, label := range labels {
179+
allLabels = append(allLabels, *label.Name)
180+
}
181+
if resp.NextPage == 0 {
182+
break
183+
}
184+
opts.Page = resp.NextPage
176185
}
177-
return labelNames, nil
186+
return allLabels, nil
178187
}
179188

180189
// ReplaceLabels replaces all labels for an issue.

internal/github/github_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,19 @@ func TestGetLabels(t *testing.T) {
560560
issueNum: 7,
561561
wantLabels: []string{"label1", "label2"},
562562
},
563+
{
564+
name: "get labels with pagination",
565+
handler: func(w http.ResponseWriter, r *http.Request) {
566+
if r.URL.Query().Get("page") == "2" {
567+
fmt.Fprint(w, `[{"name": "label3"}]`)
568+
return
569+
}
570+
w.Header().Set("Link", `<http://`+r.Host+`/repos/owner/repo/issues/7/labels?page=2>; rel="next"`)
571+
fmt.Fprint(w, `[{"name": "label1"}, {"name": "label2"}]`)
572+
},
573+
issueNum: 7,
574+
wantLabels: []string{"label1", "label2", "label3"},
575+
},
563576
{
564577
name: "GitHub API error",
565578
handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) },

0 commit comments

Comments
 (0)