sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/branchcleaner/branchcleaner.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 branchcleaner
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	prowconfig "sigs.k8s.io/prow/pkg/config"
    25  	"sigs.k8s.io/prow/pkg/github"
    26  	"sigs.k8s.io/prow/pkg/pluginhelp"
    27  	"sigs.k8s.io/prow/pkg/plugins"
    28  )
    29  
    30  const (
    31  	pluginName = "branchcleaner"
    32  )
    33  
    34  var (
    35  	preservedBranchesMsg = "The preserved branches for repo %s is %v"
    36  )
    37  
    38  func init() {
    39  	plugins.RegisterPullRequestHandler(pluginName, handlePullRequest, helpProvider)
    40  }
    41  
    42  func helpProvider(config *plugins.Configuration, enabledRepos []prowconfig.OrgRepo) (*pluginhelp.PluginHelp, error) {
    43  	msgForPreservedBranches := func(repo string, branches []string) string {
    44  		return fmt.Sprintf(preservedBranchesMsg, repo, branches)
    45  	}
    46  	yamlSnippet, err := plugins.CommentMap.GenYaml(&plugins.Configuration{
    47  		BranchCleaner: plugins.BranchCleaner{
    48  			PreservedBranches: map[string][]string{
    49  				"kubernetes/kubernetes": {"master"},
    50  				"kubernetes-sigs":       {"master"},
    51  			},
    52  		},
    53  	})
    54  	if err != nil {
    55  		logrus.WithError(err).Warnf("cannot generate comments for %s plugin", pluginName)
    56  	}
    57  	return &pluginhelp.PluginHelp{
    58  		Description: "The branchcleaner plugin automatically deletes source branches for merged PRs between two branches on the same repository. This is helpful to keep repos that don't allow forking clean.",
    59  		Config: func(repos []prowconfig.OrgRepo) map[string]string {
    60  			configMap := make(map[string]string)
    61  			for _, repo := range repos {
    62  				preservedBranches, exists := config.BranchCleaner.PreservedBranches[repo.String()]
    63  				if exists {
    64  					configMap[repo.String()] = msgForPreservedBranches(repo.String(), preservedBranches)
    65  				}
    66  			}
    67  			return configMap
    68  		}(enabledRepos),
    69  		Snippet: yamlSnippet,
    70  	}, err
    71  }
    72  
    73  func handlePullRequest(pc plugins.Agent, pre github.PullRequestEvent) error {
    74  	return handle(pc.GitHubClient, pc.Logger, pc.PluginConfig.BranchCleaner, pre)
    75  }
    76  
    77  type githubClient interface {
    78  	DeleteRef(owner, repo, ref string) error
    79  }
    80  
    81  func handle(gc githubClient, log *logrus.Entry, config plugins.BranchCleaner, pre github.PullRequestEvent) error {
    82  	// Only consider closed PRs that got merged
    83  	if pre.Action != github.PullRequestActionClosed || !pre.PullRequest.Merged {
    84  		return nil
    85  	}
    86  
    87  	pr := pre.PullRequest
    88  
    89  	// Only consider PRs from the same repo
    90  	if pr.Base.Repo.FullName != pr.Head.Repo.FullName {
    91  		return nil
    92  	}
    93  
    94  	// skip preserved branches
    95  	if config.IsPreservedBranch(pr.Base.Repo.Owner.Login, pr.Base.Repo.Name, pr.Head.Ref) {
    96  		return nil
    97  	}
    98  
    99  	if err := gc.DeleteRef(pr.Base.Repo.Owner.Login, pr.Base.Repo.Name, fmt.Sprintf("heads/%s", pr.Head.Ref)); err != nil {
   100  		return fmt.Errorf("failed to delete branch %s on repo %s/%s after Pull Request #%d got merged: %w",
   101  			pr.Head.Ref, pr.Base.Repo.Owner.Login, pr.Base.Repo.Name, pre.PullRequest.Number, err)
   102  	}
   103  
   104  	return nil
   105  }