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