Skip to content

Commit

Permalink
gitlab: fix page number check
Browse files Browse the repository at this point in the history
The current page number was being manually set to =1 in the beginning of the
count to avoid a infinite loop, but it isn't necessary, considering
go-gitlab offers the "CurrentPage" information from the API response object.
This patch checks for the library's struct member instead of forcing a magic
assignament.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed May 17, 2021
1 parent 354d89e commit de59b59
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ func MRListDiscussions(project string, mrNum int) ([]*gitlab.Discussion, error)
opt := &gitlab.ListMergeRequestDiscussionsOptions{
// 100 is the maximum allowed by the API
PerPage: maxItemsPerPage,
Page: 1,
}

for {
Expand All @@ -500,7 +499,7 @@ func MRListDiscussions(project string, mrNum int) ([]*gitlab.Discussion, error)
discussions = append(discussions, d...)

// if we've seen all the pages, then we can break here
if opt.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}

Expand Down Expand Up @@ -764,7 +763,6 @@ func IssueListDiscussions(project string, issueNum int) ([]*gitlab.Discussion, e
opt := &gitlab.ListIssueDiscussionsOptions{
// 100 is the maximum allowed by the API
PerPage: maxItemsPerPage,
Page: 1,
}

for {
Expand All @@ -778,7 +776,7 @@ func IssueListDiscussions(project string, issueNum int) ([]*gitlab.Discussion, e
discussions = append(discussions, d...)

// if we've seen all the pages, then we can break here
if opt.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}

Expand Down Expand Up @@ -833,7 +831,6 @@ func LabelList(project string) ([]*gitlab.Label, error) {
opt := &gitlab.ListLabelsOptions{
ListOptions: gitlab.ListOptions{
PerPage: maxItemsPerPage,
Page: 1,
},
}

Expand All @@ -846,7 +843,7 @@ func LabelList(project string) ([]*gitlab.Label, error) {
labels = append(labels, l...)

// if we've seen all the pages, then we can break here
if opt.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}

Expand Down Expand Up @@ -890,15 +887,14 @@ func BranchList(project string, opts *gitlab.ListBranchesOptions) ([]*gitlab.Bra
}

branches := []*gitlab.Branch{}
opts.ListOptions.Page = 1
for {
bList, resp, err := lab.Branches.ListBranches(p.ID, opts)
if err != nil {
return nil, err
}
branches = append(branches, bList...)

if opts.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}
opts.Page = resp.NextPage
Expand Down Expand Up @@ -931,7 +927,6 @@ func MilestoneList(project string, opt *gitlab.ListMilestonesOptions) ([]*gitlab
}

milestones := []*gitlab.Milestone{}
opt.ListOptions.Page = 1
for {
m, resp, err := lab.Milestones.ListMilestones(p.ID, opt)
if err != nil {
Expand All @@ -941,7 +936,7 @@ func MilestoneList(project string, opt *gitlab.ListMilestonesOptions) ([]*gitlab
milestones = append(milestones, m...)

// if we've seen all the pages, then we can break here
if opt.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}

Expand All @@ -964,7 +959,6 @@ func MilestoneList(project string, opt *gitlab.ListMilestonesOptions) ([]*gitlab
IncludeParentMilestones: &includeParents,
}

gopt.ListOptions.Page = 1
for {
groupMilestones, resp, err := lab.GroupMilestones.ListGroupMilestones(p.Namespace.ID, gopt)
if err != nil {
Expand All @@ -987,7 +981,7 @@ func MilestoneList(project string, opt *gitlab.ListMilestonesOptions) ([]*gitlab
}

// if we've seen all the pages, then we can break here
if gopt.Page >= resp.TotalPages {
if resp.CurrentPage >= resp.TotalPages {
break
}

Expand Down

0 comments on commit de59b59

Please sign in to comment.