github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/lifecycle/lifecycle.go (about)

     1  /*
     2  Copyright 2017 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 lifecycle
    18  
    19  import (
    20  	"regexp"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"k8s.io/test-infra/prow/github"
    25  	"k8s.io/test-infra/prow/labels"
    26  	"k8s.io/test-infra/prow/pluginhelp"
    27  	"k8s.io/test-infra/prow/plugins"
    28  )
    29  
    30  var (
    31  	lifecycleLabels = []string{labels.LifecycleActive, labels.LifecycleFrozen, labels.LifecycleStale, labels.LifecycleRotten}
    32  	lifecycleRe     = regexp.MustCompile(`(?mi)^/(remove-)?lifecycle (active|frozen|stale|rotten)\s*$`)
    33  )
    34  
    35  func init() {
    36  	plugins.RegisterGenericCommentHandler("lifecycle", lifecycleHandleGenericComment, help)
    37  }
    38  
    39  func help(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) {
    40  	pluginHelp := &pluginhelp.PluginHelp{
    41  		Description: "Close, reopen, flag and/or unflag an issue or PR as frozen/stale/rotten",
    42  	}
    43  	pluginHelp.AddCommand(pluginhelp.Command{
    44  		Usage:       "/close",
    45  		Description: "Closes an issue or PR.",
    46  		Featured:    false,
    47  		WhoCanUse:   "Authors and collaborators on the repository can trigger this command.",
    48  		Examples:    []string{"/close"},
    49  	})
    50  	pluginHelp.AddCommand(pluginhelp.Command{
    51  		Usage:       "/reopen",
    52  		Description: "Reopens an issue or PR",
    53  		Featured:    false,
    54  		WhoCanUse:   "Authors and collaborators on the repository can trigger this command.",
    55  		Examples:    []string{"/reopen"},
    56  	})
    57  	pluginHelp.AddCommand(pluginhelp.Command{
    58  		Usage:       "/[remove-]lifecycle <frozen|stale|rotten>",
    59  		Description: "Flags an issue or PR as frozen/stale/rotten",
    60  		Featured:    false,
    61  		WhoCanUse:   "Anyone can trigger this command.",
    62  		Examples:    []string{"/lifecycle frozen", "/remove-lifecycle stale"},
    63  	})
    64  	return pluginHelp, nil
    65  }
    66  
    67  type lifecycleClient interface {
    68  	AddLabel(owner, repo string, number int, label string) error
    69  	RemoveLabel(owner, repo string, number int, label string) error
    70  	GetIssueLabels(org, repo string, number int) ([]github.Label, error)
    71  }
    72  
    73  func lifecycleHandleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
    74  	gc := pc.GitHubClient
    75  	log := pc.Logger
    76  	if err := handleReopen(gc, log, &e); err != nil {
    77  		return err
    78  	}
    79  	if err := handleClose(gc, log, &e); err != nil {
    80  		return err
    81  	}
    82  	return handle(gc, log, &e)
    83  }
    84  
    85  func handle(gc lifecycleClient, log *logrus.Entry, e *github.GenericCommentEvent) error {
    86  	// Only consider new comments.
    87  	if e.Action != github.GenericCommentActionCreated {
    88  		return nil
    89  	}
    90  
    91  	for _, mat := range lifecycleRe.FindAllStringSubmatch(e.Body, -1) {
    92  		if err := handleOne(gc, log, e, mat); err != nil {
    93  			return err
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  func handleOne(gc lifecycleClient, log *logrus.Entry, e *github.GenericCommentEvent, mat []string) error {
   100  	org := e.Repo.Owner.Login
   101  	repo := e.Repo.Name
   102  	number := e.Number
   103  
   104  	remove := mat[1] != ""
   105  	cmd := mat[2]
   106  	lbl := "lifecycle/" + cmd
   107  
   108  	// Let's start simple and allow anyone to add/remove frozen, stale, rotten labels.
   109  	// Adjust if we find evidence of the community abusing these labels.
   110  	labels, err := gc.GetIssueLabels(org, repo, number)
   111  	if err != nil {
   112  		log.WithError(err).Errorf("Failed to get labels.")
   113  	}
   114  
   115  	// If the label exists and we asked for it to be removed, remove it.
   116  	if github.HasLabel(lbl, labels) && remove {
   117  		return gc.RemoveLabel(org, repo, number, lbl)
   118  	}
   119  
   120  	// If the label does not exist and we asked for it to be added,
   121  	// remove other existing lifecycle labels and add it.
   122  	if !github.HasLabel(lbl, labels) && !remove {
   123  		for _, label := range lifecycleLabels {
   124  			if label != lbl && github.HasLabel(label, labels) {
   125  				if err := gc.RemoveLabel(org, repo, number, label); err != nil {
   126  					log.WithError(err).Errorf("Github failed to remove the following label: %s", label)
   127  				}
   128  			}
   129  		}
   130  
   131  		if err := gc.AddLabel(org, repo, number, lbl); err != nil {
   132  			log.WithError(err).Errorf("Github failed to add the following label: %s", lbl)
   133  		}
   134  	}
   135  
   136  	return nil
   137  }