forked from jenkins-x/test-infra
/
help.go
191 lines (156 loc) · 7.1 KB
/
help.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
/*
Copyright 2017 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 help
import (
"regexp"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/plugins"
)
const pluginName = "help"
var (
helpLabel = "help wanted"
goodFirstIssueLabel = "good first issue"
helpRe = regexp.MustCompile(`(?mi)^/help\s*$`)
helpRemoveRe = regexp.MustCompile(`(?mi)^/remove-help\s*$`)
helpGoodFirstIssueRe = regexp.MustCompile(`(?mi)^/good-first-issue\s*$`)
helpGoodFirstIssueRemoveRe = regexp.MustCompile(`(?mi)^/remove-good-first-issue\s*$`)
helpGuidelinesURL = "https://git.k8s.io/community/contributors/devel/help-wanted.md"
helpMsgPruneMatch = "This request has been marked as needing help from a contributor."
helpMsg = `
This request has been marked as needing help from a contributor.
Please ensure the request meets the requirements listed [here](` + helpGuidelinesURL + `).
If this request no longer meets these requirements, the label can be removed
by commenting with the ` + "`/remove-help`" + ` command.
`
goodFirstIssueMsgPruneMatch = "This request has been marked as suitable for new contributors."
goodFirstIssueMsg = `
This request has been marked as suitable for new contributors.
Please ensure the request meets the requirements listed [here](` + helpGuidelinesURL + "#good-first-issue" + `).
If this request no longer meets these requirements, the label can be removed
by commenting with the ` + "`/remove-good-first-issue`" + ` command.
`
)
func init() {
plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment, helpProvider)
}
func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) {
// The Config field is omitted because this plugin is not configurable.
pluginHelp := &pluginhelp.PluginHelp{
Description: "The help plugin provides commands that add or remove the '" + helpLabel + "' and the '" + goodFirstIssueLabel + "' labels from issues.",
}
pluginHelp.AddCommand(pluginhelp.Command{
Usage: "/[remove-](help|good-first-issue)",
Description: "Applies or removes the '" + helpLabel + "' and '" + goodFirstIssueLabel + "' labels to an issue.",
Featured: false,
WhoCanUse: "Anyone can trigger this command on a PR.",
Examples: []string{"/help", "/remove-help", "/good-first-issue", "/remove-good-first-issue"},
})
return pluginHelp, nil
}
type githubClient interface {
BotName() (string, error)
CreateComment(owner, repo string, number int, comment string) error
AddLabel(owner, repo string, number int, label string) error
RemoveLabel(owner, repo string, number int, label string) error
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
}
type commentPruner interface {
PruneComments(shouldPrune func(github.IssueComment) bool)
}
func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error {
return handle(pc.GitHubClient, pc.Logger, pc.CommentPruner, &e)
}
func handle(gc githubClient, log *logrus.Entry, cp commentPruner, e *github.GenericCommentEvent) error {
org := e.Repo.Owner.Login
repo := e.Repo.Name
commentAuthor := e.User.Login
// Determine if the issue has the help and the good-first-issue label
labels, err := gc.GetIssueLabels(org, repo, e.Number)
if err != nil {
log.WithError(err).Errorf("Failed to get issue labels.")
}
hasHelp := github.HasLabel(helpLabel, labels)
hasGoodFirstIssue := github.HasLabel(goodFirstIssueLabel, labels)
// If PR has help label and we're asking for it to be removed, remove label
if hasHelp && helpRemoveRe.MatchString(e.Body) {
if err := gc.RemoveLabel(org, repo, e.Number, helpLabel); err != nil {
log.WithError(err).Errorf("Github failed to remove the following label: %s", helpLabel)
}
botName, err := gc.BotName()
if err != nil {
log.WithError(err).Errorf("Failed to get bot name.")
}
cp.PruneComments(shouldPrune(log, botName, helpMsgPruneMatch))
// if it has the good-first-issue label, remove it too
if hasGoodFirstIssue {
if err := gc.RemoveLabel(org, repo, e.Number, goodFirstIssueLabel); err != nil {
log.WithError(err).Errorf("Github failed to remove the following label: %s", goodFirstIssueLabel)
}
cp.PruneComments(shouldPrune(log, botName, goodFirstIssueMsgPruneMatch))
}
return nil
}
// If PR does not have the good-first-issue label and we are asking for it to be added,
// add both the good-first-issue and help labels
if !hasGoodFirstIssue && helpGoodFirstIssueRe.MatchString(e.Body) {
if err := gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(e.Body, e.IssueHTMLURL, commentAuthor, goodFirstIssueMsg)); err != nil {
log.WithError(err).Errorf("Failed to create comment \"%s\".", goodFirstIssueMsg)
}
if err := gc.AddLabel(org, repo, e.Number, goodFirstIssueLabel); err != nil {
log.WithError(err).Errorf("Github failed to add the following label: %s", goodFirstIssueLabel)
}
if !hasHelp {
if err := gc.AddLabel(org, repo, e.Number, helpLabel); err != nil {
log.WithError(err).Errorf("Github failed to add the following label: %s", helpLabel)
}
}
return nil
}
// If PR does not have the help label and we're asking it to be added,
// add the label
if !hasHelp && helpRe.MatchString(e.Body) {
if err := gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(e.Body, e.IssueHTMLURL, commentAuthor, helpMsg)); err != nil {
log.WithError(err).Errorf("Failed to create comment \"%s\".", helpMsg)
}
if err := gc.AddLabel(org, repo, e.Number, helpLabel); err != nil {
log.WithError(err).Errorf("Github failed to add the following label: %s", helpLabel)
}
return nil
}
// If PR has good-first-issue label and we are asking for it to be removed,
// remove just the good-first-issue label
if hasGoodFirstIssue && helpGoodFirstIssueRemoveRe.MatchString(e.Body) {
if err := gc.RemoveLabel(org, repo, e.Number, goodFirstIssueLabel); err != nil {
log.WithError(err).Errorf("Github failed to remove the following label: %s", goodFirstIssueLabel)
}
botName, err := gc.BotName()
if err != nil {
log.WithError(err).Errorf("Failed to get bot name.")
}
cp.PruneComments(shouldPrune(log, botName, goodFirstIssueMsgPruneMatch))
return nil
}
return nil
}
// shouldPrune finds comments left by this plugin.
func shouldPrune(log *logrus.Entry, botName, msgPruneMatch string) func(github.IssueComment) bool {
return func(comment github.IssueComment) bool {
if comment.User.Login != botName {
return false
}
return strings.Contains(comment.Body, msgPruneMatch)
}
}