-
Notifications
You must be signed in to change notification settings - Fork 6
/
merge.go
124 lines (102 loc) · 2.94 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
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"
)
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
}
pr := ne.PullRequest
org, repo := ne.GetOrgRep()
if r := canMerge(pr.Mergeable, ne.GetPRLabels(), cfg); 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, ne.GetPRNumber(), string(cfg.MergeMethod),
)
}
func (bot *robot) tryMerge(e *sdk.PullRequestEvent, cfg *botConfig) error {
if giteeclient.GetPullRequestAction(e) != giteeclient.PRActionUpdatedLabel {
return nil
}
pr := e.PullRequest
info := giteeclient.GetPRInfoByPREvent(e)
if r := canMerge(pr.Mergeable, info.Labels, cfg); len(r) > 0 {
return nil
}
return bot.mergePR(
pr.NeedReview || pr.NeedTest,
info.Org, info.Repo, info.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) []string {
if !mergeable {
return []string{msgPRConflicts}
}
reasons := []string{}
needs := sets.NewString(approvedLabel)
needs.Insert(cfg.LabelsForMerge...)
if ln := cfg.LgtmCountsRequired; ln == 1 {
needs = sets.NewString(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
}