github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/stage/stage.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 stage defines a Prow plugin that defines the stage of
    18  // the issue in the features process. Eg: alpha, beta, stable.
    19  package stage
    20  
    21  import (
    22  	"regexp"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"sigs.k8s.io/prow/pkg/config"
    27  	"sigs.k8s.io/prow/pkg/github"
    28  	"sigs.k8s.io/prow/pkg/pluginhelp"
    29  	"sigs.k8s.io/prow/pkg/plugins"
    30  )
    31  
    32  var (
    33  	stageAlpha  = "stage/alpha"
    34  	stageBeta   = "stage/beta"
    35  	stageStable = "stage/stable"
    36  	stageLabels = []string{stageAlpha, stageBeta, stageStable}
    37  	stageRe     = regexp.MustCompile(`(?mi)^/(remove-)?stage (alpha|beta|stable)\s*$`)
    38  )
    39  
    40  func init() {
    41  	plugins.RegisterGenericCommentHandler("stage", stageHandleGenericComment, help)
    42  }
    43  
    44  func help(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhelp.PluginHelp, error) {
    45  	// The Config field is omitted because this plugin is not configurable.
    46  	pluginHelp := &pluginhelp.PluginHelp{
    47  		Description: "Label the stage of an issue as alpha/beta/stable",
    48  	}
    49  	pluginHelp.AddCommand(pluginhelp.Command{
    50  		Usage:       "/[remove-]stage <alpha|beta|stable>",
    51  		Description: "Labels the stage of an issue as alpha/beta/stable",
    52  		Featured:    false,
    53  		WhoCanUse:   "Anyone can trigger this command.",
    54  		Examples:    []string{"/stage alpha", "/remove-stage alpha"},
    55  	})
    56  	return pluginHelp, nil
    57  }
    58  
    59  type stageClient interface {
    60  	AddLabel(owner, repo string, number int, label string) error
    61  	RemoveLabel(owner, repo string, number int, label string) error
    62  	GetIssueLabels(org, repo string, number int) ([]github.Label, error)
    63  }
    64  
    65  func stageHandleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
    66  	return handle(pc.GitHubClient, pc.Logger, &e)
    67  }
    68  
    69  func handle(gc stageClient, log *logrus.Entry, e *github.GenericCommentEvent) error {
    70  	// Only consider new comments.
    71  	if e.Action != github.GenericCommentActionCreated {
    72  		return nil
    73  	}
    74  
    75  	for _, mat := range stageRe.FindAllStringSubmatch(e.Body, -1) {
    76  		if err := handleOne(gc, log, e, mat); err != nil {
    77  			return err
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func handleOne(gc stageClient, log *logrus.Entry, e *github.GenericCommentEvent, mat []string) error {
    84  	org := e.Repo.Owner.Login
    85  	repo := e.Repo.Name
    86  	number := e.Number
    87  
    88  	remove := mat[1] != ""
    89  	cmd := mat[2]
    90  	lbl := "stage/" + cmd
    91  
    92  	// Let's start simple and allow anyone to add/remove alpha, beta and stable labels.
    93  	// Adjust if we find evidence of the community abusing these labels.
    94  	labels, err := gc.GetIssueLabels(org, repo, number)
    95  	if err != nil {
    96  		log.WithError(err).Errorf("Failed to get labels.")
    97  	}
    98  
    99  	// If the label exists and we asked for it to be removed, remove it.
   100  	if github.HasLabel(lbl, labels) && remove {
   101  		return gc.RemoveLabel(org, repo, number, lbl)
   102  	}
   103  
   104  	// If the label does not exist and we asked for it to be added,
   105  	// remove other existing stage labels and add it.
   106  	if !github.HasLabel(lbl, labels) && !remove {
   107  		for _, label := range stageLabels {
   108  			if label != lbl && github.HasLabel(label, labels) {
   109  				if err := gc.RemoveLabel(org, repo, number, label); err != nil {
   110  					log.WithError(err).Errorf("GitHub failed to remove the following label: %s", label)
   111  				}
   112  			}
   113  		}
   114  
   115  		if err := gc.AddLabel(org, repo, number, lbl); err != nil {
   116  			log.WithError(err).Errorf("GitHub failed to add the following label: %s", lbl)
   117  		}
   118  	}
   119  
   120  	return nil
   121  }