github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/event/event.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 "strings" 21 "time" 22 23 "github.com/google/go-github/github" 24 ) 25 26 // Matcher is an interface to match an event 27 type Matcher interface { 28 Match(event *github.IssueEvent) bool 29 } 30 31 // Actor searches for a specific actor 32 type Actor string 33 34 // Match if the event is from the specified actor 35 func (a Actor) Match(event *github.IssueEvent) bool { 36 if event == nil || event.Actor == nil || event.Actor.Login == nil { 37 return false 38 } 39 return strings.ToLower(*event.Actor.Login) == strings.ToLower(string(a)) 40 } 41 42 // AddLabel searches for "labeled" event. 43 type AddLabel struct{} 44 45 // Match if the event is of type "labeled" 46 func (a AddLabel) Match(event *github.IssueEvent) bool { 47 if event == nil || event.Event == nil { 48 return false 49 } 50 return *event.Event == "labeled" 51 } 52 53 // RemoveLabel searches for "unlabeled" event. 54 type RemoveLabel struct{} 55 56 // Match if the event is of type "unlabeled" 57 func (r RemoveLabel) Match(event *github.IssueEvent) bool { 58 if event == nil || event.Event == nil { 59 return false 60 } 61 return *event.Event == "unlabeled" 62 } 63 64 // LabelPrefix searches for event whose label starts with the string 65 type LabelPrefix string 66 67 // Match if the label starts with the string 68 func (l LabelPrefix) Match(event *github.IssueEvent) bool { 69 if event == nil || event.Label == nil || event.Label.Name == nil { 70 return false 71 } 72 return strings.HasPrefix(*event.Label.Name, string(l)) 73 } 74 75 // LabelName searches for event whose label starts with the string 76 type LabelName string 77 78 // Match if the label is exactly provided string 79 func (l LabelName) Match(event *github.IssueEvent) bool { 80 if event == nil || event.Label == nil || event.Label.Name == nil { 81 return false 82 } 83 return *event.Label.Name == string(l) 84 } 85 86 // CreatedAfter looks for event created after time 87 type CreatedAfter time.Time 88 89 // Match if the event is after the time 90 func (c CreatedAfter) Match(event *github.IssueEvent) bool { 91 if event == nil || event.CreatedAt == nil { 92 return false 93 } 94 return event.CreatedAt.After(time.Time(c)) 95 } 96 97 // CreatedBefore looks for event created before time 98 type CreatedBefore time.Time 99 100 // Match if the event is before the time 101 func (c CreatedBefore) Match(event *github.IssueEvent) bool { 102 if event == nil || event.CreatedAt == nil { 103 return false 104 } 105 return event.CreatedAt.Before(time.Time(c)) 106 } 107 108 // JenkinsBotActor returns a matcher that checks if the event was completed by JenkinsBot 109 func JenkinsBotActor() Matcher { 110 return Actor("k8s-bot") 111 } 112 113 // BotActor returns a matcher that checks if the event was done by either of the Bots 114 func BotActor(mungeBotName string) Matcher { 115 return Or([]Matcher{ 116 Actor(mungeBotName), 117 JenkinsBotActor(), 118 }) 119 } 120 121 // HumanActor returns a matcher that checks if the event was done by a Human (Not a Bot) 122 func HumanActor(mungeBotName string) Matcher { 123 return Not{BotActor(mungeBotName)} 124 }