1package iterator
  2
  3import (
  4	"context"
  5	"time"
  6
  7	"code.gitea.io/sdk/gitea"
  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	// comments iterator
 24	comment *commentIterator
 25
 26	// labels iterator
 27	label *labelIterator
 28}
 29
 30type config struct {
 31	// gitea api v1 client
 32	gc *gitea.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	// name of the repository owner on Gitea
 41	owner string
 42
 43	// name of the Gitea repository
 44	project string
 45
 46	// number of issues and notes to query at once
 47	capacity int
 48}
 49
 50// NewIterator create a new iterator
 51func NewIterator(ctx context.Context, client *gitea.Client, capacity int, owner, project string, timeout time.Duration, since time.Time) *Iterator {
 52	return &Iterator{
 53		ctx: ctx,
 54		conf: config{
 55			gc:       client,
 56			timeout:  timeout,
 57			since:    since,
 58			owner:    owner,
 59			project:  project,
 60			capacity: capacity,
 61		},
 62		issue:   newIssueIterator(),
 63		comment: newCommentIterator(),
 64		label:   newLabelIterator(),
 65	}
 66}
 67
 68// Error return last encountered error
 69func (i *Iterator) Error() error {
 70	return i.err
 71}
 72
 73func (i *Iterator) NextIssue() bool {
 74	if i.err != nil {
 75		return false
 76	}
 77
 78	if i.ctx.Err() != nil {
 79		return false
 80	}
 81
 82	more, err := i.issue.Next(i.ctx, i.conf)
 83	if err != nil {
 84		i.err = err
 85		return false
 86	}
 87
 88	// Also reset the other sub iterators as they would
 89	// no longer be valid
 90	i.comment.Reset(i.issue.Value().Index)
 91	i.label.Reset(i.issue.Value().Index)
 92
 93	return more
 94}
 95
 96func (i *Iterator) IssueValue() *gitea.Issue {
 97	return i.issue.Value()
 98}
 99
100func (i *Iterator) NextComment() bool {
101	if i.err != nil {
102		return false
103	}
104
105	if i.ctx.Err() != nil {
106		return false
107	}
108
109	more, err := i.comment.Next(i.ctx, i.conf)
110	if err != nil {
111		i.err = err
112		return false
113	}
114
115	return more
116}
117
118func (i *Iterator) CommentValue() *gitea.Comment {
119	return i.comment.Value()
120}
121
122func (i *Iterator) NextLabel() bool {
123	if i.err != nil {
124		return false
125	}
126
127	if i.ctx.Err() != nil {
128		return false
129	}
130
131	more, err := i.label.Next(i.ctx, i.conf)
132	if err != nil {
133		i.err = err
134		return false
135	}
136
137	return more
138}
139
140func (i *Iterator) LabelValue() *gitea.Label {
141	return i.label.Value()
142}