forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
199 lines (170 loc) · 4.09 KB
/
models.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
package main
import (
"regexp"
"strings"
"github.com/opensourceways/community-robot-lib/utils"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
labelCanReview = "can-review"
labelLGTM = "lgtm"
labelApproved = "approved"
labelRequestChange = "request-change"
cmdLGTM = "LGTM"
cmdLBTM = "LBTM"
cmdAPPROVE = "APPROVE"
cmdReject = "REJECT"
)
var (
validCmds = sets.NewString(cmdLGTM, cmdLBTM, cmdAPPROVE, cmdReject)
negativeCmds = sets.NewString(cmdReject, cmdLBTM)
positiveCmds = sets.NewString(cmdAPPROVE, cmdLGTM)
cmdBelongsToApprover = sets.NewString(cmdAPPROVE, cmdReject)
commandRegex = regexp.MustCompile(`(?m)^/([^\s]+)[\t ]*([^\n\r]*)`)
)
func canApplyCmd(cmd string, isPRAuthor, isApprover, allowSelfApprove bool) bool {
switch cmd {
case cmdReject:
return isApprover && !isPRAuthor
case cmdLGTM:
return !isPRAuthor
case cmdAPPROVE:
return isApprover && (allowSelfApprove || !isPRAuthor)
}
return true
}
func parseReviewCommand(comment string) []string {
r := []string{}
for _, match := range commandRegex.FindAllStringSubmatch(comment, -1) {
cmd := strings.ToUpper(match[1])
if validCmds.Has(cmd) {
r = append(r, cmd)
}
}
return r
}
type reviewSummary struct {
agreedApprovers []string
agreedReviewers []string
disagreedApprovers []string
disagreedReviewers []string
}
func (r reviewSummary) NumberOfAssentor() int {
return len(r.agreedApprovers) + len(r.agreedReviewers)
}
func (r reviewSummary) IsEmpty() bool {
v := []int{
len(r.agreedApprovers),
len(r.agreedReviewers),
len(r.disagreedApprovers),
len(r.disagreedReviewers),
}
for _, item := range v {
if item > 0 {
return false
}
}
return true
}
type reviewCommand struct {
author string
command string
}
func genReviewSummary(cmds []reviewCommand) reviewSummary {
agreedApprovers := sets.NewString()
agreedReviewers := sets.NewString()
disagreedApprovers := sets.NewString()
disagreedReviewers := sets.NewString()
for _, c := range cmds {
switch c.command {
case cmdLGTM:
agreedReviewers.Insert(c.author)
case cmdAPPROVE:
agreedApprovers.Insert(c.author)
case cmdReject:
disagreedApprovers.Insert(c.author)
case cmdLBTM:
disagreedReviewers.Insert(c.author)
}
}
return reviewSummary{
agreedApprovers: agreedApprovers.List(),
agreedReviewers: agreedReviewers.List(),
disagreedApprovers: disagreedApprovers.List(),
disagreedReviewers: disagreedReviewers.List(),
}
}
func getReviewCommand(comment, author string, isValidCmd func(cmd, author string) bool) (validCmd string, invalidCmd string) {
cmds := parseReviewCommand(comment)
if len(cmds) == 0 {
return
}
negatives := map[string]bool{}
positives := map[string]bool{}
for _, cmd := range cmds {
if !isValidCmd(cmd, author) {
if invalidCmd == "" {
invalidCmd = cmd
}
continue
}
validCmd = cmd
if negativeCmds.Has(cmd) {
negatives[cmd] = true
}
if positiveCmds.Has(cmd) {
positives[cmd] = true
}
}
if len(negatives) == 0 && positives[cmdAPPROVE] {
validCmd = cmdAPPROVE
}
return
}
type reviewResult struct {
isRejected bool
isApproved bool
isLGTM bool
isLBTM bool
needLGTMNum int
}
func genReviewResult(r reviewSummary, allFilesApproved func([]string, int) bool, cfg reviewConfig) reviewResult {
rr := reviewResult{}
if len(r.disagreedApprovers) > 0 {
rr.isRejected = true
return rr
}
an := len(r.agreedApprovers)
if allFilesApproved(r.agreedApprovers, cfg.NumberOfApprovers) {
rr.isApproved = an >= cfg.TotalNumberOfApprovers
}
rn := an + len(r.agreedReviewers)
f := func() {
rr.isLGTM = rn >= cfg.TotalNumberOfReviewers
if !rr.isLGTM {
rr.needLGTMNum = cfg.TotalNumberOfReviewers - rn
}
}
if rr.isApproved {
// overrule the lbtm
f()
return rr
}
if rn < len(r.disagreedReviewers) {
rr.isLBTM = true
} else {
f()
}
return rr
}
func multiError() *utils.MultiError {
return utils.NewMultiErrors()
}
type iPRInfo interface {
getOrgAndRepo() (string, string)
getNumber() int32
getTargetBranch() string
hasLabel(string) bool
getAuthor() string
getHeadSHA() string
}