-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator.go
83 lines (71 loc) · 1.59 KB
/
iterator.go
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
package client
import (
"context"
"fmt"
"github.com/andygrunwald/go-jira"
)
func NewIssueIterator(ctx context.Context, adp AdaptorInterface, opts IssueIteratorOptions) (IssueIteratorInterface, error) {
if opts.Query == "" {
return nil, fmt.Errorf("must pass non-nil query")
}
it := &IssueIterator{
adaptor: adp,
query: opts.Query,
expand: opts.Expand,
pageSize: opts.PageSize,
}
return it, it.more(ctx)
}
type IssueIterator struct {
adaptor AdaptorInterface
query string
expand string
issues []jira.Issue
pageSize int
startAt int
endAt int
totalResults int
index int
}
type IssueIteratorOptions struct {
Query string
Expand string
PageSize int
}
func (it *IssueIterator) initialize() error {
return nil
}
func (it *IssueIterator) more(ctx context.Context) error {
issues, response, err := it.adaptor.SearchIssuesWithContext(
ctx,
it.query,
&jira.SearchOptions{
StartAt: it.endAt,
MaxResults: it.pageSize,
Expand: it.expand,
},
)
if err != nil {
return err
}
it.issues = issues
it.startAt = response.StartAt
it.totalResults = response.Total
it.endAt = it.endAt + response.MaxResults
if it.endAt > response.Total {
it.endAt = response.Total
}
return nil
}
func (it *IssueIterator) Next(ctx context.Context) (*jira.Issue, error) {
it.index = it.index + 1
if it.index > it.totalResults {
return nil, nil
}
if it.index > it.endAt {
if err := it.more(ctx); err != nil {
return nil, fmt.Errorf("error getting issues: %v", err)
}
}
return &it.issues[it.index-it.startAt-1], nil
}