Skip to content

Commit 6a462db

Browse files
authored
fix: Use Cursor pagination for *.ListHookDeliveriesIter (#4096)
1 parent 39ef794 commit 6a462db

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

github/gen-iterators.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type method struct {
121121
UseListOptions bool
122122
UsePage bool
123123
UseAfter bool
124+
UseCursor bool
124125
WrappedItemsField string
125126
TestJSON1 string
126127
TestJSON2 string
@@ -143,6 +144,15 @@ type methodInfo struct {
143144
UseListOptions bool
144145
UsePage bool
145146
UseAfter bool
147+
UseCursor bool
148+
}
149+
150+
// useCursorPagination identifies method names that require `Cursor` pagination
151+
// instead of using `After`.
152+
var useCursorPagination = map[string]bool{
153+
"AppsService.ListHookDeliveries": true,
154+
"OrganizationsService.ListHookDeliveries": true,
155+
"RepositoriesService.ListHookDeliveries": true,
146156
}
147157

148158
// customTestJSON maps method names to the JSON response they expect in tests.
@@ -391,13 +401,18 @@ func (t *templateData) collectMethodInfo(fd *ast.FuncDecl) (*methodInfo, bool) {
391401
useListOptions := t.hasListOptions(optsType)
392402
usePage := t.hasIntPage(optsType)
393403
useAfter := t.hasStringAfter(optsType)
404+
recType := strings.TrimPrefix(recvType, "*")
405+
var useCursor bool
406+
if useCursorPagination[recType+"."+fd.Name.Name] {
407+
useCursor = true
408+
useAfter = false
409+
}
394410

395-
if !useListCursorOptions && !useListOptions && !usePage && !useAfter {
411+
if !useListCursorOptions && !useListOptions && !usePage && !useAfter && !useCursor {
396412
logf("Skipping %v.%v: opts %v does not have ListCursorOptions, ListOptions, Page int, or After string", recvType, fd.Name.Name, optsType)
397413
return nil, false
398414
}
399415

400-
recType := strings.TrimPrefix(recvType, "*")
401416
clientField := strings.TrimSuffix(recType, "Service")
402417
if clientField == "Migration" {
403418
clientField = "Migrations"
@@ -422,6 +437,7 @@ func (t *templateData) collectMethodInfo(fd *ast.FuncDecl) (*methodInfo, bool) {
422437
UseListOptions: useListOptions,
423438
UsePage: usePage,
424439
UseAfter: useAfter,
440+
UseCursor: useCursor,
425441
}, true
426442
}
427443

@@ -457,6 +473,7 @@ func (t *templateData) processReturnArrayType(fd *ast.FuncDecl, sliceRet *ast.Ar
457473
UseListOptions: methodInfo.UseListOptions,
458474
UsePage: methodInfo.UsePage,
459475
UseAfter: methodInfo.UseAfter,
476+
UseCursor: methodInfo.UseCursor,
460477
TestJSON1: testJSON1,
461478
TestJSON2: testJSON2,
462479
TestJSON3: testJSON3,
@@ -514,6 +531,7 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
514531
UseListOptions: methodInfo.UseListOptions,
515532
UsePage: methodInfo.UsePage,
516533
UseAfter: methodInfo.UseAfter,
534+
UseCursor: methodInfo.UseCursor,
517535
WrappedItemsField: itemsField,
518536
TestJSON1: testJSON1,
519537
TestJSON2: testJSON2,
@@ -669,6 +687,11 @@ func ({{.RecvVar}} *{{.RecvType}}) {{.IterMethod}}({{.Args}}) iter.Seq2[{{.Retur
669687
break
670688
}
671689
{{.OptsName}}.After = resp.After
690+
{{else if .UseCursor}}
691+
if resp.Cursor == "" {
692+
break
693+
}
694+
{{.OptsName}}.Cursor = resp.Cursor
672695
{{end -}}
673696
}
674697
}
@@ -694,7 +717,9 @@ func Test{{.RecvType}}_{{.IterMethod}}(t *testing.T) {
694717
callNum++
695718
switch callNum {
696719
case 1:
697-
{{- if or .UseListCursorOptions .UseAfter}}
720+
{{- if .UseCursor}}
721+
w.Header().Set("Link", ` + "`" + `<https://api.github.com/?cursor=yo>; rel="next"` + "`" + `)
722+
{{else if or .UseListCursorOptions .UseAfter}}
698723
w.Header().Set("Link", ` + "`" + `<https://api.github.com/?after=yo>; rel="next"` + "`" + `)
699724
{{else}}
700725
w.Header().Set("Link", ` + "`" + `<https://api.github.com/?page=1>; rel="next"` + "`" + `)

github/github-iterators.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-iterators_test.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)