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