-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
194 lines (164 loc) · 4.28 KB
/
query.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package util
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v45/github"
"github.com/pkg/errors"
)
// PullRequestQuery holds the parameters for iterating over pull requests
type PullRequestQuery struct {
Org string
Repo string
DevMode bool
Client *github.Client
}
const pageSize int = 50
// PRCallback is a type for callbacks for processing pull requests
type PRCallback func(context.Context, *github.PullRequest) error
// IteratePullRequests queries for all pull requests and invokes the
// callback with each PR individually
func (q *PullRequestQuery) IteratePullRequests(ctx context.Context, callback PRCallback) error {
opts := &github.PullRequestListOptions{
State: "all",
ListOptions: github.ListOptions{
PerPage: pageSize,
},
}
// Fetch the details of the pull requests in batches. We do not
// invoke the callback as a goroutine because it is likely to make
// other API calls and we want to limit the number of simultaneous
// requests we make to the API to avoid rate limiting.
for {
prs, response, err := q.Client.PullRequests.List(ctx, q.Org, q.Repo, opts)
if err != nil {
return errors.Wrap(err,
fmt.Sprintf(
"could not get pull requests for %s/%s", q.Org, q.Repo))
}
for _, pr := range prs {
err := callback(ctx, pr)
if err != nil {
fmt.Fprintf(os.Stderr, "\ncould not process pull request %s: %s\n",
*pr.HTMLURL, err)
continue
}
select {
case <-ctx.Done():
fmt.Fprintf(os.Stderr, "stopping\n")
return nil
default:
}
fmt.Fprintf(os.Stderr, ".")
}
if q.DevMode {
fmt.Fprintf(os.Stderr, "shortcutting for dev mode\n")
break
}
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
}
fmt.Fprintf(os.Stderr, "\n")
return nil
}
func (q *PullRequestQuery) GetIssueComments(ctx context.Context, pr *github.PullRequest) ([]*github.IssueComment, error) {
opts := &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: pageSize,
},
}
results := []*github.IssueComment{}
for {
comments, response, err := q.Client.Issues.ListComments(
ctx, q.Org, q.Repo, *pr.Number, opts)
if err != nil {
return nil, err
}
results = append(results, comments...)
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
}
return results, nil
}
func (q *PullRequestQuery) GetPRComments(ctx context.Context, pr *github.PullRequest) ([]*github.PullRequestComment, error) {
opts := &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: pageSize,
},
}
results := []*github.PullRequestComment{}
for {
comments, response, err := q.Client.PullRequests.ListComments(
ctx, q.Org, q.Repo, *pr.Number, opts)
if err != nil {
return nil, err
}
results = append(results, comments...)
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
select {
case <-ctx.Done():
return results, nil
default:
}
}
return results, nil
}
func (q *PullRequestQuery) GetReviews(ctx context.Context, pr *github.PullRequest) ([]*github.PullRequestReview, error) {
opts := &github.ListOptions{
PerPage: pageSize,
}
results := []*github.PullRequestReview{}
for {
comments, response, err := q.Client.PullRequests.ListReviews(
ctx, q.Org, q.Repo, *pr.Number, opts)
if err != nil {
return nil, err
}
results = append(results, comments...)
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
select {
case <-ctx.Done():
return results, nil
default:
}
}
return results, nil
}
func (q *PullRequestQuery) GetCommits(ctx context.Context, pr *github.PullRequest) ([]*github.RepositoryCommit, error) {
opts := &github.ListOptions{
PerPage: pageSize,
}
results := []*github.RepositoryCommit{}
for {
commits, response, err := q.Client.PullRequests.ListCommits(
ctx, q.Org, q.Repo, *pr.Number, opts)
if err != nil {
return nil, err
}
results = append(results, commits...)
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
select {
case <-ctx.Done():
return results, nil
default:
}
}
return results, nil
}
func (q *PullRequestQuery) IsMerged(ctx context.Context, pr *github.PullRequest) (bool, error) {
isMerged, _, err := q.Client.PullRequests.IsMerged(ctx, q.Org, q.Repo, *pr.Number)
return isMerged, err
}