-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathautomerge.go
More file actions
392 lines (352 loc) · 10.9 KB
/
automerge.go
File metadata and controls
392 lines (352 loc) · 10.9 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2020-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package github
import (
"context"
"fmt"
"strings"
"time"
gh "github.com/google/go-github/v71/github"
)
type AutoMerge struct {
Label string `yaml:"label"`
MinimalApprovals int `yaml:"min-approvals"`
}
func (c *Client) AutoMerge(
cfg AutoMerge,
owner, repoName string,
base,
head *gh.PullRequestBranch,
prNumber int,
prLabels PRLabels,
review *gh.PullRequestReview,
) error {
ciChecks, err := c.getCIStatus(owner, repoName, base, head, prNumber)
if err != nil {
return err
}
if len(ciChecks) != 0 {
c.log.Info().Fields(map[string]interface{}{
"owner": owner,
"repo": repoName,
"pr-number": prNumber,
"ci-checks": ciChecks,
}).Msg("Not auto merging because ci failed")
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
commit, _, err := c.GHClient.Repositories.GetCommit(ctx, owner, repoName, head.GetSHA(), &gh.ListOptions{})
if err != nil {
return err
}
commitDate := commit.GetCommit().GetCommitter().GetDate()
if commitDate.IsZero() {
c.log.Info().Fields(map[string]interface{}{
"owner": owner,
"repo": repoName,
"pr-number": prNumber,
"sha": head.GetSHA(),
"commit": gh.Stringify(commit.GetCommit()),
"full-commit": gh.Stringify(commit),
}).Msg("Not auto merging because of empty-committer")
return nil
}
// If the CI have passed, check all reviews
userReviews, err := c.getReviews(owner, repoName, prNumber)
if err != nil {
return err
}
if review != nil {
// We have received a review event. We have the most updated review
// from this user so we need to update it with the information that
// we already have because GH might keep a cache of the previous review
// done by that user.
addReview(userReviews, review)
}
var (
requestedReviews []string
approvals int
userChangesRequested = map[string]struct{}{}
)
for _, userReview := range userReviews {
// request reviews for users that have stale reviews
// (stale review is a review that was done before the PR was resynced
// by the author)
switch strings.ToLower(userReview.GetState()) {
case "changes_requested":
if userReview.SubmittedAt.Before(commitDate.Time) {
requestedReviews = append(
requestedReviews,
userReview.GetUser().GetLogin(),
)
} else {
userChangesRequested[userReview.GetUser().GetLogin()] = struct{}{}
}
case "approve", "approved":
approvals++
}
}
if len(requestedReviews) != 0 {
c.log.Info().Fields(map[string]interface{}{
"users-requested-changes": requestedReviews,
"pr-number": prNumber,
}).Msg("Requesting reviews for users")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, _, err = c.GHClient.PullRequests.RequestReviewers(ctx, owner, repoName, prNumber, gh.ReviewersRequest{
Reviewers: requestedReviews,
})
// We don't continue if we just have requested for new reviews
return err
}
// Check if we still have pending reviewers
users, teams, err := c.getPendingReviews(owner, repoName, prNumber)
if err != nil {
return err
}
// If the user has requested for changes we can delete them from here
// because we are already waiting for a review from them.
for user := range users {
delete(userChangesRequested, user)
}
if review != nil {
// We have received a review event. We have the most updated review
// from this user so we need to update it with the information that
// we already have because GH might keep a cache of the previous review
// done by that user.
switch strings.ToLower(review.GetState()) {
case "changes_requested":
userChangesRequested[review.GetUser().GetLogin()] = struct{}{}
case "approve", "approved":
delete(users, review.GetUser().GetLogin())
delete(userChangesRequested, review.GetUser().GetLogin())
}
}
if approvals < cfg.MinimalApprovals || len(users) != 0 || len(teams) != 0 || len(userChangesRequested) != 0 {
c.log.Info().Fields(map[string]interface{}{
"teams": teams,
"users": users,
"users-requested-changes": userChangesRequested,
"min-approvals": cfg.MinimalApprovals,
"total-approvals": approvals,
"pr-number": prNumber,
}).Msg("Users have requested changes, the author hasn't synced the PR or the PR does not have the minimal approvals")
// Only review the label if we know that exists or that we are handling
// a PR review event (review != nil).
if _, ok := prLabels[cfg.Label]; ok || review != nil {
c.log.Info().Msg("Removing ready-to-merge label")
_, err := c.GHClient.Issues.RemoveLabelForIssue(
context.Background(), owner, repoName, prNumber, cfg.Label)
if err != nil && !IsNotFound(err) {
return err
}
delete(prLabels, cfg.Label)
}
return nil
}
if false {
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, _, err = c.GHClient.Issues.CreateComment(ctx, owner, repoName, prNumber, &gh.IssueComment{
Body: func() *string { a := fmt.Sprintf("Setting %s to let a human merge this PR.", cfg.Label); return &a }(),
})
if err != nil {
return err
}
}
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, _, err = c.GHClient.Issues.AddLabelsToIssue(ctx, owner, repoName, prNumber, []string{cfg.Label})
if err != nil {
return err
}
c.log.Info().Fields(map[string]interface{}{
"teams": teams,
"users": users,
"users-requested-changes": userChangesRequested,
"min-approvals": cfg.MinimalApprovals,
"total-approvals": approvals,
"pr-number": prNumber,
}).Msg("Set 'ready-to-merge'")
prLabels[cfg.Label] = struct{}{}
return nil
}
// getCIStatus returns a list of CI Checks that are not successful.
func (c *Client) getCIStatus(
owner, repoName string,
base,
head *gh.PullRequestBranch,
prNumber int) ([]string, error) {
var (
cancels []context.CancelFunc
)
defer func() {
for _, cancel := range cancels {
cancel()
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cancels = append(cancels, cancel)
nextPage := 0
brProt, _, err := c.GHClient.Repositories.GetBranchProtection(ctx, owner, repoName, base.GetRef())
if err != nil {
return nil, err
}
requiredContexts := map[string]struct{}{}
for _, ctx := range *brProt.GetRequiredStatusChecks().Contexts {
requiredContexts[ctx] = struct{}{}
}
passedContexts := map[string]bool{}
for {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cancels = append(cancels, cancel)
gs, resp, err := c.GHClient.Repositories.GetCombinedStatus(ctx, owner, repoName, head.GetSHA(), &gh.ListOptions{
Page: nextPage,
})
if err != nil {
return nil, err
}
for _, statuses := range gs.Statuses {
if statuses.GetState() == "success" {
if _, ok := passedContexts[statuses.GetContext()]; !ok {
passedContexts[statuses.GetContext()] = true
}
}
}
nextPage = resp.NextPage
if nextPage != 0 {
continue
}
break
}
nextPage = 0
for {
lc, resp, err := c.GHClient.Checks.ListCheckRunsForRef(ctx, owner, repoName, head.GetSHA(), &gh.ListCheckRunsOptions{
ListOptions: gh.ListOptions{
Page: nextPage,
},
})
if err != nil {
return nil, err
}
for _, cr := range lc.CheckRuns {
if cr.GetConclusion() == "success" || cr.GetConclusion() == "skipped" {
if _, ok := passedContexts[cr.GetName()]; !ok {
passedContexts[cr.GetName()] = true
}
} else {
passedContexts[cr.GetName()] = false
}
}
nextPage = resp.NextPage
if nextPage != 0 {
continue
}
break
}
for reqCtxName := range requiredContexts {
// Remove the contexts that have passed from the requiredContexts
if passedContexts[reqCtxName] {
delete(requiredContexts, reqCtxName)
}
}
ciChecks := make([]string, 0, len(requiredContexts))
for reqCtxName := range requiredContexts {
ciChecks = append(ciChecks, reqCtxName)
}
return ciChecks, nil
}
func addReview(recentReviewsByUser map[string]*gh.PullRequestReview, review *gh.PullRequestReview) {
userName := review.GetUser().GetLogin()
userReview, ok := recentReviewsByUser[userName]
if !ok {
recentReviewsByUser[userName] = review
return
}
// We have the most up to date review from a user in in the
// following conditions:
// CHANGES_REQUESTED overwrites any previous review
// APPROVE overwrites any previous review
// COMMENTED is only kept if no other APPROVE nor CHANGES_REQUESTED
// have been made
if review.GetSubmittedAt().After(userReview.GetSubmittedAt().Time) {
switch strings.ToLower(review.GetState()) {
case "changes_requested", "approved":
recentReviewsByUser[userName] = review
}
}
}
func (c *Client) getReviews(owner string, repoName string, prNumber int) (map[string]*gh.PullRequestReview, error) {
var cancels []context.CancelFunc
defer func() {
for _, cancel := range cancels {
cancel()
}
}()
recentReviewsByUser := map[string]*gh.PullRequestReview{}
nextPage := 0
for {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cancels = append(cancels, cancel)
reviews, resp, err := c.GHClient.PullRequests.ListReviews(ctx, owner, repoName, prNumber, &gh.ListOptions{
Page: nextPage,
})
if err != nil {
return nil, err
}
for _, review := range reviews {
addReview(recentReviewsByUser, review)
}
nextPage = resp.NextPage
if nextPage != 0 {
continue
}
break
}
return recentReviewsByUser, nil
}
func (c *Client) getPendingReviews(owner string, repoName string, prNumber int) (map[string]struct{}, map[string]struct{}, error) {
nextPage := 0
var (
users = map[string]struct{}{}
teams = map[string]struct{}{}
cancels []context.CancelFunc
)
for {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cancels = append(cancels, cancel)
reviewers, resp, err := c.GHClient.PullRequests.ListReviewers(ctx, owner, repoName, prNumber, &gh.ListOptions{
Page: nextPage,
})
if err != nil {
return nil, nil, err
}
if len(reviewers.Teams) != 0 ||
len(reviewers.Users) != 0 {
for _, user := range reviewers.Users {
users[user.GetLogin()] = struct{}{}
}
for _, team := range reviewers.Teams {
teams[team.GetName()] = struct{}{}
}
}
nextPage = resp.NextPage
if nextPage != 0 {
continue
}
break
}
return users, teams, nil
}