forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guide.go
227 lines (186 loc) · 6.18 KB
/
guide.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
227
package main
import (
"fmt"
"strings"
)
const (
notificationTitle = "### Review Guide\n\nThis Pull-Request"
notificationTitleOld = "### ~~~ Approval ~~~ Notifier ~~~\nThis Pull-Request"
notificationSpliter = "\n#### Tips:\n"
notificationSpliterOld = "\n\n---\n\n"
notificationLineSpliter = "\n"
notificationLGTMPart2 = "In order to add **lgtm** label"
notificationApprovePart2 = "In order to add **approved** label"
notificationReviewersSpliter = ", "
reviewStatusStart = "gets ready to be reviewed"
reviewStatusInProgress = "is being reviewed"
reviewStatusRejected = "is **Rejected**"
reviewStatusChange = "is **Requested Change**"
reviewStatusLGTM = "is added **lgtm** label"
reviewStatusApproved = "is added **approved** label"
reviewStatusPassReview = "**Passes Review**"
)
func newNotificationComment(rs *reviewSummary, s, botName string) notificationComment {
return notificationComment{rs: rs, oldTips: s, botName: botName}
}
type notificationComment struct {
rs *reviewSummary
oldTips string
botName string
}
func (n notificationComment) genApproveTips(num int, approvers []string) string {
return fmt.Sprintf(
"%s, it still needs **%d** approvers to comment /approve.\nI suggest these approvers( %s ) to approve your PR.\nYou can assign the PR to them by writing a comment like this `/assign @%s`. Please, replace `%s` with the correct approver's name.",
notificationApprovePart2,
num,
toReviewerList(approvers),
n.botName,
n.botName,
)
}
func (n notificationComment) genLGTMTips(num int, suggestedReviewers []string) string {
key := "reviewers to comment /lgtm."
p1 := fmt.Sprintf("%s, it still needs **%d** %s", notificationLGTMPart2, num, key)
if len(suggestedReviewers) > 0 {
s2 := fmt.Sprintf(
"\nI suggest these reviewers( %s ) to review your codes.\nYou can ask them to review by writing a comment like this `@%s, Could you take a look at this PR, thanks!`. Please, replace `%s` with correct reviewer's name",
toReviewerList(suggestedReviewers),
n.botName,
n.botName,
)
return n.genPart2(p1 + s2)
}
if !containsSuggestedReviewer(n.oldTips) || !strings.Contains(n.oldTips, key) {
return ""
}
if v := strings.Split(n.oldTips, key); len(v) > 1 {
return n.genPart2(p1 + v[1])
}
return ""
}
func (n notificationComment) startReviewComment(reviewers []string) string {
s := n.genLGTMTips(len(reviewers), reviewers)
return fmt.Sprintf("%s %s.%s", notificationTitle, reviewStatusStart, s)
}
func (n notificationComment) reviewingComment(num int, reviewers []string) string {
tips := ""
if v := n.rs.disagreedReviewers; len(v) > 0 {
tips = fmt.Sprintf(
"%sReviewers who writed a comment of `/lbtm` are: %s. Please make changes if it needs.",
notificationLGTMPart2, toReviewerList(v),
)
}
if s := n.reviewInfo(); s != "" {
tips += notificationLineSpliter + s
}
p2 := n.genLGTMTips(num, reviewers)
return fmt.Sprintf("%s %s.%s%s", notificationTitle, reviewStatusInProgress, tips, p2)
}
func (n notificationComment) rejectComment() string {
return fmt.Sprintf(
"%s %s.%sIt is rejected by: %s. Please see the comments left by them and do more changes.",
notificationTitle,
reviewStatusRejected,
notificationLineSpliter,
toReviewerList(n.rs.disagreedApprovers),
)
}
func (n notificationComment) requestChangeComment() string {
return fmt.Sprintf(
"%s %s.%sIt is requested change by: %s. Please see the comments left by them and do more changes.",
notificationTitle,
reviewStatusChange,
notificationLineSpliter,
toReviewerList(n.rs.disagreedReviewers),
)
}
func (n notificationComment) passReviewComment() string {
s := n.reviewInfo()
if s != "" {
s = notificationLineSpliter + s
}
return fmt.Sprintf("%s %s.%s", notificationTitle, reviewStatusPassReview, s)
}
func (n notificationComment) approvedComment(num int, suggestedReviewers []string) string {
s := n.reviewInfo()
if s != "" {
s = notificationLineSpliter + s
}
s1 := n.genLGTMTips(num, suggestedReviewers)
return fmt.Sprintf(
"%s %s. In order to pass review, it still needs **lgtm** label.%s%s",
notificationTitle, reviewStatusApproved, s, s1,
)
}
func (n notificationComment) lgtmComment(suggestedApprovers []string) string {
s := n.reviewInfo()
if s != "" {
s = notificationLineSpliter + s
}
s1 := n.getPart2OfApproved(suggestedApprovers)
return fmt.Sprintf(
"%s %s. In order to pass review, it still needs **approved** label.%s%s",
notificationTitle, reviewStatusLGTM, s, s1,
)
}
func (n notificationComment) reviewInfo() string {
rs := n.rs
s := ""
if len(rs.agreedApprovers) > 0 {
s = fmt.Sprintf(
"Approvers who writed a comment of `/approve` are: %s.",
toReviewerList(rs.agreedApprovers),
)
}
s1 := ""
if len(rs.agreedReviewers) > 0 {
s1 = fmt.Sprintf(
"Reviewers who writed a comment of `/lgtm` are: %s.",
toReviewerList(rs.agreedReviewers),
)
}
if s != "" && s1 != "" {
return s + notificationLineSpliter + s1
}
return s + s1
}
func (n notificationComment) getPart2OfApproved(suggestedApprovers []string) string {
if num := len(suggestedApprovers); num > 0 {
return n.genPart2(n.genApproveTips(num, suggestedApprovers))
}
if !containsSuggestedApprover(n.oldTips) {
return ""
}
if v := strings.Split(n.oldTips, notificationSpliter); len(v) == 2 {
return n.genPart2(v[1])
}
if v := strings.Split(n.oldTips, notificationSpliterOld); len(v) == 2 {
return n.genPart2(v[1])
}
return ""
}
func (n notificationComment) genPart2(c string) string {
if c != "" {
return notificationSpliter + c
}
return c
}
func convertReviewers(v []string) []string {
rs := make([]string, 0, len(v))
for _, item := range v {
rs = append(rs, fmt.Sprintf("[*%s*](https://gitee.com/%s)", item, item))
}
return rs
}
func toReviewerList(v []string) string {
return strings.Join(convertReviewers(v), notificationReviewersSpliter)
}
func containsSuggestedApprover(c string) bool {
return strings.Contains(c, notificationApprovePart2)
}
func containsSuggestedReviewer(c string) bool {
return strings.Contains(c, notificationLGTMPart2)
}
func isNotificationComment(c string) bool {
return strings.HasPrefix(c, notificationTitle) || strings.HasPrefix(c, notificationTitleOld)
}