github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/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/labels"
    30  	"k8s.io/test-infra/prow/pluginhelp"
    31  	"k8s.io/test-infra/prow/plugins"
    32  )
    33  
    34  const pluginName = "sigmention"
    35  
    36  var (
    37  	chatBack = "Reiterating the mentions to trigger a notification: \n%v\n"
    38  
    39  	kindMap = map[string]string{
    40  		"bugs":             labels.Bug,
    41  		"feature-requests": "kind/feature",
    42  		"api-reviews":      "kind/api-change",
    43  		"proposals":        "kind/design",
    44  	}
    45  )
    46  
    47  type githubClient interface {
    48  	CreateComment(owner, repo string, number int, comment string) error
    49  	IsMember(org, user string) (bool, error)
    50  	AddLabel(owner, repo string, number int, label string) error
    51  	RemoveLabel(owner, repo string, number int, label string) error
    52  	GetRepoLabels(owner, repo string) ([]github.Label, error)
    53  	BotName() (string, error)
    54  	GetIssueLabels(org, repo string, number int) ([]github.Label, error)
    55  }
    56  
    57  func init() {
    58  	plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment, helpProvider)
    59  }
    60  
    61  func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) {
    62  	// Only the Description field is specified because this plugin is not triggered with commands and is not configurable.
    63  	return &pluginhelp.PluginHelp{
    64  			Description: `The sigmention plugin responds to SIG (Special Interest Group) Github team mentions like '@kubernetes/sig-testing-bugs'. The plugin responds in two ways:
    65  <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>
    66  <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>`,
    67  			Config: map[string]string{
    68  				"": fmt.Sprintf("Labels added by the plugin are triggered by mentions of Github teams matching the following regexp:\n%s", config.SigMention.Regexp),
    69  			},
    70  		},
    71  		nil
    72  }
    73  
    74  func handleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
    75  	return handle(pc.GitHubClient, pc.Logger, &e, pc.PluginConfig.SigMention.Re)
    76  }
    77  
    78  func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, re *regexp.Regexp) error {
    79  	// Ignore bot comments and comments that aren't new.
    80  	botName, err := gc.BotName()
    81  	if err != nil {
    82  		return err
    83  	}
    84  	if e.User.Login == botName {
    85  		return nil
    86  	}
    87  	if e.Action != github.GenericCommentActionCreated {
    88  		return nil
    89  	}
    90  
    91  	sigMatches := re.FindAllStringSubmatch(e.Body, -1)
    92  	if len(sigMatches) == 0 {
    93  		return nil
    94  	}
    95  
    96  	org := e.Repo.Owner.Login
    97  	repo := e.Repo.Name
    98  
    99  	labels, err := gc.GetIssueLabels(org, repo, e.Number)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	repoLabels, err := gc.GetRepoLabels(org, repo)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	RepoLabelsExisting := map[string]string{}
   108  	for _, l := range repoLabels {
   109  		RepoLabelsExisting[strings.ToLower(l.Name)] = l.Name
   110  	}
   111  
   112  	var nonexistent, toRepeat []string
   113  	for _, sigMatch := range sigMatches {
   114  		sigLabel := strings.ToLower("sig" + "/" + sigMatch[1])
   115  		sigLabel, ok := RepoLabelsExisting[sigLabel]
   116  		if !ok {
   117  			nonexistent = append(nonexistent, "sig/"+sigMatch[1])
   118  			continue
   119  		}
   120  		if !github.HasLabel(sigLabel, labels) {
   121  			if err := gc.AddLabel(org, repo, e.Number, sigLabel); err != nil {
   122  				log.WithError(err).Errorf("Github failed to add the following label: %s", sigLabel)
   123  			}
   124  		}
   125  
   126  		if len(sigMatch) > 2 {
   127  			if kindLabel, ok := kindMap[sigMatch[2]]; ok && !github.HasLabel(kindLabel, labels) {
   128  				if err := gc.AddLabel(org, repo, e.Number, kindLabel); err != nil {
   129  					log.WithError(err).Errorf("Github failed to add the following label: %s", kindLabel)
   130  				}
   131  			}
   132  		}
   133  
   134  		toRepeat = append(toRepeat, sigMatch[0])
   135  	}
   136  	//TODO(grodrigues3): Once labels are standardized, make this reply with a comment.
   137  	if len(nonexistent) > 0 {
   138  		log.Infof("Nonexistent labels: %v", nonexistent)
   139  	}
   140  
   141  	isMember, err := gc.IsMember(org, e.User.Login)
   142  	if err != nil {
   143  		log.WithError(err).Errorf("Error from IsMember(%q of org %q).", e.User.Login, org)
   144  	}
   145  	if isMember || len(toRepeat) == 0 {
   146  		return nil
   147  	}
   148  
   149  	msg := fmt.Sprintf(chatBack, strings.Join(toRepeat, ", "))
   150  	return gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, msg))
   151  }