aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/gitlab/iterator/iterator.go
blob: ee2090b001aecef177e82da609d6db9e004c4e11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package iterator

import (
	"context"
	"time"

	"github.com/xanzy/go-gitlab"
)

type Iterator struct {
	// shared context
	ctx context.Context

	// to pass to sub-iterators
	conf config

	// sticky error
	err error

	// issues iterator
	issue *issueIterator

	// notes iterator
	note *noteIterator

	// labelEvent iterator
	labelEvent *labelEventIterator
}

type config struct {
	// gitlab api v4 client
	gc *gitlab.Client

	timeout time.Duration

	// if since is given the iterator will query only the issues
	// updated after this date
	since time.Time

	// project id
	project string

	// number of issues and notes to query at once
	capacity int
}

// NewIterator create a new iterator
func NewIterator(ctx context.Context, client *gitlab.Client, capacity int, projectID string, since time.Time) *Iterator {
	return &Iterator{
		ctx: ctx,
		conf: config{
			gc:       client,
			timeout:  60 * time.Second,
			since:    since,
			project:  projectID,
			capacity: capacity,
		},
		issue:      newIssueIterator(),
		note:       newNoteIterator(),
		labelEvent: newLabelEventIterator(),
	}
}

// Error return last encountered error
func (i *Iterator) Error() error {
	return i.err
}

func (i *Iterator) NextIssue() bool {
	if i.err != nil {
		return false
	}

	if i.ctx.Err() != nil {
		return false
	}

	more, err := i.issue.Next(i.ctx, i.conf)
	if err != nil {
		i.err = err
		return false
	}

	// Also reset the other sub iterators as they would
	// no longer be valid
	i.note.Reset(i.issue.Value().IID)
	i.labelEvent.Reset(i.issue.Value().IID)

	return more
}

func (i *Iterator) IssueValue() *gitlab.Issue {
	return i.issue.Value()
}

func (i *Iterator) NextNote() bool {
	if i.err != nil {
		return false
	}

	if i.ctx.Err() != nil {
		return false
	}

	more, err := i.note.Next(i.ctx, i.conf)
	if err != nil {
		i.err = err
		return false
	}

	return more
}

func (i *Iterator) NoteValue() *gitlab.Note {
	return i.note.Value()
}

func (i *Iterator) NextLabelEvent() bool {
	if i.err != nil {
		return false
	}

	if i.ctx.Err() != nil {
		return false
	}

	more, err := i.labelEvent.Next(i.ctx, i.conf)
	if err != nil {
		i.err = err
		return false
	}

	return more
}

func (i *Iterator) LabelEventValue() *gitlab.LabelEvent {
	return i.labelEvent.Value()
}