github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/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  	"sigs.k8s.io/prow/pkg/config"
    26  	"sigs.k8s.io/prow/pkg/github"
    27  	"sigs.k8s.io/prow/pkg/pluginhelp"
    28  	"sigs.k8s.io/prow/pkg/plugins"
    29  )
    30  
    31  const (
    32  	// PluginName defines this plugin's registered name.
    33  	PluginName = "owners-label"
    34  )
    35  
    36  func init() {
    37  	plugins.RegisterPullRequestHandler(PluginName, handlePullRequest, helpProvider)
    38  }
    39  
    40  func helpProvider(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhelp.PluginHelp, error) {
    41  	return &pluginhelp.PluginHelp{
    42  			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.",
    43  		},
    44  		nil
    45  }
    46  
    47  type ownersClient interface {
    48  	FindLabelsForFile(path string) sets.Set[string]
    49  }
    50  
    51  type githubClient interface {
    52  	AddLabel(org, repo string, number int, label string) error
    53  	GetIssueLabels(org, repo string, number int) ([]github.Label, error)
    54  	GetRepoLabels(owner, repo string) ([]github.Label, error)
    55  	GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
    56  }
    57  
    58  func handlePullRequest(pc plugins.Agent, pre github.PullRequestEvent) error {
    59  	if pre.Action != github.PullRequestActionOpened && pre.Action != github.PullRequestActionReopened && pre.Action != github.PullRequestActionSynchronize {
    60  		return nil
    61  	}
    62  
    63  	oc, err := pc.OwnersClient.LoadRepoOwners(pre.Repo.Owner.Login, pre.Repo.Name, pre.PullRequest.Base.Ref)
    64  	if err != nil {
    65  		return fmt.Errorf("error loading RepoOwners: %w", err)
    66  	}
    67  
    68  	return handle(pc.GitHubClient, oc, pc.Logger, &pre)
    69  }
    70  
    71  func handle(ghc githubClient, oc ownersClient, log *logrus.Entry, pre *github.PullRequestEvent) error {
    72  	org := pre.Repo.Owner.Login
    73  	repo := pre.Repo.Name
    74  	number := pre.Number
    75  
    76  	// First see if there are any labels requested based on the files changed.
    77  	changes, err := ghc.GetPullRequestChanges(org, repo, number)
    78  	if err != nil {
    79  		return fmt.Errorf("error getting PR changes: %w", err)
    80  	}
    81  	neededLabels := sets.New[string]()
    82  	for _, change := range changes {
    83  		neededLabels.Insert(sets.List(oc.FindLabelsForFile(change.Filename))...)
    84  	}
    85  	if neededLabels.Len() == 0 {
    86  		// No labels requested for the given files. Return now to save API tokens.
    87  		return nil
    88  	}
    89  
    90  	repoLabels, err := ghc.GetRepoLabels(org, repo)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	issuelabels, err := ghc.GetIssueLabels(org, repo, number)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	RepoLabelsExisting := sets.New[string]()
   100  	for _, label := range repoLabels {
   101  		RepoLabelsExisting.Insert(label.Name)
   102  	}
   103  	currentLabels := sets.New[string]()
   104  	for _, label := range issuelabels {
   105  		currentLabels.Insert(label.Name)
   106  	}
   107  
   108  	nonexistent := sets.New[string]()
   109  	for _, labelToAdd := range sets.List(neededLabels.Difference(currentLabels)) {
   110  		if !RepoLabelsExisting.Has(labelToAdd) {
   111  			nonexistent.Insert(labelToAdd)
   112  			continue
   113  		}
   114  		if err := ghc.AddLabel(org, repo, number, labelToAdd); err != nil {
   115  			log.WithError(err).Errorf("GitHub failed to add the following label: %s", labelToAdd)
   116  		}
   117  	}
   118  
   119  	if nonexistent.Len() > 0 {
   120  		log.Warnf("Unable to add nonexistent labels: %q", sets.List(nonexistent))
   121  	}
   122  	return nil
   123  }