-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetcher.go
329 lines (286 loc) · 7.65 KB
/
fetcher.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"context"
"log"
"net/http"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
)
type Fetcher interface {
Auth(context.Context)
OpenedReleasePRNumber(context.Context) (int, bool, error)
MergedPullRequests(context.Context) (PullRequests, error)
OpenedPullRequests(context.Context, string) (PullRequests, error)
GetComparison() Comparison
CreatePullRequest(context.Context, string, string, []string) (PullRequest, error)
UpdatePullRequest(context.Context, string, string, int, []string) (PullRequest, error)
}
type fetcher struct {
Repo Repo
Token string
client *github.Client
comparison *Comparison
}
func (f *fetcher) Auth(ctx context.Context) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: f.Token},
)
tc := oauth2.NewClient(ctx, ts)
f.client = github.NewClient(tc)
}
func (f *fetcher) OpenedReleasePRNumber(ctx context.Context) (int, bool, error) {
opts := &github.PullRequestListOptions{
State: "open",
Head: f.Repo.JoinedHead(),
Base: f.Repo.Base,
}
prs, _, err := f.client.PullRequests.List(ctx, f.Repo.Owner, f.Repo.Repo, opts)
if err != nil {
return 0, false, errors.Wrap(err, "fail fetch pull requests")
}
if len(prs) == 0 {
return 0, false, nil
}
return prs[0].GetNumber(), true, nil
}
func (f *fetcher) headBranch(ctx context.Context) (Branch, error) {
b, _, err := f.client.Repositories.GetBranch(ctx, f.Repo.Owner, f.Repo.Repo, f.Repo.Head)
if err != nil {
return Branch{}, errors.Wrap(err, "fail fetching head branch information")
}
branch := ToBranch(b)
return branch, nil
}
func (f *fetcher) baseBranch(ctx context.Context) (Branch, error) {
b, _, err := f.client.Repositories.GetBranch(ctx, f.Repo.Owner, f.Repo.Repo, f.Repo.Base)
if err != nil {
return Branch{}, errors.Wrap(err, "fail fetching information of base branch")
}
branch := ToBranch(b)
return branch, nil
}
func (f *fetcher) compare(ctx context.Context) (Comparison, error) {
head, err := f.headBranch(ctx)
if err != nil {
return Comparison{}, err
}
base, err := f.baseBranch(ctx)
if err != nil {
return Comparison{}, err
}
comparison, _, err := f.client.Repositories.CompareCommits(
ctx, f.Repo.Owner, f.Repo.Repo, base.Commit, head.Commit,
)
if err != nil {
return Comparison{}, errors.Wrap(err, "fail fetching comapre commits")
}
var com Comparison
if len(comparison.Files) > 0 {
files := make([]ComparisonFile, 0, len(comparison.Files))
for _, f := range comparison.Files {
cf := ComparisonFile{
Name: f.GetFilename(),
SHA: f.GetSHA(),
Deletions: f.GetDeletions(),
Additions: f.GetAdditions(),
Patch: f.GetPatch(),
}
files = append(files, cf)
}
com.Files = files
}
if len(comparison.Commits) > 0 {
commits := make([]ComparisonCommit, 0, len(comparison.Commits))
for _, c := range comparison.Commits {
var parentHashes []string
if len(c.Parents) > 0 {
parentHashes = make([]string, 0, len(c.Parents))
for _, parent := range c.Parents {
parentHashes = append(parentHashes, parent.GetSHA())
}
}
cc := ComparisonCommit{
Parents: parentHashes,
Message: c.Commit.GetMessage(),
}
commits = append(commits, cc)
}
com.Commits = commits
}
return com, nil
}
func (f *fetcher) fetchCompare(ctx context.Context) error {
if f.comparison != nil {
return nil
}
comparison, err := f.compare(ctx)
if err != nil {
return err
}
f.comparison = &comparison
return nil
}
func (f *fetcher) MergedPullRequests(ctx context.Context) (PullRequests, error) {
err := f.fetchCompare(ctx)
if err != nil {
return nil, err
}
commits := f.comparison.Commits
if commits == nil {
return nil, nil
}
prs := make([]*PullRequest, 0, len(commits)/2)
for _, commit := range commits {
if !commit.IsMerge() || commit.IsPullFromMaster() {
continue
}
pr, err := commit.PullRequest()
if err != nil {
log.Printf("[DEBUG] %s", err)
continue
}
prs = append(prs, &pr)
}
eg := errgroup.Group{}
for _, pr := range prs {
_pr := pr
eg.Go(func() error {
err := f.FetchPullRequest(ctx, _pr)
if err == nil {
return nil
}
cause := errors.Cause(err)
errResp, ok := cause.(*github.ErrorResponse)
if ok && errResp.Response.StatusCode == http.StatusNotFound {
return nil
}
return err
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
freezedPRs := make(PullRequests, 0, len(prs))
for _, pr := range prs {
if !pr.Fetched {
continue
}
if !f.Repo.ContainsBase(pr.Base) {
continue
}
freezedPRs = append(freezedPRs, *pr)
}
return freezedPRs, nil
}
func (f *fetcher) FetchPullRequest(ctx context.Context, pr *PullRequest) error {
prr, _, err := f.client.PullRequests.Get(ctx, f.Repo.Owner, f.Repo.Repo, pr.Number)
if err != nil {
return errors.Wrap(err, "fail fetching pull request")
}
pr.Head = prr.GetHead().GetRef()
pr.Base = prr.GetBase().GetRef()
pr.Title = prr.GetTitle()
pr.Author = prr.GetUser().GetLogin()
pr.Fetched = true
return nil
}
func (f *fetcher) OpenedPullRequests(ctx context.Context, label string) (PullRequests, error) {
opt := &github.IssueListByRepoOptions{
State: "open",
Labels: []string{label},
}
issues, _, err := f.client.Issues.ListByRepo(ctx, f.Repo.Owner, f.Repo.Repo, opt)
if err != nil {
return nil, errors.Wrap(err, "fail fetching issues on OpenedPullRequests")
}
prs := make([]*PullRequest, 0, len(issues))
for _, issue := range issues {
if !issue.IsPullRequest() {
continue
}
pr := &PullRequest{
Number: issue.GetNumber(),
Title: issue.GetTitle(),
}
prs = append(prs, pr)
}
eg := errgroup.Group{}
for _, pr := range prs {
_pr := pr
eg.Go(func() error {
return f.FetchPullRequest(ctx, _pr)
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
freezedPRs := make(PullRequests, 0, len(prs))
for _, pr := range prs {
if pr.Base != f.Repo.Head {
continue
}
freezedPRs = append(freezedPRs, *pr)
}
return freezedPRs, nil
}
func (f *fetcher) GetComparison() Comparison {
return *f.comparison
}
func (f *fetcher) CreatePullRequest(ctx context.Context, title, body string, labels []string) (PullRequest, error) {
pull := &github.NewPullRequest{
Title: &title,
Body: &body,
Head: &f.Repo.Head,
Base: &f.Repo.Base,
}
pr, _, err := f.client.PullRequests.Create(ctx, f.Repo.Owner, f.Repo.Repo, pull)
if err != nil {
return PullRequest{}, errors.Wrap(err, "fail create pull request")
}
ppr := PullRequest{
Number: pr.GetNumber(),
Title: pr.GetTitle(),
Head: pr.GetHead().GetRef(),
Base: pr.GetBase().GetRef(),
Author: pr.GetUser().GetLogin(),
}
err = f.updateLabel(ctx, ppr.Number, labels)
if err != nil {
return PullRequest{}, err
}
return ppr, nil
}
func (f *fetcher) updateLabel(ctx context.Context, number int, labels []string) error {
if len(labels) == 0 {
return nil
}
_, _, err := f.client.Issues.AddLabelsToIssue(ctx, f.Repo.Owner, f.Repo.Repo, number, labels)
if err != nil {
return errors.Wrap(err, "fail update label to issue")
}
return nil
}
func (f *fetcher) UpdatePullRequest(ctx context.Context, title, body string, number int, labels []string) (PullRequest, error) {
pull := &github.PullRequest{
Title: &title,
Body: &body,
}
pr, _, err := f.client.PullRequests.Edit(ctx, f.Repo.Owner, f.Repo.Repo, number, pull)
if err != nil {
return PullRequest{}, errors.Wrap(err, "fail update pull request")
}
ppr := PullRequest{
Number: pr.GetNumber(),
Title: pr.GetTitle(),
Head: pr.GetHead().GetRef(),
Base: pr.GetBase().GetRef(),
Author: pr.GetUser().GetLogin(),
}
err = f.updateLabel(ctx, ppr.Number, labels)
if err != nil {
return PullRequest{}, err
}
return ppr, nil
}