diff --git a/pkg/tool/git/gitlab/branch.go b/pkg/tool/git/gitlab/branch.go index 841d9f5d2d..88c36dde20 100644 --- a/pkg/tool/git/gitlab/branch.go +++ b/pkg/tool/git/gitlab/branch.go @@ -16,36 +16,45 @@ limitations under the License. package gitlab -import ( - "github.com/koderover/zadig/v2/pkg/tool/log" - "github.com/xanzy/go-gitlab" -) +import "github.com/xanzy/go-gitlab" // ListBranches lists branches by projectID <- urlEncode(namespace/projectName) func (c *Client) ListBranches(owner, repo, key string, opts *ListOptions) ([]*gitlab.Branch, error) { - branches, err := wrap(paginated(func(o *gitlab.ListOptions) ([]interface{}, *gitlab.Response, error) { - log.Debugf("opts: %+v", opts) - bs, r, err := c.Branches.ListBranches(generateProjectName(owner, repo), &gitlab.ListBranchesOptions{ListOptions: *o, Search: &key}) - log.Debugf("count of branches: %d", len(bs)) - var res []interface{} - for _, b := range bs { - res = append(res, b) + got := 0 + req := opts.PerPage + limit := 100 + res := []*gitlab.Branch{} + + opts.Page = 1 + opts.PerPage = limit + for got < req { + branches, err := wrap(paginated(func(o *gitlab.ListOptions) ([]interface{}, *gitlab.Response, error) { + bs, r, err := c.Branches.ListBranches(generateProjectName(owner, repo), &gitlab.ListBranchesOptions{ListOptions: *o, Search: &key}) + var res []interface{} + for _, b := range bs { + res = append(res, b) + } + return res, r, err + }, opts)) + + if err != nil { + return nil, err } - return res, r, err - }, opts)) - if err != nil { - return nil, err - } + bs, ok := branches.([]interface{}) + if !ok { + return nil, nil + } + for _, b := range bs { + res = append(res, b.(*gitlab.Branch)) + } - var res []*gitlab.Branch - bs, ok := branches.([]interface{}) - if !ok { - return nil, nil - } - for _, b := range bs { - res = append(res, b.(*gitlab.Branch)) + got += len(bs) + if len(bs) < limit { + break + } + opts.Page++ } - return res, err + return res, nil }