github.com/abayer/test-infra@v0.0.5/prow/plugins/lifecycle/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 lifecycle 18 19 import ( 20 "fmt" 21 "regexp" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/test-infra/prow/github" 26 "k8s.io/test-infra/prow/plugins" 27 ) 28 29 var reopenRe = regexp.MustCompile(`(?mi)^/reopen\s*$`) 30 31 type githubClient interface { 32 CreateComment(owner, repo string, number int, comment string) error 33 ReopenIssue(owner, repo string, number int) error 34 ReopenPR(owner, repo string, number int) error 35 } 36 37 func handleReopen(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent) error { 38 // Only consider closed issues and new comments. 39 if e.IssueState != "closed" || e.Action != github.GenericCommentActionCreated { 40 return nil 41 } 42 43 if !reopenRe.MatchString(e.Body) { 44 return nil 45 } 46 47 org := e.Repo.Owner.Login 48 repo := e.Repo.Name 49 number := e.Number 50 commentAuthor := e.User.Login 51 52 // Allow assignees and authors to re-open issues. 53 isAssignee := false 54 for _, assignee := range e.Assignees { 55 if commentAuthor == assignee.Login { 56 isAssignee = true 57 break 58 } 59 } 60 if e.IssueAuthor.Login != commentAuthor && !isAssignee { 61 resp := "you can't re-open an issue/PR unless you authored it or you are assigned to it." 62 log.Infof("Commenting \"%s\".", resp) 63 return gc.CreateComment(org, repo, number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp)) 64 } 65 66 if e.IsPR { 67 log.Infof("/reopen PR") 68 err := gc.ReopenPR(org, repo, number) 69 if err != nil { 70 if scbc, ok := err.(github.StateCannotBeChanged); ok { 71 resp := fmt.Sprintf("failed to re-open PR: %v", scbc) 72 return gc.CreateComment(org, repo, number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp)) 73 } 74 } 75 return err 76 } 77 78 log.Infof("/reopen issue") 79 err := gc.ReopenIssue(org, repo, number) 80 if err != nil { 81 if scbc, ok := err.(github.StateCannotBeChanged); ok { 82 resp := fmt.Sprintf("failed to re-open Issue: %v", scbc) 83 return gc.CreateComment(org, repo, number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp)) 84 } 85 } 86 return err 87 }