44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
|
|
package cmd
|
||
|
|
|
||
|
|
// paginateGitea walks pages of a gitea SDK list method until the response
|
||
|
|
// is short (last page) or we hit limit. limit=0 means unlimited.
|
||
|
|
//
|
||
|
|
// Forgejo/Gitea caps PageSize at 50, so naive `PageSize: limit` for limit > 50
|
||
|
|
// silently truncated results across most `fj * list` commands. This helper
|
||
|
|
// centralizes the loop so every list command paginates consistently.
|
||
|
|
//
|
||
|
|
// fetch is called with (page, pageSize) and returns the items for that page.
|
||
|
|
// The 1-based `page` matches the gitea SDK convention.
|
||
|
|
func paginateGitea[T any](limit int, fetch func(page, pageSize int) ([]T, error)) ([]T, error) {
|
||
|
|
const maxPageSize = 50
|
||
|
|
pageSize := maxPageSize
|
||
|
|
if limit > 0 && limit < pageSize {
|
||
|
|
pageSize = limit
|
||
|
|
}
|
||
|
|
|
||
|
|
var all []T
|
||
|
|
for page := 1; ; page++ {
|
||
|
|
if limit > 0 && len(all) >= limit {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
batch, err := fetch(page, pageSize)
|
||
|
|
if err != nil {
|
||
|
|
return all, err
|
||
|
|
}
|
||
|
|
if len(batch) == 0 {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
all = append(all, batch...)
|
||
|
|
// A short page (less than the requested size) is the conventional
|
||
|
|
// "you've reached the end" signal — saves one extra round-trip.
|
||
|
|
if len(batch) < pageSize {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if limit > 0 && len(all) > limit {
|
||
|
|
all = all[:limit]
|
||
|
|
}
|
||
|
|
return all, nil
|
||
|
|
}
|