iterator.go

  1package iterator
  2
  3import (
  4	"context"
  5	"time"
  6
  7	"github.com/xanzy/go-gitlab"
  8)
  9
 10type Iterator struct {
 11	// shared context
 12	ctx context.Context
 13
 14	// to pass to sub-iterators
 15	conf config
 16
 17	// sticky error
 18	err error
 19
 20	// issues iterator
 21	issue *issueIterator
 22
 23	// notes iterator
 24	note *noteIterator
 25
 26	// labelEvent iterator
 27	labelEvent *labelEventIterator
 28}
 29
 30type config struct {
 31	// gitlab api v4 client
 32	gc *gitlab.Client
 33
 34	timeout time.Duration
 35
 36	// if since is given the iterator will query only the issues
 37	// updated after this date
 38	since time.Time
 39
 40	// project id
 41	project string
 42
 43	// number of issues and notes to query at once
 44	capacity int
 45}
 46
 47// NewIterator create a new iterator
 48func NewIterator(ctx context.Context, client *gitlab.Client, capacity int, projectID string, since time.Time) *Iterator {
 49	return &Iterator{
 50		ctx: ctx,
 51		conf: config{
 52			gc:       client,
 53			timeout:  60 * time.Second,
 54			since:    since,
 55			project:  projectID,
 56			capacity: capacity,
 57		},
 58		issue:      newIssueIterator(),
 59		note:       newNoteIterator(),
 60		labelEvent: newLabelEventIterator(),
 61	}
 62}
 63
 64// Error return last encountered error
 65func (i *Iterator) Error() error {
 66	return i.err
 67}
 68
 69func (i *Iterator) NextIssue() bool {
 70	if i.err != nil {
 71		return false
 72	}
 73
 74	if i.ctx.Err() != nil {
 75		return false
 76	}
 77
 78	more, err := i.issue.Next(i.ctx, i.conf)
 79	if err != nil {
 80		i.err = err
 81		return false
 82	}
 83
 84	// Also reset the other sub iterators as they would
 85	// no longer be valid
 86	i.note.Reset(i.issue.Value().IID)
 87	i.labelEvent.Reset(i.issue.Value().IID)
 88
 89	return more
 90}
 91
 92func (i *Iterator) IssueValue() *gitlab.Issue {
 93	return i.issue.Value()
 94}
 95
 96func (i *Iterator) NextNote() bool {
 97	if i.err != nil {
 98		return false
 99	}
100
101	if i.ctx.Err() != nil {
102		return false
103	}
104
105	more, err := i.note.Next(i.ctx, i.conf)
106	if err != nil {
107		i.err = err
108		return false
109	}
110
111	return more
112}
113
114func (i *Iterator) NoteValue() *gitlab.Note {
115	return i.note.Value()
116}
117
118func (i *Iterator) NextLabelEvent() bool {
119	if i.err != nil {
120		return false
121	}
122
123	if i.ctx.Err() != nil {
124		return false
125	}
126
127	more, err := i.labelEvent.Next(i.ctx, i.conf)
128	if err != nil {
129		i.err = err
130		return false
131	}
132
133	return more
134}
135
136func (i *Iterator) LabelEventValue() *gitlab.LabelEvent {
137	return i.labelEvent.Value()
138}