forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlgtm.go
206 lines (185 loc) · 6.35 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
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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lgtm
import (
"fmt"
"regexp"
"github.com/Sirupsen/logrus"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/plugins"
)
const pluginName = "lgtm"
var (
lgtmLabel = "lgtm"
lgtmRe = regexp.MustCompile(`(?mi)^/lgtm(?: no-issue)?\s*$`)
lgtmCancelRe = regexp.MustCompile(`(?mi)^/lgtm cancel\s*$`)
)
type event struct {
action string
org string
repo string
number int
prAuthor string
commentAuthor string
body string
assignees []github.User
hasLabel func(label string) (bool, error)
htmlurl string
}
func init() {
plugins.RegisterIssueCommentHandler(pluginName, handleIssueComment)
plugins.RegisterReviewEventHandler(pluginName, handleReview)
plugins.RegisterReviewCommentEventHandler(pluginName, handleReviewComment)
}
type githubClient interface {
IsMember(owner, login string) (bool, error)
AddLabel(owner, repo string, number int, label string) error
AssignIssue(owner, repo string, number int, assignees []string) error
CreateComment(owner, repo string, number int, comment string) error
RemoveLabel(owner, repo string, number int, label string) error
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
}
// prLabelChecker returns a function that lazily checks if a label is applied to a pr.
func prLabelChecker(gc githubClient, log *logrus.Entry, org, repo string, num int) func(string) (bool, error) {
return func(label string) (bool, error) {
labels, err := gc.GetIssueLabels(org, repo, num)
if err != nil {
return false, err
}
for _, candidate := range labels {
if candidate.Name == label {
return true, nil
}
}
return false, nil
}
}
func handleIssueComment(pc plugins.PluginClient, ic github.IssueCommentEvent) error {
// Only consider open PRs.
if !ic.Issue.IsPullRequest() || ic.Issue.State != "open" {
return nil
}
e := &event{
action: ic.Action,
org: ic.Repo.Owner.Login,
repo: ic.Repo.Name,
number: ic.Issue.Number,
prAuthor: ic.Issue.User.Login,
commentAuthor: ic.Comment.User.Login,
body: ic.Comment.Body,
assignees: ic.Issue.Assignees,
hasLabel: func(label string) (bool, error) { return ic.Issue.HasLabel(label), nil },
htmlurl: ic.Comment.HTMLURL,
}
return handle(pc.GitHubClient, pc.Logger, e)
}
func handleReview(pc plugins.PluginClient, re github.ReviewEvent) error {
e := &event{
action: re.Action,
org: re.Repo.Owner.Login,
repo: re.Repo.Name,
number: re.PullRequest.Number,
prAuthor: re.PullRequest.User.Login,
commentAuthor: re.Review.User.Login,
body: re.Review.Body,
assignees: re.PullRequest.Assignees,
hasLabel: prLabelChecker(
pc.GitHubClient,
pc.Logger,
re.Repo.Owner.Login,
re.Repo.Name,
re.PullRequest.Number,
),
htmlurl: re.Review.HTMLURL,
}
return handle(pc.GitHubClient, pc.Logger, e)
}
func handleReviewComment(pc plugins.PluginClient, rce github.ReviewCommentEvent) error {
e := &event{
action: rce.Action,
org: rce.Repo.Owner.Login,
repo: rce.Repo.Name,
number: rce.PullRequest.Number,
prAuthor: rce.PullRequest.User.Login,
commentAuthor: rce.Comment.User.Login,
body: rce.Comment.Body,
assignees: rce.PullRequest.Assignees,
hasLabel: prLabelChecker(
pc.GitHubClient,
pc.Logger,
rce.Repo.Owner.Login,
rce.Repo.Name,
rce.PullRequest.Number,
),
htmlurl: rce.Comment.HTMLURL,
}
return handle(pc.GitHubClient, pc.Logger, e)
}
func handle(gc githubClient, log *logrus.Entry, e *event) error {
if e.action != "created" && e.action != "submitted" {
return nil
}
// If we create an "/lgtm" comment, add lgtm if necessary.
// If we create a "/lgtm cancel" comment, remove lgtm if necessary.
wantLGTM := false
if lgtmRe.MatchString(e.body) {
wantLGTM = true
} else if lgtmCancelRe.MatchString(e.body) {
wantLGTM = false
} else {
return nil
}
// Allow authors to cancel LGTM. Do not allow authors to LGTM, and do not
// accept commands from any other user.
isAssignee := false
for _, user := range e.assignees {
if user.Login == e.commentAuthor {
isAssignee = true
break
}
}
isAuthor := e.commentAuthor == e.prAuthor
if isAuthor && wantLGTM {
resp := "you cannot LGTM your own PR"
log.Infof("Commenting with \"%s\".", resp)
return gc.CreateComment(e.org, e.repo, e.number, plugins.FormatResponseRaw(e.body, e.htmlurl, e.commentAuthor, resp))
} else if !isAuthor && !isAssignee {
log.Infof("Assigning %s/%s#%d to %s", e.org, e.repo, e.number, e.commentAuthor)
if err := gc.AssignIssue(e.org, e.repo, e.number, []string{e.commentAuthor}); err != nil {
msg := "assigning you to the PR failed"
if ok, merr := gc.IsMember(e.org, e.commentAuthor); merr == nil && !ok {
msg = "only kubernetes org members may be assigned issues"
} else if merr != nil {
log.WithError(merr).Errorf("Failed IsMember(%s, %s)", e.org, e.commentAuthor)
} else {
log.WithError(err).Errorf("Failed AssignIssue(%s, %s, %d, %s)", e.org, e.repo, e.number, e.commentAuthor)
}
resp := "changing LGTM is restricted to assignees, and " + msg
log.Infof("Reply to assign via /lgtm request with comment: \"%s\"", resp)
return gc.CreateComment(e.org, e.repo, e.number, plugins.FormatResponseRaw(e.body, e.htmlurl, e.commentAuthor, resp))
}
}
// Only add the label if it doesn't have it, and vice versa.
hasLGTM, err := e.hasLabel(lgtmLabel)
if err != nil {
return fmt.Errorf("failed to get the labels on %s/%s#%d: %v", e.org, e.repo, e.number, err)
}
if hasLGTM && !wantLGTM {
log.Info("Removing LGTM label.")
return gc.RemoveLabel(e.org, e.repo, e.number, lgtmLabel)
} else if !hasLGTM && wantLGTM {
log.Info("Adding LGTM label.")
return gc.AddLabel(e.org, e.repo, e.number, lgtmLabel)
}
return nil
}