Skip to content

Commit

Permalink
fix gitlab list branches paginated
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed May 27, 2024
1 parent f16b9b5 commit 1d8fbcf
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions pkg/tool/git/gitlab/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1d8fbcf

Please sign in to comment.