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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination helper to easily collect all items #1498

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/pagination_helper.go
@@ -0,0 +1,67 @@
//
// Copyright 2021, Timo Furrer <tuxtimo@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"fmt"
"log"

"github.com/xanzy/go-gitlab"
)

func main() {
git, err := gitlab.NewClient("yourtokengoeshere")
if err != nil {
log.Fatal(err)
}

options := &gitlab.ListInstanceVariablesOptions{
Page: 1,
}
instanceVariables, err := gitlab.Collect(
git.InstanceVariables.ListVariables,
options,
)
fmt.Printf("%d\n", len(instanceVariables))

projectOptions := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
},
Archived: gitlab.Bool(false),
}
projects, err := gitlab.Collect(
git.Projects.ListProjects,
projectOptions,
)
fmt.Printf("%d\n", len(projects))


userProjectOptions := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
},
Archived: gitlab.Bool(false),
}
userProjects, err := gitlab.Collect(
func(opt *gitlab.ListProjectsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Project, *gitlab.Response, error) {
return git.Projects.ListUserProjects(1, opt, options...)
},
userProjectOptions,
)
fmt.Printf("%d\n", len(userProjects))
}
8 changes: 8 additions & 0 deletions instance_variables.go
Expand Up @@ -54,6 +54,14 @@ func (v InstanceVariable) String() string {
// https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#list-all-instance-variables
type ListInstanceVariablesOptions ListOptions

func (o *ListInstanceVariablesOptions) GetPage() int {
return o.Page
}

func (o *ListInstanceVariablesOptions) SetPage(page int) {
o.Page = page
}

// ListVariables gets a list of all variables for an instance.
//
// GitLab API docs:
Expand Down
31 changes: 31 additions & 0 deletions pagination_helpers.go
@@ -0,0 +1,31 @@

package gitlab

type ListableOptions interface{
GetPage() int
SetPage(int)
}

func (o *ListOptions) GetPage() int {
return o.Page
}

func (o *ListOptions) SetPage(page int) {
o.Page = page
}

func Collect[T any, U ListableOptions](f func(U, ...RequestOptionFunc) ([]*T, *Response, error), opt U, options ...RequestOptionFunc) ([]*T, error) {
var collection []*T
opt.SetPage(1)
for opt.GetPage() != 0 {
page, resp, err := f(opt, options...)
if err != nil {
return nil, err
}

collection = append(collection, page...)
opt.SetPage(resp.NextPage)
}

return collection, nil
}
7 changes: 7 additions & 0 deletions projects.go
Expand Up @@ -316,6 +316,13 @@ type ListProjectsOptions struct {
WithMergeRequestsEnabled *bool `url:"with_merge_requests_enabled,omitempty" json:"with_merge_requests_enabled,omitempty"`
WithProgrammingLanguage *string `url:"with_programming_language,omitempty" json:"with_programming_language,omitempty"`
}
func (o *ListProjectsOptions) GetPage() int {
return o.ListOptions.Page
}

func (o *ListProjectsOptions) SetPage(p int) {
o.ListOptions.Page = p
}

// ListProjects gets a list of projects accessible by the authenticated user.
//
Expand Down