github.com/abayer/test-infra@v0.0.5/prow/plugins/sigmention/sigmention.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package sigmention recognize SIG '@' mentions and adds 'sig/*' and 'kind/*' labels as appropriate. 18 // SIG mentions are also reitierated by the bot if the user who made the mention is not a member in 19 // order for the mention to trigger a notification for the github team. 20 package sigmention 21 22 import ( 23 "fmt" 24 "regexp" 25 "strings" 26 27 "github.com/sirupsen/logrus" 28 "k8s.io/test-infra/prow/github" 29 "k8s.io/test-infra/prow/pluginhelp" 30 "k8s.io/test-infra/prow/plugins" 31 ) 32 33 const pluginName = "sigmention" 34 35 var ( 36 chatBack = "Reiterating the mentions to trigger a notification: \n%v\n" 37 38 kindMap = map[string]string{ 39 "bugs": "kind/bug", 40 "feature-requests": "kind/feature", 41 "api-reviews": "kind/api-change", 42 "proposals": "kind/design", 43 } 44 ) 45 46 type githubClient interface { 47 CreateComment(owner, repo string, number int, comment string) error 48 IsMember(org, user string) (bool, error) 49 AddLabel(owner, repo string, number int, label string) error 50 RemoveLabel(owner, repo string, number int, label string) error 51 GetRepoLabels(owner, repo string) ([]github.Label, error) 52 BotName() (string, error) 53 GetIssueLabels(org, repo string, number int) ([]github.Label, error) 54 } 55 56 func init() { 57 plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment, helpProvider) 58 } 59 60 func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) { 61 // Only the Description field is specified because this plugin is not triggered with commands and is not configurable. 62 return &pluginhelp.PluginHelp{ 63 Description: `The sigmention plugin responds to SIG (Special Interest Group) Github team mentions like '@kubernetes/sig-testing-bugs'. The plugin responds in two ways: 64 <ol><li> The appropriate 'sig/*' and 'kind/*' labels are applied to the issue or pull request. In this case 'sig/testing' and 'kind/bug'.</li> 65 <li> If the user who mentioned the Github team is not a member of the organization that owns the repository the bot will create a comment that repeats the mention. This is necessary because non-member mentions do not trigger Github notifications.</li></ol>`, 66 Config: map[string]string{ 67 "": fmt.Sprintf("Labels added by the plugin are triggered by mentions of Github teams matching the following regexp:\n%s", config.SigMention.Regexp), 68 }, 69 }, 70 nil 71 } 72 73 func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error { 74 return handle(pc.GitHubClient, pc.Logger, &e, pc.PluginConfig.SigMention.Re) 75 } 76 77 func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, re *regexp.Regexp) error { 78 // Ignore bot comments and comments that aren't new. 79 botName, err := gc.BotName() 80 if err != nil { 81 return err 82 } 83 if e.User.Login == botName { 84 return nil 85 } 86 if e.Action != github.GenericCommentActionCreated { 87 return nil 88 } 89 90 sigMatches := re.FindAllStringSubmatch(e.Body, -1) 91 if len(sigMatches) == 0 { 92 return nil 93 } 94 95 org := e.Repo.Owner.Login 96 repo := e.Repo.Name 97 98 labels, err := gc.GetIssueLabels(org, repo, e.Number) 99 if err != nil { 100 return err 101 } 102 repoLabels, err := gc.GetRepoLabels(org, repo) 103 if err != nil { 104 return err 105 } 106 existingLabels := map[string]string{} 107 for _, l := range repoLabels { 108 existingLabels[strings.ToLower(l.Name)] = l.Name 109 } 110 111 var nonexistent, toRepeat []string 112 for _, sigMatch := range sigMatches { 113 sigLabel := strings.ToLower("sig" + "/" + sigMatch[1]) 114 sigLabel, ok := existingLabels[sigLabel] 115 if !ok { 116 nonexistent = append(nonexistent, "sig/"+sigMatch[1]) 117 continue 118 } 119 if !github.HasLabel(sigLabel, labels) { 120 if err := gc.AddLabel(org, repo, e.Number, sigLabel); err != nil { 121 log.WithError(err).Errorf("Github failed to add the following label: %s", sigLabel) 122 } 123 } 124 125 if len(sigMatch) > 2 { 126 if kindLabel, ok := kindMap[sigMatch[2]]; ok && !github.HasLabel(kindLabel, labels) { 127 if err := gc.AddLabel(org, repo, e.Number, kindLabel); err != nil { 128 log.WithError(err).Errorf("Github failed to add the following label: %s", kindLabel) 129 } 130 } 131 } 132 133 toRepeat = append(toRepeat, sigMatch[0]) 134 } 135 //TODO(grodrigues3): Once labels are standardized, make this reply with a comment. 136 if len(nonexistent) > 0 { 137 log.Infof("Nonexistent labels: %v", nonexistent) 138 } 139 140 isMember, err := gc.IsMember(org, e.User.Login) 141 if err != nil { 142 log.WithError(err).Errorf("Error from IsMember(%q of org %q).", e.User.Login, org) 143 } 144 if isMember || len(toRepeat) == 0 { 145 return nil 146 } 147 148 msg := fmt.Sprintf(chatBack, strings.Join(toRepeat, ", ")) 149 return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, msg)) 150 }