/
owners-label.go
123 lines (102 loc) · 3.74 KB
/
owners-label.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
/*
Copyright 2018 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 ownerslabel
import (
"fmt"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/plugins"
)
const (
// PluginName defines this plugin's registered name.
PluginName = "owners-label"
)
func init() {
plugins.RegisterPullRequestHandler(PluginName, handlePullRequest, helpProvider)
}
func helpProvider(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhelp.PluginHelp, error) {
return &pluginhelp.PluginHelp{
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.",
},
nil
}
type ownersClient interface {
FindLabelsForFile(path string) sets.Set[string]
}
type githubClient interface {
AddLabel(org, repo string, number int, label string) error
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
GetRepoLabels(owner, repo string) ([]github.Label, error)
GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
}
func handlePullRequest(pc plugins.Agent, pre github.PullRequestEvent) error {
if pre.Action != github.PullRequestActionOpened && pre.Action != github.PullRequestActionReopened && pre.Action != github.PullRequestActionSynchronize {
return nil
}
oc, err := pc.OwnersClient.LoadRepoOwners(pre.Repo.Owner.Login, pre.Repo.Name, pre.PullRequest.Base.Ref)
if err != nil {
return fmt.Errorf("error loading RepoOwners: %w", err)
}
return handle(pc.GitHubClient, oc, pc.Logger, &pre)
}
func handle(ghc githubClient, oc ownersClient, log *logrus.Entry, pre *github.PullRequestEvent) error {
org := pre.Repo.Owner.Login
repo := pre.Repo.Name
number := pre.Number
// First see if there are any labels requested based on the files changed.
changes, err := ghc.GetPullRequestChanges(org, repo, number)
if err != nil {
return fmt.Errorf("error getting PR changes: %w", err)
}
neededLabels := sets.New[string]()
for _, change := range changes {
neededLabels.Insert(sets.List(oc.FindLabelsForFile(change.Filename))...)
}
if neededLabels.Len() == 0 {
// No labels requested for the given files. Return now to save API tokens.
return nil
}
repoLabels, err := ghc.GetRepoLabels(org, repo)
if err != nil {
return err
}
issuelabels, err := ghc.GetIssueLabels(org, repo, number)
if err != nil {
return err
}
RepoLabelsExisting := sets.New[string]()
for _, label := range repoLabels {
RepoLabelsExisting.Insert(label.Name)
}
currentLabels := sets.New[string]()
for _, label := range issuelabels {
currentLabels.Insert(label.Name)
}
nonexistent := sets.New[string]()
for _, labelToAdd := range sets.List(neededLabels.Difference(currentLabels)) {
if !RepoLabelsExisting.Has(labelToAdd) {
nonexistent.Insert(labelToAdd)
continue
}
if err := ghc.AddLabel(org, repo, number, labelToAdd); err != nil {
log.WithError(err).Errorf("GitHub failed to add the following label: %s", labelToAdd)
}
}
if nonexistent.Len() > 0 {
log.Warnf("Unable to add nonexistent labels: %q", sets.List(nonexistent))
}
return nil
}