2019-07-12 18:30:14 +03:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
type issueIterator struct {
|
|
|
|
page int
|
|
|
|
index int
|
|
|
|
cache []*gitlab.Issue
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
type noteIterator struct {
|
2019-07-12 18:30:14 +03:00
|
|
|
page int
|
|
|
|
index int
|
|
|
|
cache []*gitlab.Note
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
type labelEventIterator struct {
|
|
|
|
page int
|
|
|
|
index int
|
|
|
|
cache []*gitlab.LabelEvent
|
|
|
|
}
|
|
|
|
|
2019-07-12 18:30:14 +03:00
|
|
|
type iterator struct {
|
|
|
|
// gitlab api v4 client
|
|
|
|
gc *gitlab.Client
|
|
|
|
|
2019-07-23 19:40:10 +03:00
|
|
|
// if since is given the iterator will query only the issues
|
|
|
|
// updated after this date
|
2019-07-12 18:30:14 +03:00
|
|
|
since time.Time
|
|
|
|
|
|
|
|
// project id
|
|
|
|
project string
|
|
|
|
|
|
|
|
// number of issues and notes to query at once
|
|
|
|
capacity int
|
|
|
|
|
|
|
|
// sticky error
|
|
|
|
err error
|
|
|
|
|
|
|
|
// issues iterator
|
|
|
|
issue *issueIterator
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
// notes iterator
|
|
|
|
note *noteIterator
|
|
|
|
|
2019-07-22 01:13:47 +03:00
|
|
|
// labelEvent iterator
|
2019-07-14 16:54:09 +03:00
|
|
|
labelEvent *labelEventIterator
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIterator create a new iterator
|
2019-07-14 20:09:05 +03:00
|
|
|
func NewIterator(projectID, token string, since time.Time) *iterator {
|
2019-07-12 18:30:14 +03:00
|
|
|
return &iterator{
|
|
|
|
gc: buildClient(token),
|
|
|
|
project: projectID,
|
|
|
|
since: since,
|
2019-07-14 20:09:05 +03:00
|
|
|
capacity: 20,
|
2019-07-12 18:30:14 +03:00
|
|
|
issue: &issueIterator{
|
|
|
|
index: -1,
|
|
|
|
page: 1,
|
|
|
|
},
|
2019-07-14 16:54:09 +03:00
|
|
|
note: ¬eIterator{
|
|
|
|
index: -1,
|
|
|
|
page: 1,
|
|
|
|
},
|
|
|
|
labelEvent: &labelEventIterator{
|
2019-07-12 18:30:14 +03:00
|
|
|
index: -1,
|
|
|
|
page: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error return last encountered error
|
|
|
|
func (i *iterator) Error() error {
|
|
|
|
return i.err
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
func (i *iterator) getNextIssues() bool {
|
2019-07-12 18:30:14 +03:00
|
|
|
issues, _, err := i.gc.Issues.ListProjectIssues(
|
|
|
|
i.project,
|
|
|
|
&gitlab.ListProjectIssuesOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: i.issue.page,
|
|
|
|
PerPage: i.capacity,
|
|
|
|
},
|
2019-07-23 19:40:10 +03:00
|
|
|
Scope: gitlab.String("all"),
|
2019-07-12 18:30:14 +03:00
|
|
|
UpdatedAfter: &i.since,
|
2019-07-23 19:40:10 +03:00
|
|
|
Sort: gitlab.String("asc"),
|
2019-07-12 18:30:14 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
if err != nil {
|
|
|
|
i.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if repository doesn't have any issues
|
|
|
|
if len(issues) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
i.issue.cache = issues
|
|
|
|
i.issue.index = 0
|
|
|
|
i.issue.page++
|
|
|
|
i.note.index = -1
|
|
|
|
i.note.cache = nil
|
|
|
|
|
|
|
|
return true
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *iterator) NextIssue() bool {
|
2019-07-23 19:40:10 +03:00
|
|
|
if i.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-12 18:30:14 +03:00
|
|
|
// first query
|
|
|
|
if i.issue.cache == nil {
|
2019-07-14 16:54:09 +03:00
|
|
|
return i.getNextIssues()
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// move cursor index
|
2019-07-23 18:10:07 +03:00
|
|
|
if i.issue.index < len(i.issue.cache)-1 {
|
2019-07-12 18:30:14 +03:00
|
|
|
i.issue.index++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
return i.getNextIssues()
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *iterator) IssueValue() *gitlab.Issue {
|
|
|
|
return i.issue.cache[i.issue.index]
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
func (i *iterator) getNextNotes() bool {
|
2019-07-12 18:30:14 +03:00
|
|
|
notes, _, err := i.gc.Notes.ListIssueNotes(
|
|
|
|
i.project,
|
|
|
|
i.IssueValue().IID,
|
|
|
|
&gitlab.ListIssueNotesOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
2019-07-14 16:54:09 +03:00
|
|
|
Page: i.note.page,
|
2019-07-12 18:30:14 +03:00
|
|
|
PerPage: i.capacity,
|
|
|
|
},
|
2019-07-23 19:40:10 +03:00
|
|
|
Sort: gitlab.String("asc"),
|
|
|
|
OrderBy: gitlab.String("created_at"),
|
2019-07-12 18:30:14 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
if err != nil {
|
|
|
|
i.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(notes) == 0 {
|
|
|
|
i.note.index = -1
|
|
|
|
i.note.page = 1
|
|
|
|
i.note.cache = nil
|
|
|
|
return false
|
|
|
|
}
|
2019-07-12 18:30:14 +03:00
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
i.note.cache = notes
|
|
|
|
i.note.page++
|
|
|
|
i.note.index = 0
|
|
|
|
return true
|
2019-07-12 18:48:57 +03:00
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
func (i *iterator) NextNote() bool {
|
2019-07-12 18:30:14 +03:00
|
|
|
if i.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
if len(i.note.cache) == 0 {
|
|
|
|
return i.getNextNotes()
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// move cursor index
|
2019-07-23 18:10:07 +03:00
|
|
|
if i.note.index < len(i.note.cache)-1 {
|
2019-07-14 16:54:09 +03:00
|
|
|
i.note.index++
|
2019-07-12 18:30:14 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
return i.getNextNotes()
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:09 +03:00
|
|
|
func (i *iterator) NoteValue() *gitlab.Note {
|
|
|
|
return i.note.cache[i.note.index]
|
2019-07-12 18:30:14 +03:00
|
|
|
}
|
|
|
|
|
2019-07-14 16:54:48 +03:00
|
|
|
func (i *iterator) getNextLabelEvents() bool {
|
|
|
|
labelEvents, _, err := i.gc.ResourceLabelEvents.ListIssueLabelEvents(
|
|
|
|
i.project,
|
|
|
|
i.IssueValue().IID,
|
|
|
|
&gitlab.ListLabelEventsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: i.labelEvent.page,
|
|
|
|
PerPage: i.capacity,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
i.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(labelEvents) == 0 {
|
|
|
|
i.labelEvent.page = 1
|
|
|
|
i.labelEvent.index = -1
|
|
|
|
i.labelEvent.cache = nil
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
i.labelEvent.cache = labelEvents
|
|
|
|
i.labelEvent.page++
|
|
|
|
i.labelEvent.index = 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-23 20:16:52 +03:00
|
|
|
// because Gitlab
|
2019-07-14 16:54:48 +03:00
|
|
|
func (i *iterator) NextLabelEvent() bool {
|
|
|
|
if i.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(i.labelEvent.cache) == 0 {
|
|
|
|
return i.getNextLabelEvents()
|
|
|
|
}
|
|
|
|
|
|
|
|
// move cursor index
|
2019-07-23 18:10:07 +03:00
|
|
|
if i.labelEvent.index < len(i.labelEvent.cache)-1 {
|
2019-07-14 16:54:48 +03:00
|
|
|
i.labelEvent.index++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.getNextLabelEvents()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *iterator) LabelEventValue() *gitlab.LabelEvent {
|
|
|
|
return i.labelEvent.cache[i.labelEvent.index]
|
|
|
|
}
|