forked from review-robot/robot-gitee-openeuler-review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.go
226 lines (180 loc) · 4.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
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
package main
import (
"encoding/base64"
"fmt"
"regexp"
"strings"
sdk "gitee.com/openeuler/go-gitee/gitee"
"github.com/opensourceways/community-robot-lib/giteeclient"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"
)
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 = "The target branch of PR has been frozen and it can be merge only by branch owners: %s"
)
var regCheckPr = regexp.MustCompile(`(?mi)^/check-pr\s*$`)
func (bot *robot) handleCheckPR(e *sdk.NoteEvent, cfg *botConfig, log *logrus.Entry) error {
ne := giteeclient.NewPRNoteEvent(e)
if !ne.IsPullRequest() ||
!ne.IsPROpen() ||
!ne.IsCreatingCommentEvent() ||
!regCheckPr.MatchString(ne.GetComment()) {
return nil
}
return bot.tryMerge(ne, cfg, true, log)
}
func (bot *robot) tryMerge(e giteeclient.PRNoteEvent, cfg *botConfig, addComment bool, log *logrus.Entry) error {
org, repo := e.GetOrgRep()
h := mergeHelper{
cfg: cfg,
org: org,
repo: repo,
cli: bot.cli,
pr: e.GetPullRequest(),
trigger: e.GetCommenter(),
}
if r, ok := h.canMerge(log); !ok {
if len(r) > 0 && addComment {
return bot.cli.CreatePRComment(
org, repo, e.GetPRNumber(),
fmt.Sprintf(
"@%s , this pr is not mergeable and the reasons are below:\n%s",
e.GetCommenter(), strings.Join(r, "\n"),
),
)
}
return nil
}
return h.merge()
}
func (bot *robot) handleLabelUpdate(e *sdk.PullRequestEvent, cfg *botConfig, log *logrus.Entry) error {
if giteeclient.GetPullRequestAction(e) != giteeclient.PRActionUpdatedLabel {
return nil
}
org, repo := giteeclient.GetOwnerAndRepoByPREvent(e)
h := mergeHelper{
cfg: cfg,
org: org,
repo: repo,
cli: bot.cli,
pr: e.GetPullRequest(),
}
if _, ok := h.canMerge(log); ok {
return h.merge()
}
return nil
}
type mergeHelper struct {
pr *sdk.PullRequestHook
cfg *botConfig
org string
repo string
trigger string
cli iClient
}
func (m *mergeHelper) merge() error {
number := m.pr.Number
if m.pr.NeedReview || m.pr.NeedTest {
v := int32(0)
p := sdk.PullRequestUpdateParam{
AssigneesNumber: &v,
TestersNumber: &v,
}
if _, err := m.cli.UpdatePullRequest(m.org, m.repo, number, p); err != nil {
return err
}
}
return m.cli.MergePR(
m.org, m.repo, number,
sdk.PullRequestMergePutParam{
MergeMethod: string(m.cfg.MergeMethod),
},
)
}
func (m *mergeHelper) canMerge(log *logrus.Entry) ([]string, bool) {
if !m.pr.GetMergeable() {
return []string{msgPRConflicts}, false
}
labels := sets.NewString()
for _, item := range m.pr.Labels {
labels.Insert(item.Name)
}
if r := isLabelMatched(labels, m.cfg); len(r) > 0 {
return r, false
}
freeze, err := m.getFreezeInfo(log)
if err != nil {
return nil, false
}
if freeze == nil || !freeze.isFrozen() {
return nil, true
}
if m.trigger == "" {
return nil, false
}
if freeze.isOwner(m.trigger) {
return nil, true
}
return []string{
fmt.Sprintf(msgFrozenWithOwner, strings.Join(freeze.Owner, ", ")),
}, false
}
func (m *mergeHelper) getFreezeInfo(log *logrus.Entry) (*freezeItem, error) {
branch := m.pr.GetBase().GetRef()
for _, v := range m.cfg.FreezeFile {
fc, err := m.getFreezeContent(v)
if err != nil {
log.Errorf("get freeze file:%s, err:%s", v.toString(), err.Error())
return nil, err
}
if v := fc.getFreezeItem(m.org, branch); v != nil {
return v, nil
}
}
return nil, nil
}
func (m *mergeHelper) getFreezeContent(f freezeFile) (freezeContent, error) {
var fc freezeContent
c, err := m.cli.GetPathContent(f.Owner, f.Repo, f.Path, f.Branch)
if err != nil {
return fc, err
}
b, err := base64.StdEncoding.DecodeString(c.Content)
if err != nil {
return fc, err
}
err = yaml.Unmarshal(b, &fc)
return fc, err
}
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
}