github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/owners-label/owners-label.go (about) 1 /* 2 Copyright 2018 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 ownerslabel 18 19 import ( 20 "fmt" 21 22 "github.com/sirupsen/logrus" 23 24 "k8s.io/apimachinery/pkg/util/sets" 25 "k8s.io/test-infra/prow/github" 26 "k8s.io/test-infra/prow/pluginhelp" 27 "k8s.io/test-infra/prow/plugins" 28 ) 29 30 const ( 31 // PluginName defines this plugin's registered name. 32 PluginName = "owners-label" 33 ) 34 35 func init() { 36 plugins.RegisterPullRequestHandler(PluginName, handlePullRequest, helpProvider) 37 } 38 39 func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) { 40 return &pluginhelp.PluginHelp{ 41 Description: "The owners-label plugin automatically adds labels to PRs based on the files they touch. Specifically, the 'labels' sections of OWNERS files are used to determine which labels apply to the changes.", 42 }, 43 nil 44 } 45 46 type ownersClient interface { 47 FindLabelsForFile(path string) sets.String 48 } 49 50 type githubClient interface { 51 AddLabel(org, repo string, number int, label string) error 52 GetIssueLabels(org, repo string, number int) ([]github.Label, error) 53 GetRepoLabels(owner, repo string) ([]github.Label, error) 54 GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error) 55 } 56 57 func handlePullRequest(pc plugins.Agent, pre github.PullRequestEvent) error { 58 if pre.Action != github.PullRequestActionOpened && pre.Action != github.PullRequestActionReopened && pre.Action != github.PullRequestActionSynchronize { 59 return nil 60 } 61 62 oc, err := pc.OwnersClient.LoadRepoOwners(pre.Repo.Owner.Login, pre.Repo.Name, pre.PullRequest.Base.Ref) 63 if err != nil { 64 return fmt.Errorf("error loading RepoOwners: %v", err) 65 } 66 67 return handle(pc.GitHubClient, oc, pc.Logger, &pre) 68 } 69 70 func handle(ghc githubClient, oc ownersClient, log *logrus.Entry, pre *github.PullRequestEvent) error { 71 org := pre.Repo.Owner.Login 72 repo := pre.Repo.Name 73 number := pre.Number 74 75 repoLabels, err := ghc.GetRepoLabels(org, repo) 76 if err != nil { 77 return err 78 } 79 issuelabels, err := ghc.GetIssueLabels(org, repo, number) 80 if err != nil { 81 return err 82 } 83 84 RepoLabelsExisting := sets.NewString() 85 for _, label := range repoLabels { 86 RepoLabelsExisting.Insert(label.Name) 87 } 88 changes, err := ghc.GetPullRequestChanges(org, repo, number) 89 if err != nil { 90 return fmt.Errorf("error getting PR changes: %v", err) 91 } 92 currentLabels := sets.NewString() 93 for _, label := range issuelabels { 94 currentLabels.Insert(label.Name) 95 } 96 neededLabels := sets.NewString() 97 for _, change := range changes { 98 neededLabels.Insert(oc.FindLabelsForFile(change.Filename).List()...) 99 } 100 101 nonexistent := sets.NewString() 102 103 for _, labelToAdd := range neededLabels.Difference(currentLabels).List() { 104 if !RepoLabelsExisting.Has(labelToAdd) { 105 nonexistent.Insert(labelToAdd) 106 continue 107 } 108 if err := ghc.AddLabel(org, repo, number, labelToAdd); err != nil { 109 log.WithError(err).Errorf("Github failed to add the following label: %s", labelToAdd) 110 } 111 } 112 113 if nonexistent.Len() > 0 { 114 log.Warnf("Unable to add nonexistent labels: %q", nonexistent.List()) 115 } 116 return nil 117 }