github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/comment/finder.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 comment 18 19 import ( 20 "time" 21 ) 22 23 // FilteredComments is a list of comments 24 type FilteredComments []*Comment 25 26 // GetLast returns the last comment in a series of comments 27 func (f FilteredComments) GetLast() *Comment { 28 if f.Empty() { 29 return nil 30 } 31 return f[len(f)-1] 32 } 33 34 // Empty Checks to see if the list of comments is empty 35 func (f FilteredComments) Empty() bool { 36 return len(f) == 0 37 } 38 39 // FilterComments will return the list of matching comments 40 func FilterComments(comments []*Comment, matcher Matcher) FilteredComments { 41 matches := FilteredComments{} 42 43 for _, comment := range comments { 44 if matcher.Match(comment) { 45 matches = append(matches, comment) 46 } 47 } 48 49 return matches 50 } 51 52 // LastComment returns the creation date of the last comment that matches. Or deflt if there is no such comment. 53 func LastComment(comments []*Comment, matcher Matcher, deflt *time.Time) *time.Time { 54 matches := FilterComments(comments, matcher) 55 if matches.Empty() { 56 return deflt 57 } 58 return matches.GetLast().CreatedAt 59 }