diff options
author | Michael Muré <batolettre@gmail.com> | 2020-03-28 21:09:36 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-03-28 21:23:18 +0100 |
commit | f4ca533fe10f7fa893e1953f8c8d9ed3e783486c (patch) | |
tree | f9fdaff8aa02f723acc3a4492d46655d63cbff5a /bridge/gitlab/iterator/issue.go | |
parent | 38b42bc867f8f352908ba81334bec86b001e8fac (diff) | |
download | git-bug-f4ca533fe10f7fa893e1953f8c8d9ed3e783486c.tar.gz |
gitlab: refactor the iterator, fix bugs
Notably, properly reset sub iterators when changing to the next issue
Diffstat (limited to 'bridge/gitlab/iterator/issue.go')
-rw-r--r-- | bridge/gitlab/iterator/issue.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/bridge/gitlab/iterator/issue.go b/bridge/gitlab/iterator/issue.go new file mode 100644 index 00000000..a6bcebf1 --- /dev/null +++ b/bridge/gitlab/iterator/issue.go @@ -0,0 +1,79 @@ +package iterator + +import ( + "context" + + "github.com/xanzy/go-gitlab" +) + +type issueIterator struct { + page int + index int + cache []*gitlab.Issue +} + +func newIssueIterator() *issueIterator { + ii := &issueIterator{} + ii.Reset() + return ii +} + +func (ii *issueIterator) Next(ctx context.Context, conf config) (bool, error) { + // first query + if ii.cache == nil { + return ii.getNext(ctx, conf) + } + + // move cursor index + if ii.index < len(ii.cache)-1 { + ii.index++ + return true, nil + } + + return ii.getNext(ctx, conf) +} + +func (ii *issueIterator) Value() *gitlab.Issue { + return ii.cache[ii.index] +} + +func (ii *issueIterator) getNext(ctx context.Context, conf config) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, conf.timeout) + defer cancel() + + issues, _, err := conf.gc.Issues.ListProjectIssues( + conf.project, + &gitlab.ListProjectIssuesOptions{ + ListOptions: gitlab.ListOptions{ + Page: ii.page, + PerPage: conf.capacity, + }, + Scope: gitlab.String("all"), + UpdatedAfter: &conf.since, + Sort: gitlab.String("asc"), + }, + gitlab.WithContext(ctx), + ) + + if err != nil { + ii.Reset() + return false, err + } + + // if repository doesn't have any issues + if len(issues) == 0 { + return false, nil + } + + ii.cache = issues + ii.index = 0 + ii.page++ + + return true, nil +} + +func (ii *issueIterator) Reset() { + ii.index = -1 + ii.page = -1 + ii.cache = nil +} |