-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
143 lines (127 loc) · 3.79 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
package main
import (
"fmt"
"strings"
"time"
"github.com/opensourceways/robot-framework-lib/client"
"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"
ActionAddLabel = "add label"
)
type labelLog struct {
label string
who string
t time.Time
}
func (bot *robot) handleMerge(configmap *repoConfig, org, repo, number string) error {
labels := bot.getPRLabelSet(org, repo, number)
ops, ok := bot.cli.ListPullRequestOperationLogs(org, repo, number)
if !ok {
return fmt.Errorf("failed to list pull request operation logs")
}
if err := checkLabelsLegal(configmap, ops, labels); err != nil {
return err
}
if reasons := isLabelMatched(configmap, labels); len(reasons) > 0 {
return fmt.Errorf(strings.Join(reasons, "\n\n"))
}
methodOfMerge := bot.genMergeMethod(org, repo, number)
if ok := bot.cli.MergePullRequest(org, repo, number, methodOfMerge); !ok {
return fmt.Errorf("failed to merge pull request")
}
return nil
}
func isLabelMatched(configmap *repoConfig, labels sets.Set[string]) []string {
var reasons []string
for _, l := range configmap.LabelsNotAllowMerge {
if labels.Has(l) {
reasons = append(reasons, fmt.Sprintf(msgInvalidLabels, l))
}
}
needs := sets.New[string](approvedLabel)
needs.Insert(configmap.LabelsForMerge...)
if ln := configmap.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 {
vl := v.UnsortedList()
var vlp []string
for _, i := range vl {
vlp = append(vlp, fmt.Sprintf("***%s***", i))
}
reasons = append(reasons, fmt.Sprintf(msgMissingLabels, strings.Join(vlp, ", ")))
}
return reasons
}
func checkLabelsLegal(configmap *repoConfig, ops []client.PullRequestOperationLog, labels sets.Set[string]) error {
reason := make([]string, 0, len(labels))
needs := sets.New[string](approvedLabel)
needs.Insert(configmap.LabelsForMerge...)
if ln := configmap.LgtmCountsRequired; ln == 1 {
needs.Insert(lgtmLabel)
} else {
needs.Insert(getLGTMLabelsOnPR(labels)...)
}
legalOperator := configmap.LegalOperator
for label := range labels {
if ok := needs.Has(label); ok {
if s := isLabelLegal(ops, label, legalOperator); s != "" {
reason = append(reason, s)
}
}
}
if n := len(reason); n > 0 {
s := "label is "
if n > 1 {
s = "labels are "
}
return fmt.Errorf("**The following %s not ready**.\n\n%s", s, strings.Join(reason, "\n\n"))
}
return nil
}
func isLabelLegal(ops []client.PullRequestOperationLog, label string, legalOperator string) string {
labelLog, ok := getLatestLog(ops, label)
if !ok {
return fmt.Sprintf("The corresponding operation log is missing. you should delete "+
"the label **%s** and add it again by correct way", label)
}
if labelLog.who != legalOperator {
return fmt.Sprintf("%s You can't add **%s** by yourself, you should delete "+
"the label and add it again by correct way", labelLog.who, labelLog.label)
}
return ""
}
func getLatestLog(ops []client.PullRequestOperationLog, label string) (labelLog, bool) {
var t time.Time
index := -1
for i := range ops {
op := &ops[i]
if !strings.HasPrefix(op.Content, ActionAddLabel) || !strings.Contains(op.Content, label) {
continue
}
if index < 0 || op.CreatedAt.After(t) {
t = op.CreatedAt
index = i
}
}
if index >= 0 {
if user := ops[index].UserName; user != "" {
return labelLog{
label: label,
t: t,
who: user,
}, true
}
}
return labelLog{}, false
}