diff --git a/README.md b/README.md index f014dfb60f9..b0308f750ad 100644 --- a/README.md +++ b/README.md @@ -367,7 +367,7 @@ Go v1.23 introduces the new `iter` package. The new `github/gen-iterators.go` file auto-generates "*Iter" methods in `github/github-iterators.go` for all methods that support page number iteration (using the `NextPage` field in each response) -or string cursor iteration (using the `Cursor` field in each response). +or string cursor iteration (using the `After` field in each response). To handle rate limiting issues, make sure to use a rate-limiting transport. (See [Rate Limiting](/#rate-limiting) above for more details.) To use these methods, simply create an iterator and then range over it, for example: diff --git a/github/gen-iterators.go b/github/gen-iterators.go index 242863ac20e..5f48a81620b 100644 --- a/github/gen-iterators.go +++ b/github/gen-iterators.go @@ -448,16 +448,16 @@ func ({{.RecvVar}} *{{.RecvType}}) {{.IterMethod}}({{.Args}}) iter.Seq2[{{.Retur } {{if and .UseListCursorOptions .UseListOptions}} - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - {{.OptsName}}.ListCursorOptions.Cursor = resp.Cursor + {{.OptsName}}.ListCursorOptions.After = resp.After {{.OptsName}}.ListOptions.Page = resp.NextPage {{else if .UseListCursorOptions}} - if resp.Cursor == "" { + if resp.After == "" { break } - {{.OptsName}}.ListCursorOptions.Cursor = resp.Cursor + {{.OptsName}}.ListCursorOptions.After = resp.After {{else if .UseListOptions}} if resp.NextPage == 0 { break @@ -494,7 +494,7 @@ func Test{{.RecvType}}_{{.IterMethod}}(t *testing.T) { switch callNum { case 1: {{- if .UseListCursorOptions}} - w.Header().Set("Link", ` + "`" + `; rel="next"` + "`" + `) + w.Header().Set("Link", ` + "`" + `; rel="next"` + "`" + `) {{else}} w.Header().Set("Link", ` + "`" + `; rel="next"` + "`" + `) {{end -}} diff --git a/github/github-iterators.go b/github/github-iterators.go index 90a057e63d2..ddeb888747f 100644 --- a/github/github-iterators.go +++ b/github/github-iterators.go @@ -719,10 +719,10 @@ func (s *CodeScanningService) ListAlertsForOrgIter(ctx context.Context, org stri } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -751,10 +751,10 @@ func (s *CodeScanningService) ListAlertsForRepoIter(ctx context.Context, owner s } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -814,10 +814,10 @@ func (s *DependabotService) ListOrgAlertsIter(ctx context.Context, org string, o } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -846,10 +846,10 @@ func (s *DependabotService) ListRepoAlertsIter(ctx context.Context, owner string } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -1405,10 +1405,10 @@ func (s *IssuesService) ListByRepoIter(ctx context.Context, owner string, repo s } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -3545,10 +3545,10 @@ func (s *SecretScanningService) ListAlertsForEnterpriseIter(ctx context.Context, } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -3577,10 +3577,10 @@ func (s *SecretScanningService) ListAlertsForOrgIter(ctx context.Context, org st } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -3609,10 +3609,10 @@ func (s *SecretScanningService) ListAlertsForRepoIter(ctx context.Context, owner } } - if resp.Cursor == "" && resp.NextPage == 0 { + if resp.After == "" && resp.NextPage == 0 { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After opts.ListOptions.Page = resp.NextPage } } @@ -3672,10 +3672,10 @@ func (s *SecurityAdvisoriesService) ListGlobalSecurityAdvisoriesIter(ctx context } } - if resp.Cursor == "" { + if resp.After == "" { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After } } } @@ -3703,10 +3703,10 @@ func (s *SecurityAdvisoriesService) ListRepositorySecurityAdvisoriesIter(ctx con } } - if resp.Cursor == "" { + if resp.After == "" { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After } } } @@ -3734,10 +3734,10 @@ func (s *SecurityAdvisoriesService) ListRepositorySecurityAdvisoriesForOrgIter(c } } - if resp.Cursor == "" { + if resp.After == "" { break } - opts.ListCursorOptions.Cursor = resp.Cursor + opts.ListCursorOptions.After = resp.After } } } diff --git a/github/github-iterators_test.go b/github/github-iterators_test.go index 85a0bca6431..b8412d9dfa6 100644 --- a/github/github-iterators_test.go +++ b/github/github-iterators_test.go @@ -1607,7 +1607,7 @@ func TestCodeScanningService_ListAlertsForOrgIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -1679,7 +1679,7 @@ func TestCodeScanningService_ListAlertsForRepoIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -1823,7 +1823,7 @@ func TestDependabotService_ListOrgAlertsIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -1895,7 +1895,7 @@ func TestDependabotService_ListRepoAlertsIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -3191,7 +3191,7 @@ func TestIssuesService_ListByRepoIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8159,7 +8159,7 @@ func TestSecretScanningService_ListAlertsForEnterpriseIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8231,7 +8231,7 @@ func TestSecretScanningService_ListAlertsForOrgIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8303,7 +8303,7 @@ func TestSecretScanningService_ListAlertsForRepoIter(t *testing.T) { callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8447,7 +8447,7 @@ func TestSecurityAdvisoriesService_ListGlobalSecurityAdvisoriesIter(t *testing.T callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8519,7 +8519,7 @@ func TestSecurityAdvisoriesService_ListRepositorySecurityAdvisoriesIter(t *testi callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) @@ -8591,7 +8591,7 @@ func TestSecurityAdvisoriesService_ListRepositorySecurityAdvisoriesForOrgIter(t callNum++ switch callNum { case 1: - w.Header().Set("Link", `; rel="next"`) + w.Header().Set("Link", `; rel="next"`) fmt.Fprint(w, `[{},{},{}]`) case 2: fmt.Fprint(w, `[{},{},{},{}]`) diff --git a/test/integration/pagination_test.go b/test/integration/pagination_test.go new file mode 100644 index 00000000000..567058b365e --- /dev/null +++ b/test/integration/pagination_test.go @@ -0,0 +1,38 @@ +// Copyright 2026 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build integration + +package integration + +import ( + "testing" + + "github.com/google/go-github/v82/github" +) + +func TestSecurityAdvisories_ListGlobalSecurityAdvisories(t *testing.T) { + opt := &github.ListGlobalSecurityAdvisoriesOptions{ + ListCursorOptions: github.ListCursorOptions{ + PerPage: 2, + }, + } + advisories, resp, err := client.SecurityAdvisories.ListGlobalSecurityAdvisories(t.Context(), opt) + if err != nil { + t.Fatalf("ListGlobalSecurityAdvisories returned error: %v", err) + } + + if got, want := len(advisories), 2; got != want { + t.Errorf("ListGlobalSecurityAdvisories returned %v advisories, want %v", got, want) + } + + if resp.After == "" { + t.Error("ListGlobalSecurityAdvisories returned an empty 'after' cursor") + } + + if resp.Cursor != "" { + t.Error("ListGlobalSecurityAdvisories returned a non-empty 'cursor' value") + } +}