-
Notifications
You must be signed in to change notification settings - Fork 12
/
merge.go
154 lines (124 loc) · 3.77 KB
/
merge.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
package main
import (
"fmt"
"regexp"
"strings"
sdk "gitee.com/openeuler/go-gitee/gitee"
"github.com/opensourceways/community-robot-lib/giteeclient"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
msgPRConflicts = "PR conflicts to the target branch."
msgMissingLabels = "PR does not have these lables: %s"
msgInvalidLabels = "PR should remove these labels: %s"
msgNotEnoughLGTMLabel = "PR needs %d lgtm labels and now gets %d"
msgFrozenWithOwner = "PR merge target has been frozen, and can merge only by branch owners: %s"
)
var regCheckPr = regexp.MustCompile(`(?mi)^/check-pr\s*$`)
func (bot *robot) handleCheckPR(e *sdk.NoteEvent, cfg *botConfig) error {
ne := giteeclient.NewPRNoteEvent(e)
if !ne.IsPullRequest() ||
!ne.IsPROpen() ||
!ne.IsCreatingCommentEvent() ||
!regCheckPr.MatchString(ne.GetComment()) {
return nil
}
org, repo := ne.GetOrgRep()
pr := e.GetPullRequest()
freeze, err := bot.getFreezeInfo(org, pr.GetBase().GetRef(), cfg.FreezeFile)
if err != nil {
return err
}
if r := canMerge(pr.Mergeable, ne.GetPRLabels(), cfg, freeze.getFrozenMsg(ne.GetCommenter())); len(r) > 0 {
return bot.cli.CreatePRComment(
org, repo, ne.GetPRNumber(),
fmt.Sprintf(
"@%s , this pr is not mergeable and the reasons are below:\n%s",
ne.GetCommenter(), strings.Join(r, "\n"),
),
)
}
return bot.mergePR(
pr.NeedReview || pr.NeedTest,
org, repo, pr.Number, string(cfg.MergeMethod),
)
}
func (bot *robot) handleLabelUpdate(e *sdk.PullRequestEvent, cfg *botConfig) error {
if giteeclient.GetPullRequestAction(e) != giteeclient.PRActionUpdatedLabel {
return nil
}
org, repo := giteeclient.GetOwnerAndRepoByPREvent(e)
pr := e.GetPullRequest()
freeze, err := bot.getFreezeInfo(org, pr.GetBase().GetRef(), cfg.FreezeFile)
if err != nil {
return err
}
return bot.tryMerge(org, repo, pr, cfg, freeze.getFrozenMsg())
}
func (bot *robot) tryMerge(org, repo string, pr *sdk.PullRequestHook, cfg *botConfig, isFreeze func() string) error {
if r := canMerge(pr.Mergeable, nil, cfg, isFreeze); len(r) > 0 {
return nil
}
return bot.mergePR(
pr.NeedReview || pr.NeedTest,
org, repo, pr.Number, string(cfg.MergeMethod),
)
}
func (bot *robot) mergePR(needReviewOrTest bool, org, repo string, number int32, method string) error {
if needReviewOrTest {
v := int32(0)
p := sdk.PullRequestUpdateParam{
AssigneesNumber: &v,
TestersNumber: &v,
}
if _, err := bot.cli.UpdatePullRequest(org, repo, number, p); err != nil {
return err
}
}
return bot.cli.MergePR(
org, repo, number,
sdk.PullRequestMergePutParam{
MergeMethod: method,
},
)
}
func canMerge(mergeable bool, labels sets.String, cfg *botConfig, isFreeze func() string) []string {
var reasons []string
if !mergeable {
reasons = append(reasons, msgPRConflicts)
}
if r := isLabelMatched(labels, cfg); len(r) > 0 {
reasons = append(reasons, r...)
}
if r := isFreeze(); r != "" {
reasons = append(reasons, r)
}
return reasons
}
func isLabelMatched(labels sets.String, cfg *botConfig) []string {
var reasons []string
needs := sets.NewString(approvedLabel)
needs.Insert(cfg.LabelsForMerge...)
if ln := cfg.LgtmCountsRequired; ln == 1 {
needs.Insert(lgtmLabel)
} else {
v := getLGTMLabelsOnPR(labels)
if n := uint(len(v)); n < ln {
reasons = append(reasons, fmt.Sprintf(msgNotEnoughLGTMLabel, ln, n))
}
}
if v := needs.Difference(labels); v.Len() > 0 {
reasons = append(reasons, fmt.Sprintf(
msgMissingLabels, strings.Join(v.UnsortedList(), ", "),
))
}
if len(cfg.MissingLabelsForMerge) > 0 {
missing := sets.NewString(cfg.MissingLabelsForMerge...)
if v := missing.Intersection(labels); v.Len() > 0 {
reasons = append(reasons, fmt.Sprintf(
msgInvalidLabels, strings.Join(v.UnsortedList(), ", "),
))
}
}
return reasons
}