/
label.go
337 lines (291 loc) · 12.9 KB
/
label.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
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 label
import (
"fmt"
"regexp"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
prowlabels "k8s.io/test-infra/prow/labels"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/plugins"
)
const (
PluginName = "label"
)
var (
defaultLabels = []string{"kind", "priority", "area"}
commentRegex = regexp.MustCompile(`(?s)<!--(.*?)-->`)
labelRegex = regexp.MustCompile(`(?m)^/(area|committee|kind|language|priority|sig|triage|wg)\s*(.*?)\s*$`)
removeLabelRegex = regexp.MustCompile(`(?m)^/remove-(area|committee|kind|language|priority|sig|triage|wg)\s*(.*?)\s*$`)
customLabelRegex = regexp.MustCompile(`(?m)^/label\s*(.*?)\s*$`)
customRemoveLabelRegex = regexp.MustCompile(`(?m)^/remove-label\s*(.*?)\s*$`)
)
func init() {
plugins.RegisterGenericCommentHandler(PluginName, handleGenericComment, helpProvider)
plugins.RegisterPullRequestHandler(PluginName, handlePullRequest, helpProvider)
}
func configString(labels []string) string {
var formattedLabels []string
for _, label := range labels {
formattedLabels = append(formattedLabels, fmt.Sprintf(`"%s/*"`, label))
}
return fmt.Sprintf("The label plugin will work on %s and %s labels.", strings.Join(formattedLabels[:len(formattedLabels)-1], ", "), formattedLabels[len(formattedLabels)-1])
}
func helpProvider(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhelp.PluginHelp, error) {
labels := []string{}
labels = append(labels, defaultLabels...)
labels = append(labels, config.Label.AdditionalLabels...)
yamlSnippet, err := plugins.CommentMap.GenYaml(&plugins.Configuration{
Label: plugins.Label{
AdditionalLabels: []string{"api-review", "community/discussion"},
RestrictedLabels: map[string][]plugins.RestrictedLabel{
"*": {{
Label: "restricted-label",
AllowedTeams: []string{"authorized-team"},
AllowedUsers: []string{"alice", "bob"},
AssignOn: []plugins.AssignOnLabel{{Label: "other-label"}},
}},
},
},
})
if err != nil {
logrus.WithError(err).Warnf("cannot generate comments for %s plugin", PluginName)
}
pluginHelp := &pluginhelp.PluginHelp{
Description: "The label plugin provides commands that add or remove certain types of labels. Labels of the following types can be manipulated: 'area/*', 'committee/*', 'kind/*', 'language/*', 'priority/*', 'sig/*', 'triage/*', and 'wg/*'. More labels can be configured to be used via the /label command. Restricted labels are only able to be added by the teams and users present in their configuration, and those users can be automatically assigned when another label is added using the assign_on config.",
Config: map[string]string{
"": configString(labels),
},
Snippet: yamlSnippet,
}
pluginHelp.AddCommand(pluginhelp.Command{
Usage: "/[remove-](area|committee|kind|language|priority|sig|triage|wg|label) <target>",
Description: "Applies or removes a label from one of the recognized types of labels.",
Featured: false,
WhoCanUse: "Anyone can trigger this command on issues and PRs. `triage/accepted` can only be added by org members. Restricted labels are only able to be added by teams and users in their configuration.",
Examples: []string{"/kind bug", "/remove-area prow", "/sig testing", "/language zh", "/label foo-bar-baz"},
})
return pluginHelp, nil
}
func handleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
return handleComment(pc.GitHubClient, pc.Logger, pc.PluginConfig.Label, &e)
}
func handlePullRequest(pc plugins.Agent, e github.PullRequestEvent) error {
return handleLabelAdd(pc.GitHubClient, pc.Logger, pc.PluginConfig.Label, &e)
}
type githubClient interface {
CreateComment(owner, repo string, number int, comment string) error
AddLabel(owner, repo string, number int, label string) error
IsMember(org, user string) (bool, error)
RemoveLabel(owner, repo string, number int, label string) error
GetRepoLabels(owner, repo string) ([]github.Label, error)
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
TeamBySlugHasMember(org string, teamSlug string, memberLogin string) (bool, error)
AssignIssue(owner, repo string, number int, assignees []string) error
}
// Get Labels from Regexp matches
func getLabelsFromREMatches(matches [][]string) (labels []string) {
for _, match := range matches {
for _, label := range strings.Split(strings.TrimSpace(match[0]), " ")[1:] {
label = strings.ToLower(match[1] + "/" + strings.TrimSpace(label))
labels = append(labels, label)
}
}
return
}
// getLabelsFromGenericMatches returns label matches with extra labels if those
// have been configured in the plugin config.
func getLabelsFromGenericMatches(matches [][]string, labelFilter func(string) bool, invalidLabels *[]string) []string {
var labels []string
for _, match := range matches {
parts := strings.Split(strings.TrimSpace(match[0]), " ")
if ((parts[0] != "/label") && (parts[0] != "/remove-label")) || len(parts) != 2 {
continue
}
if labelFilter(strings.ToLower(parts[1])) {
labels = append(labels, strings.ToLower(parts[1]))
} else {
*invalidLabels = append(*invalidLabels, match[0])
}
}
return labels
}
func handleComment(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.GenericCommentEvent) error {
if e.Action != github.GenericCommentActionCreated {
return nil
}
bodyWithoutComments := commentRegex.ReplaceAllString(e.Body, "")
labelMatches := labelRegex.FindAllStringSubmatch(bodyWithoutComments, -1)
removeLabelMatches := removeLabelRegex.FindAllStringSubmatch(bodyWithoutComments, -1)
customLabelMatches := customLabelRegex.FindAllStringSubmatch(bodyWithoutComments, -1)
customRemoveLabelMatches := customRemoveLabelRegex.FindAllStringSubmatch(bodyWithoutComments, -1)
if len(labelMatches) == 0 && len(removeLabelMatches) == 0 && len(customLabelMatches) == 0 && len(customRemoveLabelMatches) == 0 {
return nil
}
org := e.Repo.Owner.Login
repo := e.Repo.Name
user := e.User.Login
repoLabels, err := gc.GetRepoLabels(org, repo)
if err != nil {
return err
}
labels, err := gc.GetIssueLabels(org, repo, e.Number)
if err != nil {
return err
}
RepoLabelsExisting := sets.String{}
for _, l := range repoLabels {
RepoLabelsExisting.Insert(strings.ToLower(l.Name))
}
var (
nonexistent []string
noSuchLabelsInRepo []string
noSuchLabelsOnIssue []string
labelsToAdd []string
labelsToRemove []string
nonMemberTriageAccepted bool
)
additionalLabelSet := sets.String{}
for _, label := range config.AdditionalLabels {
additionalLabelSet.Insert(strings.ToLower(label))
}
restrictedLabels := config.RestrictedLabelsFor(e.Repo.Owner.Login, e.Repo.Name)
labelFilter := func(label string) bool {
label = strings.ToLower(label)
_, restrictedLabel := restrictedLabels[label]
return restrictedLabel || additionalLabelSet.Has(label)
}
// Get labels to add and labels to remove from regexp matches
labelsToAdd = append(getLabelsFromREMatches(labelMatches), getLabelsFromGenericMatches(customLabelMatches, labelFilter, &nonexistent)...)
labelsToRemove = append(getLabelsFromREMatches(removeLabelMatches), getLabelsFromGenericMatches(customRemoveLabelMatches, labelFilter, &nonexistent)...)
// Add labels
for _, labelToAdd := range labelsToAdd {
if github.HasLabel(labelToAdd, labels) {
continue
}
if !RepoLabelsExisting.Has(labelToAdd) {
noSuchLabelsInRepo = append(noSuchLabelsInRepo, labelToAdd)
continue
}
// only org members can add triage/accepted
if labelToAdd == prowlabels.TriageAccepted {
if member, err := gc.IsMember(org, user); err != nil {
log.WithError(err).Errorf("error in IsMember(%s): %v", org, err)
continue
} else if !member {
nonMemberTriageAccepted = true
continue
}
}
canSetLabel, canNotSetLabelReason, err := canUserSetLabel(gc, org, e.User.Login, labelToAdd, restrictedLabels)
if err != nil {
log.WithError(err).WithField("label", labelToAdd).Error("failed to check if user can set label")
continue
}
if !canSetLabel {
gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, canNotSetLabelReason))
continue
}
if err := gc.AddLabel(org, repo, e.Number, labelToAdd); err != nil {
log.WithError(err).WithField("label", labelToAdd).Error("GitHub failed to add the label")
}
}
// Remove labels
for _, labelToRemove := range labelsToRemove {
if !github.HasLabel(labelToRemove, labels) {
noSuchLabelsOnIssue = append(noSuchLabelsOnIssue, labelToRemove)
continue
}
if !RepoLabelsExisting.Has(labelToRemove) {
continue
}
canSetLabel, canNotSetLabelReason, err := canUserSetLabel(gc, org, e.User.Login, labelToRemove, restrictedLabels)
if err != nil {
log.WithError(err).WithField("label", labelToRemove).Error("failed to check if user can set label")
continue
}
if !canSetLabel {
gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, canNotSetLabelReason))
continue
}
if err := gc.RemoveLabel(org, repo, e.Number, labelToRemove); err != nil {
log.WithError(err).WithField("label", labelToRemove).Error("GitHub failed to remove the label")
}
}
if len(nonexistent) > 0 {
log.Infof("Nonexistent labels: %v", nonexistent)
msg := fmt.Sprintf("The label(s) `%s` cannot be applied. These labels are supported: `%s`. Is this label configured under `labels -> additional_labels` or `labels -> restricted_labels` in `plugin.yaml`?", strings.Join(nonexistent, ", "), strings.Join(append(config.AdditionalLabels, sets.StringKeySet(restrictedLabels).List()...), ", "))
return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, msg))
}
if len(noSuchLabelsInRepo) > 0 {
log.Infof("Labels missing in repo: %v", noSuchLabelsInRepo)
msg := fmt.Sprintf("The label(s) `%s` cannot be applied, because the repository doesn't have them.", strings.Join(noSuchLabelsInRepo, ", "))
return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, msg))
}
// Tried to remove Labels that were not present on the Issue
if len(noSuchLabelsOnIssue) > 0 {
msg := fmt.Sprintf("Those labels are not set on the issue: `%v`", strings.Join(noSuchLabelsOnIssue, ", "))
return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, msg))
}
if nonMemberTriageAccepted {
msg := fmt.Sprintf("The label `%s` cannot be applied. Only GitHub organization members can add the label.", prowlabels.TriageAccepted)
return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(bodyWithoutComments, e.HTMLURL, e.User.Login, msg))
}
return nil
}
func canUserSetLabel(ghc githubClient, org string, user string, label string, restrictedLabels map[string]plugins.RestrictedLabel) (canSet bool, canNotSetReason string, err error) {
config, isRestricted := restrictedLabels[label]
if !isRestricted {
return true, "", nil
}
for _, allowedUser := range config.AllowedUsers {
if strings.EqualFold(allowedUser, user) {
return true, "", nil
}
}
for _, team := range config.AllowedTeams {
isMember, err := ghc.TeamBySlugHasMember(org, team, user)
if err != nil {
return false, "", err
}
if isMember {
return true, "", nil
}
}
return false, fmt.Sprintf("Can not set label %s: Must be member in one of these teams: %v", label, config.AllowedTeams), nil
}
func handleLabelAdd(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.PullRequestEvent) error {
if e.Action != github.PullRequestActionLabeled {
return nil
}
org := e.Repo.Owner.Login
repo := e.Repo.Name
number := e.PullRequest.Number
restrictedLabels := config.RestrictedLabelsFor(e.Repo.Owner.Login, e.Repo.Name)
for _, restrictedLabel := range restrictedLabels {
for _, assignOn := range restrictedLabel.AssignOn {
if strings.EqualFold(e.Label.Name, assignOn.Label) {
log.WithField("label", restrictedLabel.Label).Info("Assigning users for restricted label")
// It's okay to re-assign users so no need to check if they are assigned
if err := gc.AssignIssue(org, repo, number, restrictedLabel.AllowedUsers); err != nil {
log.WithError(err).WithField("label", restrictedLabel.Label).Error("GitHub failed to assign reviewers for the label")
}
}
}
}
return nil
}