-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgh.go
More file actions
694 lines (634 loc) · 19.6 KB
/
gh.go
File metadata and controls
694 lines (634 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
package gh
import (
"context"
"fmt"
"io"
"maps"
"slices"
"strings"
"time"
"github.com/google/go-github/v63/github"
"github.com/multimediallc/codeowners-plus/internal/git"
"github.com/multimediallc/codeowners-plus/pkg/codeowners"
f "github.com/multimediallc/codeowners-plus/pkg/functional"
)
type NoPRError struct{}
func (e NoPRError) Error() string {
return "PR not initialized"
}
type UserReviewerMapNotInitError struct{}
func (e UserReviewerMapNotInitError) Error() string {
return "User reviewer map not initialized"
}
type Client interface {
SetWarningBuffer(writer io.Writer)
SetInfoBuffer(writer io.Writer)
InitPR(pr_id int) error
PR() *github.PullRequest
InitUserReviewerMap(reviewers []string) error
GetTokenUser() (string, error)
InitReviews() error
AllApprovals() ([]*CurrentApproval, error)
FindUserApproval(ghUser string) (*CurrentApproval, error)
GetCurrentReviewerApprovals() ([]*CurrentApproval, error)
GetAlreadyReviewed() ([]codeowners.Slug, error)
GetReviewedButNotApproved() ([]codeowners.Slug, error)
GetCurrentlyRequested() ([]codeowners.Slug, error)
DismissStaleReviews(staleApprovals []*CurrentApproval) error
RequestReviewers(reviewers []string) error
ApprovePR() error
InitComments() error
AddComment(comment string) error
FindExistingComment(prefix string, since *time.Time) (int64, bool, error)
UpdateComment(commentID int64, body string) error
IsInComments(comment string, since *time.Time) (bool, error)
IsSubstringInComments(substring string, since *time.Time) (bool, error)
CheckApprovals(fileReviewerMap map[string][]string, approvals []*CurrentApproval, originalDiff git.Diff) (approvers []codeowners.Slug, staleApprovals []*CurrentApproval)
IsInLabels(labels []string) (bool, error)
IsRepositoryAdmin(username string) (bool, error)
ContainsValidBypassApproval(allowedUsers []string) (bool, error)
}
type GHClient struct {
ctx context.Context
owner string
repo string
client *github.Client
pr *github.PullRequest
userReviewerMap ghUserReviewerMap
comments []*github.IssueComment
reviews []*github.PullRequestReview
warningBuffer io.Writer
infoBuffer io.Writer
}
func NewClient(owner, repo, token string) Client {
client := github.NewClient(nil).WithAuthToken(token)
return &GHClient{
context.Background(),
owner,
repo,
client,
nil,
nil,
nil,
nil,
io.Discard,
io.Discard,
}
}
func (gh *GHClient) PR() *github.PullRequest {
return gh.pr
}
func (gh *GHClient) SetWarningBuffer(writer io.Writer) {
gh.warningBuffer = writer
}
func (gh *GHClient) SetInfoBuffer(writer io.Writer) {
gh.infoBuffer = writer
}
func (gh *GHClient) InitPR(pr_id int) error {
pull, res, err := gh.client.PullRequests.Get(gh.ctx, gh.owner, gh.repo, pr_id)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
gh.pr = pull
return nil
}
func (gh *GHClient) InitUserReviewerMap(reviewers []string) error {
teamFetch := func(org, team string) []*github.User {
_, _ = fmt.Fprintf(gh.infoBuffer, "Fetching team members for %s/%s\n", org, team)
allUsers := make([]*github.User, 0)
getMembers := func(page int) (*github.Response, error) {
listOptions := &github.TeamListTeamMembersOptions{ListOptions: github.ListOptions{PerPage: 100, Page: page}}
users, res, err := gh.client.Teams.ListTeamMembersBySlug(gh.ctx, org, team, listOptions)
if err != nil {
_, _ = fmt.Fprintf(gh.warningBuffer, "WARNING: Error fetching team members for %s/%s: %v\n", org, team, err)
}
allUsers = append(allUsers, users...)
return res, err
}
err := walkPaginatedApi(getMembers)
if err != nil {
_, _ = fmt.Fprintf(gh.warningBuffer, "WARNING: Error fetching team members: %v\n", err)
}
return allUsers
}
gh.userReviewerMap = makeGHUserReviwerMap(reviewers, teamFetch)
return nil
}
func (gh *GHClient) GetTokenUser() (string, error) {
user, _, err := gh.client.Users.Get(gh.ctx, "")
if err != nil {
return "", err
}
return user.GetLogin(), nil
}
func (gh *GHClient) InitReviews() error {
if gh.pr == nil {
return &NoPRError{}
}
allReviews := make([]*github.PullRequestReview, 0)
listReviews := func(page int) (*github.Response, error) {
listOptions := &github.ListOptions{PerPage: 100, Page: page}
reviews, res, err := gh.client.PullRequests.ListReviews(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), listOptions)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
allReviews = append(allReviews, reviews...)
return res, err
}
err := walkPaginatedApi(listReviews)
if err != nil {
return err
}
allReviews = f.Filtered(allReviews, func(review *github.PullRequestReview) bool {
return review.User.GetLogin() != gh.pr.User.GetLogin()
})
// use descending chronological order (default ascending)
slices.Reverse(allReviews)
gh.reviews = allReviews
return nil
}
func (gh *GHClient) approvals() []*github.PullRequestReview {
seen := make(map[string]bool, 0)
approvals := f.Filtered(gh.reviews, func(approval *github.PullRequestReview) bool {
userName := strings.ToLower(approval.GetUser().GetLogin())
if _, ok := seen[userName]; ok {
// we only care about the most recent reviews for each user
return false
} else if approval.GetState() == "APPROVED" {
seen[userName] = true
}
return approval.GetState() == "APPROVED"
})
return approvals
}
// ContainsValidBypassApproval checks if any approval is a valid admin bypass approval
func (gh *GHClient) ContainsValidBypassApproval(allowedUsers []string) (bool, error) {
if gh.pr == nil {
return false, &NoPRError{}
}
if gh.reviews == nil {
err := gh.InitReviews()
if err != nil {
return false, err
}
}
for _, review := range gh.reviews {
// Check if the review body contains bypass text
body := review.GetBody()
if !strings.Contains(strings.ToLower(body), "codeowners bypass") {
continue
}
username := review.GetUser().GetLogin()
usernameSlug := codeowners.NewSlug(username)
// Check if user is in allowed users list
for _, allowedUser := range allowedUsers {
if usernameSlug.EqualsString(allowedUser) {
return true, nil
}
}
// Check if user is repository admin
isAdmin, err := gh.IsRepositoryAdmin(username)
if err != nil {
// Log error but continue checking other approvals
_, _ = fmt.Fprintf(gh.warningBuffer, "Warning: Could not check admin status for user %s: %v\n", username, err)
continue
}
if isAdmin {
return true, nil
}
}
return false, nil
}
func (gh *GHClient) AllApprovals() ([]*CurrentApproval, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.reviews == nil {
err := gh.InitReviews()
if err != nil {
return nil, err
}
}
return f.Map(gh.approvals(), func(approval *github.PullRequestReview) *CurrentApproval {
return &CurrentApproval{codeowners.NewSlug(approval.User.GetLogin()), approval.GetID(), nil, approval.GetCommitID()}
}), nil
}
func (gh *GHClient) FindUserApproval(ghUser string) (*CurrentApproval, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.reviews == nil {
err := gh.InitReviews()
if err != nil {
return nil, err
}
}
ghUserSlug := codeowners.NewSlug(ghUser)
review, found := f.Find(gh.approvals(), func(review *github.PullRequestReview) bool {
return ghUserSlug.EqualsString(review.GetUser().GetLogin())
})
if !found {
return nil, nil
}
return &CurrentApproval{codeowners.NewSlug(review.User.GetLogin()), review.GetID(), nil, review.GetCommitID()}, nil
}
func (gh *GHClient) GetCurrentReviewerApprovals() ([]*CurrentApproval, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.userReviewerMap == nil {
return nil, &UserReviewerMapNotInitError{}
}
if gh.reviews == nil {
err := gh.InitReviews()
if err != nil {
return nil, err
}
}
return currentReviewerApprovalsFromReviews(gh.approvals(), gh.userReviewerMap), nil
}
func currentReviewerApprovalsFromReviews(approvals []*github.PullRequestReview, userReviewerMap ghUserReviewerMap) []*CurrentApproval {
filteredApprovals := make([]*CurrentApproval, 0, len(approvals))
for _, review := range approvals {
reviewingUser := review.GetUser().GetLogin()
reviewingUserSlug := codeowners.NewSlug(reviewingUser)
if reviewers, ok := userReviewerMap[strings.ToLower(reviewingUser)]; ok {
newApproval := &CurrentApproval{reviewingUserSlug, review.GetID(), reviewers, review.GetCommitID()}
filteredApprovals = append(filteredApprovals, newApproval)
} else {
newApproval := &CurrentApproval{reviewingUserSlug, review.GetID(), []codeowners.Slug{}, review.GetCommitID()}
filteredApprovals = append(filteredApprovals, newApproval)
}
}
return filteredApprovals
}
func (gh *GHClient) GetAlreadyReviewed() ([]codeowners.Slug, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.userReviewerMap == nil {
return nil, &UserReviewerMapNotInitError{}
}
return reviewerAlreadyReviewed(gh.reviews, gh.userReviewerMap), nil
}
func reviewerAlreadyReviewed(reviews []*github.PullRequestReview, userReviewerMap ghUserReviewerMap) []codeowners.Slug {
reviewsReviewers := make(map[string]codeowners.Slug, len(reviews))
for _, review := range reviews {
reviewingUser := review.GetUser().GetLogin()
if reviewers, ok := userReviewerMap[strings.ToLower(reviewingUser)]; ok {
for _, reviewer := range reviewers {
reviewsReviewers[reviewer.Normalized()] = reviewer
}
}
}
return slices.Collect(maps.Values(reviewsReviewers))
}
func (gh *GHClient) GetReviewedButNotApproved() ([]codeowners.Slug, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.userReviewerMap == nil {
return nil, &UserReviewerMapNotInitError{}
}
if gh.reviews == nil {
err := gh.InitReviews()
if err != nil {
return nil, err
}
}
return reviewerReviewedButNotApproved(gh.reviews, gh.userReviewerMap), nil
}
func reviewerReviewedButNotApproved(reviews []*github.PullRequestReview, userReviewerMap ghUserReviewerMap) []codeowners.Slug {
// Track the most recent review state for each user
// Since reviews are in descending chronological order (most recent first),
// the first occurrence of each user is their most recent review
userReviewStates := make(map[string]string)
for _, review := range reviews {
reviewingUser := strings.ToLower(review.GetUser().GetLogin())
// Only track if we haven't seen this user yet (most recent review)
if _, seen := userReviewStates[reviewingUser]; !seen {
state := review.GetState()
// Only track non-approved states (CHANGES_REQUESTED or COMMENTED)
if state == github.ReviewStateChangesRequested || state == github.ReviewStateCommented {
userReviewStates[reviewingUser] = state
}
}
}
reviewsReviewers := make(map[string]codeowners.Slug)
for reviewingUserLower := range userReviewStates {
// Include reviewers whose most recent review is CHANGES_REQUESTED or COMMENTED
if reviewers, ok := userReviewerMap[reviewingUserLower]; ok {
for _, reviewer := range reviewers {
reviewsReviewers[reviewer.Normalized()] = reviewer
}
}
}
return slices.Collect(maps.Values(reviewsReviewers))
}
func (gh *GHClient) GetCurrentlyRequested() ([]codeowners.Slug, error) {
if gh.pr == nil {
return nil, &NoPRError{}
}
if gh.userReviewerMap == nil {
return nil, &UserReviewerMapNotInitError{}
}
return currentlyRequested(gh.pr, gh.owner, gh.userReviewerMap), nil
}
func currentlyRequested(pr *github.PullRequest, owner string, userReviewerMap ghUserReviewerMap) []codeowners.Slug {
requestedUsers := f.Map(pr.RequestedReviewers, func(user *github.User) string {
return user.GetLogin()
})
requestedTeams := f.Map(pr.RequestedTeams, func(team *github.Team) string {
return fmt.Sprintf("%s/%s", owner, team.GetSlug())
})
requested := slices.Concat(requestedUsers, requestedTeams)
reviewers := make([]codeowners.Slug, 0, len(requested))
for _, user := range requested {
if reviewer, ok := userReviewerMap[strings.ToLower(user)]; ok {
reviewers = append(reviewers, reviewer...)
}
}
// Deduplicate based on normalized form
seen := make(map[string]codeowners.Slug)
for _, rev := range reviewers {
seen[rev.Normalized()] = rev
}
return slices.Collect(maps.Values(seen))
}
func (gh *GHClient) DismissStaleReviews(staleApprovals []*CurrentApproval) error {
if gh.pr == nil {
return &NoPRError{}
}
staleMessage := "Changes in owned files since last approval"
for _, approval := range staleApprovals {
dismissRequest := &github.PullRequestReviewDismissalRequest{Message: &staleMessage}
_, res, err := gh.client.PullRequests.DismissReview(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), approval.ReviewID, dismissRequest)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
}
return nil
}
func (gh *GHClient) RequestReviewers(reviewers []string) error {
if gh.pr == nil {
return &NoPRError{}
}
if len(reviewers) == 0 {
return nil
}
indvidualReviewers, teamReviewers := splitReviewers(reviewers)
reviewersRequest := github.ReviewersRequest{Reviewers: indvidualReviewers, TeamReviewers: teamReviewers}
_, res, err := gh.client.PullRequests.RequestReviewers(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), reviewersRequest)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
return err
}
func splitReviewers(reviewers []string) ([]string, []string) {
indvidualReviewers := make([]string, 0, len(reviewers))
teamReviewers := make([]string, 0, len(reviewers))
for _, reviewer := range reviewers {
reviewerString := reviewer[1:] // trim the @
if strings.Contains(reviewerString, "/") {
split := strings.SplitN(reviewerString, "/", 2)
teamReviewers = append(teamReviewers, split[1])
} else {
indvidualReviewers = append(indvidualReviewers, reviewerString)
}
}
return indvidualReviewers, teamReviewers
}
func (gh *GHClient) ApprovePR() error {
if gh.pr == nil {
return &NoPRError{}
}
createReviewOptions := &github.PullRequestReviewRequest{
Event: github.String("APPROVE"),
Body: github.String("Codeowners reviews satisfied"),
}
_, res, err := gh.client.PullRequests.CreateReview(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), createReviewOptions)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
return err
}
func (gh *GHClient) InitComments() error {
if gh.pr == nil {
return &NoPRError{}
}
allComments := make([]*github.IssueComment, 0)
listReviews := func(page int) (*github.Response, error) {
listOptions := &github.IssueListCommentsOptions{ListOptions: github.ListOptions{PerPage: 100, Page: page}}
comments, res, err := gh.client.Issues.ListComments(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), listOptions)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
allComments = append(allComments, comments...)
return res, err
}
err := walkPaginatedApi(listReviews)
if err != nil {
return err
}
gh.comments = allComments
return nil
}
func (gh *GHClient) AddComment(comment string) error {
if gh.pr == nil {
return &NoPRError{}
}
createCommentOptions := &github.IssueComment{
Body: &comment,
}
_, res, err := gh.client.Issues.CreateComment(gh.ctx, gh.owner, gh.repo, gh.pr.GetNumber(), createCommentOptions)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
return err
}
func (gh *GHClient) FindExistingComment(prefix string, since *time.Time) (int64, bool, error) {
if gh.pr == nil {
return 0, false, &NoPRError{}
}
if err := gh.InitComments(); err != nil {
return 0, false, err
}
for _, comment := range gh.comments {
if since != nil && comment.GetCreatedAt().Before(*since) {
continue
}
if strings.HasPrefix(comment.GetBody(), prefix) {
return comment.GetID(), true, nil
}
}
return 0, false, nil
}
func (gh *GHClient) UpdateComment(commentID int64, body string) error {
if gh.pr == nil {
return &NoPRError{}
}
comment := &github.IssueComment{
Body: &body,
}
_, res, err := gh.client.Issues.EditComment(gh.ctx, gh.owner, gh.repo, commentID, comment)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
return nil
}
func (gh *GHClient) IsInComments(comment string, since *time.Time) (bool, error) {
if gh.pr == nil {
return false, &NoPRError{}
}
if gh.comments == nil {
if err := gh.InitComments(); err != nil {
_, _ = fmt.Fprintf(gh.warningBuffer, "WARNING: Error initializing comments: %v\n", err)
}
}
for _, c := range gh.comments {
if since != nil && c.GetCreatedAt().Before(*since) {
continue
}
if c.GetBody() == comment {
return true, nil
}
}
return false, nil
}
func (gh *GHClient) IsSubstringInComments(substring string, since *time.Time) (bool, error) {
if gh.pr == nil {
return false, &NoPRError{}
}
if gh.comments == nil {
if err := gh.InitComments(); err != nil {
_, _ = fmt.Fprintf(gh.warningBuffer, "WARNING: Error initializing comments: %v\n", err)
}
}
for _, c := range gh.comments {
if since != nil && c.GetCreatedAt().Before(*since) {
continue
}
if strings.Contains(c.GetBody(), substring) {
return true, nil
}
}
return false, nil
}
// IsInLabels checks if the PR has any of the given labels
func (gh *GHClient) IsInLabels(labels []string) (bool, error) {
if gh.pr == nil {
return false, &NoPRError{}
}
if len(labels) == 0 {
return false, nil
}
for _, label := range gh.pr.Labels {
for _, targetLabel := range labels {
if label.GetName() == targetLabel {
return true, nil
}
}
}
return false, nil
}
// Apply approver satisfaction to the owners map, and return the approvals which should be invalidated
func (gh *GHClient) CheckApprovals(
fileReviewerMap map[string][]string,
approvals []*CurrentApproval,
originalDiff git.Diff,
) (approvers []codeowners.Slug, staleApprovals []*CurrentApproval) {
appovalsWithDiff, badApprovals := getApprovalDiffs(approvals, originalDiff, gh.warningBuffer, gh.infoBuffer)
approvers, staleApprovals = checkStale(fileReviewerMap, appovalsWithDiff)
return approvers, append(badApprovals, staleApprovals...)
}
// IsRepositoryAdmin checks if a user has admin permissions for the repository
func (gh *GHClient) IsRepositoryAdmin(username string) (bool, error) {
permission, _, err := gh.client.Repositories.GetPermissionLevel(
gh.ctx,
gh.owner,
gh.repo,
username,
)
if err != nil {
return false, err
}
return permission.GetPermission() == "admin", nil
}
type ghUserReviewerMap map[string][]codeowners.Slug
type CurrentApproval struct {
GHLogin codeowners.Slug
ReviewID int64
Reviewers []codeowners.Slug
CommitID string
}
func (p *CurrentApproval) String() string {
return fmt.Sprintf("%+v", *p)
}
func makeGHUserReviwerMap(reviewers []string, teamFetcher func(string, string) []*github.User) ghUserReviewerMap {
userReviewerMap := make(ghUserReviewerMap)
insertReviewer := func(userName string, reviewer codeowners.Slug) {
reviewers, found := userReviewerMap[userName]
if found {
userReviewerMap[userName] = append(reviewers, reviewer)
} else {
userReviewerMap[userName] = []codeowners.Slug{reviewer}
}
}
for _, reviewer := range reviewers {
reviewerSlug := codeowners.NewSlug(reviewer)
reviewerString := reviewer[1:] // trim the @
// Add the team or user to the map
insertReviewer(strings.ToLower(reviewerString), reviewerSlug)
if !strings.Contains(reviewerString, "/") {
// This is a user
continue
}
split := strings.SplitN(reviewerString, "/", 2)
org := split[0]
team := split[1]
users := teamFetcher(org, team)
// Add the team members to the map
for _, user := range users {
insertReviewer(strings.ToLower(user.GetLogin()), reviewerSlug)
}
}
return userReviewerMap
}
func walkPaginatedApi(apiCall func(int) (*github.Response, error)) error {
page := 1
for {
res, err := apiCall(page)
if err != nil {
return err
}
if res.NextPage == 0 {
break
}
page = res.NextPage
}
return nil
}