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

View File

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

View File

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