gitlab: fix iterator (paginate with first index 1) and avoid the trailing API call

This commit is contained in:
Michael Muré 2020-04-04 11:59:53 +02:00
parent f4ca533fe1
commit 903549cadf
No known key found for this signature in database
GPG Key ID: A4457C029293126F
3 changed files with 38 additions and 12 deletions

View File

@ -7,9 +7,10 @@ import (
)
type issueIterator struct {
page int
index int
cache []*gitlab.Issue
page int
lastPage bool
index int
cache []*gitlab.Issue
}
func newIssueIterator() *issueIterator {
@ -38,10 +39,14 @@ func (ii *issueIterator) Value() *gitlab.Issue {
}
func (ii *issueIterator) getNext(ctx context.Context, conf config) (bool, error) {
if ii.lastPage {
return false, nil
}
ctx, cancel := context.WithTimeout(ctx, conf.timeout)
defer cancel()
issues, _, err := conf.gc.Issues.ListProjectIssues(
issues, resp, err := conf.gc.Issues.ListProjectIssues(
conf.project,
&gitlab.ListProjectIssuesOptions{
ListOptions: gitlab.ListOptions{
@ -60,6 +65,10 @@ func (ii *issueIterator) getNext(ctx context.Context, conf config) (bool, error)
return false, err
}
if resp.TotalPages == ii.page {
ii.lastPage = true
}
// if repository doesn't have any issues
if len(issues) == 0 {
return false, nil
@ -74,6 +83,7 @@ func (ii *issueIterator) getNext(ctx context.Context, conf config) (bool, error)
func (ii *issueIterator) Reset() {
ii.index = -1
ii.page = -1
ii.page = 1
ii.lastPage = false
ii.cache = nil
}

View File

@ -49,7 +49,7 @@ func (lei *labelEventIterator) getNext(ctx context.Context, conf config) (bool,
// and sort them by ID
page := 1
for {
labelEvents, _, err := conf.gc.ResourceLabelEvents.ListIssueLabelEvents(
labelEvents, resp, err := conf.gc.ResourceLabelEvents.ListIssueLabelEvents(
conf.project,
lei.issue,
&gitlab.ListLabelEventsOptions{
@ -68,7 +68,13 @@ func (lei *labelEventIterator) getNext(ctx context.Context, conf config) (bool,
if len(labelEvents) == 0 {
break
}
lei.cache = append(lei.cache, labelEvents...)
if resp.TotalPages == page {
break
}
page++
}

View File

@ -7,10 +7,11 @@ import (
)
type noteIterator struct {
issue int
page int
index int
cache []*gitlab.Note
issue int
page int
lastPage bool
index int
cache []*gitlab.Note
}
func newNoteIterator() *noteIterator {
@ -39,10 +40,14 @@ func (in *noteIterator) Value() *gitlab.Note {
}
func (in *noteIterator) getNext(ctx context.Context, conf config) (bool, error) {
if in.lastPage {
return false, nil
}
ctx, cancel := context.WithTimeout(ctx, conf.timeout)
defer cancel()
notes, _, err := conf.gc.Notes.ListIssueNotes(
notes, resp, err := conf.gc.Notes.ListIssueNotes(
conf.project,
in.issue,
&gitlab.ListIssueNotesOptions{
@ -61,6 +66,10 @@ func (in *noteIterator) getNext(ctx context.Context, conf config) (bool, error)
return false, err
}
if resp.TotalPages == in.page {
in.lastPage = true
}
if len(notes) == 0 {
return false, nil
}
@ -75,6 +84,7 @@ func (in *noteIterator) getNext(ctx context.Context, conf config) (bool, error)
func (in *noteIterator) Reset(issue int) {
in.issue = issue
in.index = -1
in.page = -1
in.page = 1
in.lastPage = false
in.cache = nil
}