diff options
author | Michael Muré <batolettre@gmail.com> | 2020-04-04 12:48:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-04 12:48:24 +0200 |
commit | 2d64b85db71a17ff3277bbbf7ac9d8e81f8e416c (patch) | |
tree | 58df795ebf4bc2f5517c2537135bef2bb2a77c10 /bridge/gitlab/iterator/note.go | |
parent | 38b42bc867f8f352908ba81334bec86b001e8fac (diff) | |
parent | 903549cadf40ede3771053781eb6e9fd31aaa64e (diff) | |
download | git-bug-2d64b85db71a17ff3277bbbf7ac9d8e81f8e416c.tar.gz |
Merge pull request #363 from MichaelMure/gitlab-iterator0.7.1
gitlab: refactor the iterator, fix bug
Diffstat (limited to 'bridge/gitlab/iterator/note.go')
-rw-r--r-- | bridge/gitlab/iterator/note.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/bridge/gitlab/iterator/note.go b/bridge/gitlab/iterator/note.go new file mode 100644 index 00000000..a1e0544c --- /dev/null +++ b/bridge/gitlab/iterator/note.go @@ -0,0 +1,90 @@ +package iterator + +import ( + "context" + + "github.com/xanzy/go-gitlab" +) + +type noteIterator struct { + issue int + page int + lastPage bool + index int + cache []*gitlab.Note +} + +func newNoteIterator() *noteIterator { + in := ¬eIterator{} + in.Reset(-1) + return in +} + +func (in *noteIterator) Next(ctx context.Context, conf config) (bool, error) { + // first query + if in.cache == nil { + return in.getNext(ctx, conf) + } + + // move cursor index + if in.index < len(in.cache)-1 { + in.index++ + return true, nil + } + + return in.getNext(ctx, conf) +} + +func (in *noteIterator) Value() *gitlab.Note { + return in.cache[in.index] +} + +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, resp, err := conf.gc.Notes.ListIssueNotes( + conf.project, + in.issue, + &gitlab.ListIssueNotesOptions{ + ListOptions: gitlab.ListOptions{ + Page: in.page, + PerPage: conf.capacity, + }, + Sort: gitlab.String("asc"), + OrderBy: gitlab.String("created_at"), + }, + gitlab.WithContext(ctx), + ) + + if err != nil { + in.Reset(-1) + return false, err + } + + if resp.TotalPages == in.page { + in.lastPage = true + } + + if len(notes) == 0 { + return false, nil + } + + in.cache = notes + in.index = 0 + in.page++ + + return true, nil +} + +func (in *noteIterator) Reset(issue int) { + in.issue = issue + in.index = -1 + in.page = 1 + in.lastPage = false + in.cache = nil +} |