Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pagination helper #1741

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,3 +950,48 @@ func parseError(raw interface{}) string {
return fmt.Sprintf("failed to parse unexpected error type: %T", raw)
}
}

// Helper method to get all items with pagination.
// Allows passing an abort function to define early aborts
// eg. for only certain pages or until a condition (id > xxx) is met.
// Also allows passing a process method that allows to process
// the items after each page instead of waiting for all pages to finish.
func GetItemsPaginated[I any](
searchFunc func(pageNum int) ([]I, *Response, error),
abortFunc func(items []I, pageNum int) (bool, error),
processFunc func(item I) error) ([]I, error) {
items := []I{}
pageNum := 1
for {
// Get the items
tempItems, response, err := searchFunc(pageNum)
if err != nil {
return nil, err
}
// Process the items
if processFunc != nil {
for _, item := range tempItems {
err = processFunc(item)
if err != nil {
return nil, err
}
}
}
// Calculate abort
abort := false
if abortFunc != nil {
abort, err = abortFunc(tempItems, pageNum)
if err != nil {
return nil, err
}
}
// Append the items
items = append(items, tempItems...)
// Abort correctly
if response.NextPage == 0 || abort {
break
}
pageNum = response.NextPage
}
return items, nil
}