aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/gitlab/iterator/labelEvent.go
diff options
context:
space:
mode:
authorMatthias Simon <matthias.simon@nokia.com>2021-02-14 20:35:03 +0100
committerMatthias Simon <matthias.simon@nokia.com>2021-04-23 09:02:48 +0200
commitaa4e225a80b37ce26f5f8c69041ee735f512b113 (patch)
treef34b7ff03f4cec4be4ed6eb3458d515452a92fe6 /bridge/gitlab/iterator/labelEvent.go
parenta8f3b55986982db5f7c3918acaba2c214c919d11 (diff)
downloadgit-bug-aa4e225a80b37ce26f5f8c69041ee735f512b113.tar.gz
gitlab: Add new iterator with state change events
Retrieving events is spread across various various Gitlab APIs. This makes importing and sorting Gitlab events by time quite complicated. This commit replaces the old iterators with a goroutine/channel-based iterator, which merges the individual Gitlab API streams into a single (sorted) event stream.
Diffstat (limited to 'bridge/gitlab/iterator/labelEvent.go')
-rw-r--r--bridge/gitlab/iterator/labelEvent.go105
1 files changed, 0 insertions, 105 deletions
diff --git a/bridge/gitlab/iterator/labelEvent.go b/bridge/gitlab/iterator/labelEvent.go
deleted file mode 100644
index 812e6646..00000000
--- a/bridge/gitlab/iterator/labelEvent.go
+++ /dev/null
@@ -1,105 +0,0 @@
-package iterator
-
-import (
- "context"
- "sort"
-
- "github.com/xanzy/go-gitlab"
-)
-
-// Since Gitlab does not return the label events items in the correct order
-// we need to sort the list ourselves and stop relying on the pagination model
-// #BecauseGitlab
-type labelEventIterator struct {
- issue int
- index int
- cache []*gitlab.LabelEvent
-}
-
-func newLabelEventIterator() *labelEventIterator {
- lei := &labelEventIterator{}
- lei.Reset(-1)
- return lei
-}
-
-func (lei *labelEventIterator) Next(ctx context.Context, conf config) (bool, error) {
- // first query
- if lei.cache == nil {
- return lei.getNext(ctx, conf)
- }
-
- // move cursor index
- if lei.index < len(lei.cache)-1 {
- lei.index++
- return true, nil
- }
-
- return false, nil
-}
-
-func (lei *labelEventIterator) Value() *gitlab.LabelEvent {
- return lei.cache[lei.index]
-}
-
-func (lei *labelEventIterator) getNext(ctx context.Context, conf config) (bool, error) {
- ctx, cancel := context.WithTimeout(ctx, conf.timeout)
- defer cancel()
-
- // since order is not guaranteed we should query all label events
- // and sort them by ID
- page := 1
- for {
- labelEvents, resp, err := conf.gc.ResourceLabelEvents.ListIssueLabelEvents(
- conf.project,
- lei.issue,
- &gitlab.ListLabelEventsOptions{
- ListOptions: gitlab.ListOptions{
- Page: page,
- PerPage: conf.capacity,
- },
- },
- gitlab.WithContext(ctx),
- )
- if err != nil {
- lei.Reset(-1)
- return false, err
- }
-
- if len(labelEvents) == 0 {
- break
- }
-
- lei.cache = append(lei.cache, labelEvents...)
-
- if resp.TotalPages == page {
- break
- }
-
- page++
- }
-
- sort.Sort(lei)
- lei.index = 0
-
- return len(lei.cache) > 0, nil
-}
-
-func (lei *labelEventIterator) Reset(issue int) {
- lei.issue = issue
- lei.index = -1
- lei.cache = nil
-}
-
-// ORDERING
-
-func (lei *labelEventIterator) Len() int {
- return len(lei.cache)
-}
-
-func (lei *labelEventIterator) Swap(i, j int) {
- lei.cache[i], lei.cache[j] = lei.cache[j], lei.cache[i]
-}
-
-func (lei *labelEventIterator) Less(i, j int) bool {
- return lei.cache[i].ID < lei.cache[j].ID
-}