github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/reopen/reopen.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 reopen 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/plugins" 26 ) 27 28 const pluginName = "reopen" 29 30 var reopenRe = regexp.MustCompile(`(?mi)^/reopen\s*$`) 31 32 func init() { 33 plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment) 34 } 35 36 type githubClient interface { 37 CreateComment(owner, repo string, number int, comment string) error 38 ReopenIssue(owner, repo string, number int) error 39 ReopenPR(owner, repo string, number int) error 40 } 41 42 func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error { 43 return handle(pc.GitHubClient, pc.Logger, &e) 44 } 45 46 func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent) error { 47 // Only consider closed issues and new comments. 48 if e.IssueState != "closed" || e.Action != github.GenericCommentActionCreated { 49 return nil 50 } 51 52 if !reopenRe.MatchString(e.Body) { 53 return nil 54 } 55 56 org := e.Repo.Owner.Login 57 repo := e.Repo.Name 58 number := e.Number 59 commentAuthor := e.User.Login 60 61 // Allow assignees and authors to re-open issues. 62 isAssignee := false 63 for _, assignee := range e.Assignees { 64 if commentAuthor == assignee.Login { 65 isAssignee = true 66 break 67 } 68 } 69 if e.IssueAuthor.Login != commentAuthor && !isAssignee { 70 resp := "you can't re-open an issue/PR unless you authored it or you are assigned to it." 71 log.Infof("Commenting \"%s\".", resp) 72 return gc.CreateComment(org, repo, number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp)) 73 } 74 75 if e.IsPR { 76 log.Info("Re-opening PR.") 77 return gc.ReopenPR(org, repo, number) 78 } 79 80 log.Info("Re-opening issue.") 81 return gc.ReopenIssue(org, repo, number) 82 }