github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/hold/hold.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 hold contains a plugin which will allow users to label their 18 // own pull requests as not ready or ready for merge. The submit queue 19 // will honor the label to ensure pull requests do not merge when it is 20 // applied. 21 package hold 22 23 import ( 24 "fmt" 25 "regexp" 26 27 "github.com/sirupsen/logrus" 28 29 "k8s.io/test-infra/prow/github" 30 "k8s.io/test-infra/prow/plugins" 31 ) 32 33 const pluginName = "hold" 34 35 var ( 36 label = "do-not-merge/hold" 37 labelRe = regexp.MustCompile(`(?mi)^/hold\s*$`) 38 labelCancelRe = regexp.MustCompile(`(?mi)^/hold cancel\s*$`) 39 ) 40 41 type hasLabelFunc func(e *github.GenericCommentEvent) (bool, error) 42 43 func init() { 44 plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment) 45 } 46 47 type githubClient interface { 48 AddLabel(owner, repo string, number int, label string) error 49 RemoveLabel(owner, repo string, number int, label string) error 50 GetIssueLabels(org, repo string, number int) ([]github.Label, error) 51 } 52 53 func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error { 54 hasLabel := func(e *github.GenericCommentEvent) (bool, error) { 55 return hasLabel(pc.GitHubClient, e.Repo.Owner.Login, e.Repo.Name, e.Number, label) 56 } 57 return handle(pc.GitHubClient, pc.Logger, &e, hasLabel) 58 } 59 60 // handle drives the pull request to the desired state. If any user adds 61 // a /hold directive, we want to add a label if one does not already exist. 62 // If they add /hold cancel, we want to remove the label if it exists. 63 func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, f hasLabelFunc) error { 64 if e.Action != github.GenericCommentActionCreated { 65 return nil 66 } 67 needsLabel := false 68 if labelRe.MatchString(e.Body) { 69 needsLabel = true 70 } else if labelCancelRe.MatchString(e.Body) { 71 needsLabel = false 72 } else { 73 return nil 74 } 75 76 hasLabel, err := f(e) 77 if err != nil { 78 return err 79 } 80 81 org := e.Repo.Owner.Login 82 repo := e.Repo.Name 83 84 if hasLabel && !needsLabel { 85 log.Info("Removing %q label for %s/%s#%d", label, org, repo, e.Number) 86 return gc.RemoveLabel(org, repo, e.Number, label) 87 } else if !hasLabel && needsLabel { 88 log.Info("Adding %q label for %s/%s#%d", label, org, repo, e.Number) 89 return gc.AddLabel(org, repo, e.Number, label) 90 } 91 return nil 92 } 93 94 // hasLabel checks if a label is applied to a pr. 95 func hasLabel(c githubClient, org, repo string, num int, label string) (bool, error) { 96 labels, err := c.GetIssueLabels(org, repo, num) 97 if err != nil { 98 return false, fmt.Errorf("failed to get the labels on %s/%s#%d: %v", org, repo, num, err) 99 } 100 for _, candidate := range labels { 101 if candidate.Name == label { 102 return true, nil 103 } 104 } 105 return false, nil 106 }