-
Notifications
You must be signed in to change notification settings - Fork 12
/
lgtm.go
175 lines (139 loc) · 4.22 KB
/
lgtm.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
package main
import (
"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"
)
const (
// the gitee platform limits the maximum length of label to 20.
labelLenLimit = 20
lgtmLabel = "lgtm"
commentAddLGTMBySelf = "***lgtm*** can not be added in your self-own pull request. :astonished:"
commentClearLabel = `New code changes of pr are detected and remove these labels ***%s***. :flushed: `
commentNoPermissionForLabel = `
***@%s*** has no permission to %s ***%s*** label in this pull request. :astonished:
Please contact to the collaborators in this repository.`
)
var (
regAddLgtm = regexp.MustCompile(`(?mi)^/lgtm\s*$`)
regRemoveLgtm = regexp.MustCompile(`(?mi)^/lgtm cancel\s*$`)
)
func (bot *robot) handleLGTM(e *sdk.NoteEvent, cfg *botConfig, log *logrus.Entry) error {
ne := giteeclient.NewPRNoteEvent(e)
if !ne.IsPullRequest() || !ne.IsPROpen() || !ne.IsCreatingCommentEvent() {
return nil
}
if regAddLgtm.MatchString(ne.GetComment()) {
return bot.addLGTM(cfg, ne, log)
}
if regRemoveLgtm.MatchString(ne.GetComment()) {
return bot.removeLGTM(cfg, ne, log)
}
return nil
}
func (bot *robot) addLGTM(cfg *botConfig, e giteeclient.PRNoteEvent, log *logrus.Entry) error {
pr := e.GetPRInfo()
org, repo, number := pr.Org, pr.Repo, pr.Number
commenter := e.GetCommenter()
if pr.Author == commenter {
return bot.cli.CreatePRComment(org, repo, number, commentAddLGTMBySelf)
}
v, err := bot.hasPermission(commenter, pr, cfg, log)
if err != nil {
return err
}
if !v {
return bot.cli.CreatePRComment(org, repo, number, fmt.Sprintf(
commentNoPermissionForLabel, commenter, "add", lgtmLabel,
))
}
label := genLGTMLabel(commenter, cfg.LgtmCountsRequired)
if label != lgtmLabel {
if err := bot.createLabelIfNeed(org, repo, label); err != nil {
log.WithError(err).Errorf("create repo label: %s", label)
}
}
if err := bot.cli.AddPRLabel(org, repo, number, label); err != nil {
return err
}
freeze, err := bot.getFreezeInfo(org, pr.BaseRef, cfg.FreezeFile)
if err != nil || !freeze.isFrozen() {
return err
}
return bot.tryMerge(org, repo, e.GetPullRequest(), cfg, freeze.getFrozenMsg(commenter))
}
func (bot *robot) removeLGTM(cfg *botConfig, e giteeclient.PRNoteEvent, log *logrus.Entry) error {
pr := e.GetPRInfo()
org, repo, number := pr.Org, pr.Repo, pr.Number
if commenter := e.GetCommenter(); pr.Author != commenter {
v, err := bot.hasPermission(commenter, pr, cfg, log)
if err != nil {
return err
}
if !v {
return bot.cli.CreatePRComment(org, repo, number, fmt.Sprintf(
commentNoPermissionForLabel, commenter, "remove", lgtmLabel,
))
}
return bot.cli.RemovePRLabel(
org, repo, number,
genLGTMLabel(commenter, cfg.LgtmCountsRequired),
)
}
// the author of pr can remove all of lgtm[-login name] kind labels
if v := getLGTMLabelsOnPR(pr.Labels); len(v) > 0 {
return bot.cli.RemovePRLabels(org, repo, number, v)
}
return nil
}
func (bot *robot) createLabelIfNeed(org, repo, label string) error {
repoLabels, err := bot.cli.GetRepoLabels(org, repo)
if err != nil {
return err
}
for _, v := range repoLabels {
if v.Name == label {
return nil
}
}
return bot.cli.CreateRepoLabel(org, repo, label, "")
}
func (bot *robot) clearLabel(e *sdk.PullRequestEvent) error {
if giteeclient.GetPullRequestAction(e) != giteeclient.PRActionChangedSourceBranch {
return nil
}
pr := giteeclient.GetPRInfoByPREvent(e)
if v := getLGTMLabelsOnPR(pr.Labels); len(v) > 0 {
if err := bot.cli.RemovePRLabels(pr.Org, pr.Repo, pr.Number, v); err != nil {
return err
}
return bot.cli.CreatePRComment(
pr.Org, pr.Repo, pr.Number,
fmt.Sprintf(commentClearLabel, strings.Join(v, ", ")),
)
}
return nil
}
func genLGTMLabel(commenter string, lgtmCount uint) string {
if lgtmCount <= 1 {
return lgtmLabel
}
l := fmt.Sprintf("%s-%s", lgtmLabel, strings.ToLower(commenter))
if len(l) > labelLenLimit {
return l[:labelLenLimit]
}
return l
}
func getLGTMLabelsOnPR(labels sets.String) []string {
var r []string
for l := range labels {
if strings.HasPrefix(l, lgtmLabel) {
r = append(r, l)
}
}
return r
}