github.com/abayer/test-infra@v0.0.5/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 = "owners-label" 32 ) 33 34 func init() { 35 plugins.RegisterPullRequestHandler(pluginName, handlePullRequest, helpProvider) 36 } 37 38 func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) { 39 return &pluginhelp.PluginHelp{ 40 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.", 41 }, 42 nil 43 } 44 45 type ownersClient interface { 46 FindLabelsForFile(path string) sets.String 47 } 48 49 type githubClient interface { 50 AddLabel(org, repo string, number int, label string) error 51 GetIssueLabels(org, repo string, number int) ([]github.Label, error) 52 GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error) 53 } 54 55 func handlePullRequest(pc plugins.PluginClient, pre github.PullRequestEvent) error { 56 if pre.Action != github.PullRequestActionOpened && pre.Action != github.PullRequestActionReopened && pre.Action != github.PullRequestActionSynchronize { 57 return nil 58 } 59 60 oc, err := pc.OwnersClient.LoadRepoOwners(pre.Repo.Owner.Login, pre.Repo.Name, pre.PullRequest.Base.Ref) 61 if err != nil { 62 return fmt.Errorf("error loading RepoOwners: %v", err) 63 } 64 65 return handle(pc.GitHubClient, oc, pc.Logger, &pre) 66 } 67 68 func handle(ghc githubClient, oc ownersClient, log *logrus.Entry, pre *github.PullRequestEvent) error { 69 changes, err := ghc.GetPullRequestChanges(pre.Repo.Owner.Login, pre.Repo.Name, pre.Number) 70 if err != nil { 71 return fmt.Errorf("error getting PR changes: %v", err) 72 } 73 labels, err := ghc.GetIssueLabels(pre.Repo.Owner.Login, pre.Repo.Name, pre.Number) 74 if err != nil { 75 return fmt.Errorf("error getting PR labels: %v", err) 76 } 77 78 currentLabels := sets.NewString() 79 for _, label := range labels { 80 currentLabels.Insert(label.Name) 81 } 82 neededLabels := sets.NewString() 83 for _, change := range changes { 84 neededLabels.Insert(oc.FindLabelsForFile(change.Filename).List()...) 85 } 86 87 for _, toAdd := range neededLabels.Difference(currentLabels).List() { 88 if err := ghc.AddLabel(pre.Repo.Owner.Login, pre.Repo.Name, pre.Number, toAdd); err != nil { 89 log.WithError(err).Errorf("Adding %q label.", toAdd) 90 } 91 } 92 return nil 93 }