-
Notifications
You must be signed in to change notification settings - Fork 5
/
merges.go
83 lines (71 loc) · 2.48 KB
/
merges.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 dreck
import (
"context"
"fmt"
"github.com/miekg/dreck/log"
"github.com/google/go-github/v28/github"
)
func (d Dreck) pullRequestMerge(ctx context.Context, client *github.Client, req IssueCommentOuter, pull *github.PullRequest) (*github.Response, error) {
opt := &github.PullRequestOptions{MergeMethod: d.strategy}
msg := "Automatically submitted."
_, resp, err := client.PullRequests.Merge(ctx, req.Repository.Owner.Login, req.Repository.Name, *pull.Number, msg, opt)
return resp, err
}
func (d Dreck) pullRequestStatus(ctx context.Context, client *github.Client, req IssueCommentOuter, pull *github.PullRequest) (bool, error) {
listOpts := &github.ListOptions{PerPage: 100}
combined, _, err := client.Repositories.GetCombinedStatus(ctx, req.Repository.Owner.Login, req.Repository.Name, pull.Head.GetSHA(), listOpts)
if err != nil {
return false, err
}
log.Infof("Checking %d statuses for PR %d", combined.GetTotalCount(), pull.GetNumber())
for _, status := range combined.Statuses {
if status.GetState() != statusOK {
log.Infof("Status %s is %s", status.GetContext(), status.GetState())
return false, nil
}
}
return true, nil
}
func (d Dreck) pullRequestReviewed(ctx context.Context, client *github.Client, req IssueCommentOuter, pull *github.PullRequest) (bool, error) {
reviews, err := ListReviews(ctx, client, req, pull)
if err != nil {
return false, err
}
ok := false
for _, review := range reviews {
if review.GetState() == "REQUEST_CHANGES" {
ok = false
break
}
if review.GetState() == "APPROVED" {
ok = true
}
}
if !ok {
return false, fmt.Errorf("PR %d is no reviewers or has a %s", pull.GetNumber(), "REQUEST_CHANGES")
}
return true, nil
}
func (d Dreck) merge(ctx context.Context, client *github.Client, req IssueCommentOuter) (*github.Response, error) {
pull, _, err := client.PullRequests.Get(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number)
if err != nil {
// Pr does not exist, noop.
return nil, err
}
if pull.ClosedAt != nil {
// Pr has been closed or deleted. Don't merge!
return nil, fmt.Errorf("PR %d has been deleted at %s", req.Issue.Number, pull.GetClosedAt())
}
ok1, _ := d.pullRequestStatus(ctx, client, req, pull)
ok2, _ := d.pullRequestReviewed(ctx, client, req, pull)
if ok1 && ok2 && pull.Mergeable != nil {
return d.pullRequestMerge(ctx, client, req, pull)
}
return nil, nil
}
const (
statusOK = "success"
statusPending = "pending"
statusFail = "failure"
statusError = "error"
)