github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/comment-deleter.go (about) 1 /* 2 Copyright 2016 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 mungers 18 19 import ( 20 "k8s.io/apimachinery/pkg/util/sets" 21 "k8s.io/test-infra/mungegithub/features" 22 "k8s.io/test-infra/mungegithub/github" 23 "k8s.io/test-infra/mungegithub/options" 24 25 "github.com/golang/glog" 26 githubapi "github.com/google/go-github/github" 27 ) 28 29 const ( 30 commentDeleterName = "comment-deleter" 31 ) 32 33 var ( 34 _ = glog.Infof 35 deleters = []StaleIssueComment{} 36 ) 37 38 // CommentDeleter looks for comments which are no longer useful 39 // and deletes them 40 type CommentDeleter struct{} 41 42 func init() { 43 RegisterMungerOrDie(CommentDeleter{}) 44 } 45 46 // StaleIssueComment is an interface for a munger which writes issue comments which might go stale 47 // and which should be cleaned up. 48 type StaleIssueComment interface { 49 StaleIssueComments(*github.MungeObject, []*githubapi.IssueComment) []*githubapi.IssueComment 50 } 51 52 // RegisterStaleIssueComments is the method for a munger to register that it creates issue comments 53 // which might go stale and need to be cleaned up. 54 func RegisterStaleIssueComments(s StaleIssueComment) { 55 deleters = append(deleters, s) 56 } 57 58 // Name is the name usable in --pr-mungers 59 func (CommentDeleter) Name() string { return commentDeleterName } 60 61 // RequiredFeatures is a slice of 'features' that must be provided 62 func (CommentDeleter) RequiredFeatures() []string { return []string{} } 63 64 // Initialize will initialize the munger 65 func (CommentDeleter) Initialize(config *github.Config, features *features.Features) error { return nil } 66 67 // EachLoop is called at the start of every munge loop 68 func (CommentDeleter) EachLoop() error { return nil } 69 70 // RegisterOptions registers options for this munger; returns any that require a restart when changed. 71 func (CommentDeleter) RegisterOptions(opts *options.Options) sets.String { return nil } 72 73 func validComment(comment *githubapi.IssueComment) bool { 74 if comment.User == nil || comment.User.Login == nil { 75 return false 76 } 77 if comment.CreatedAt == nil { 78 return false 79 } 80 if comment.Body == nil { 81 return false 82 } 83 return true 84 } 85 86 // Munge is the workhorse the will actually make updates to the Issue. 87 func (CommentDeleter) Munge(obj *github.MungeObject) { 88 if obj.Issue == nil || obj.Issue.State == nil || *obj.Issue.State == "closed" { 89 return 90 } 91 92 comments, ok := obj.ListComments() 93 if !ok { 94 return 95 } 96 97 validComments := []*githubapi.IssueComment{} 98 for i := range comments { 99 comment := comments[i] 100 if !validComment(comment) { 101 continue 102 } 103 validComments = append(validComments, comment) 104 } 105 for _, d := range deleters { 106 stale := d.StaleIssueComments(obj, validComments) 107 for _, comment := range stale { 108 obj.DeleteComment(comment) 109 } 110 } 111 } 112 113 func jenkinsBotComment(comment *githubapi.IssueComment) bool { 114 return *comment.User.Login == jenkinsBotName 115 } 116 117 // Checks each comment in `comments` and returns a slice of comments for which the `stale` function was true 118 func forEachCommentTest(obj *github.MungeObject, comments []*githubapi.IssueComment, stale func(*github.MungeObject, *githubapi.IssueComment) bool) []*githubapi.IssueComment { 119 out := []*githubapi.IssueComment{} 120 121 for _, comment := range comments { 122 if stale(obj, comment) { 123 out = append(out, comment) 124 } 125 } 126 return out 127 }