github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/events.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 plugins 18 19 import ( 20 "fmt" 21 "strings" 22 ) 23 24 // EventMatcher generates events based on name and labels 25 type EventMatcher interface { 26 // Does eventName and label match the event 27 Match(eventName, label string) bool 28 // Return the opposite of this eventmatcher 29 Opposite() EventMatcher 30 } 31 32 // FalseEvent is a false event 33 type FalseEvent struct{} 34 35 var _ EventMatcher = FalseEvent{} 36 37 // Match is false 38 func (FalseEvent) Match(eventName, label string) bool { 39 return false 40 } 41 42 // Opposite is true 43 func (FalseEvent) Opposite() EventMatcher { 44 return TrueEvent{} 45 } 46 47 // TrueEvent is a true event 48 type TrueEvent struct{} 49 50 var _ EventMatcher = TrueEvent{} 51 52 // Match is true 53 func (TrueEvent) Match(eventName, label string) bool { 54 return true 55 } 56 57 // Opposite is false 58 func (TrueEvent) Opposite() EventMatcher { 59 return FalseEvent{} 60 } 61 62 // OpenEvent is an "opened" event 63 type OpenEvent struct{} 64 65 var _ EventMatcher = OpenEvent{} 66 67 // Match is "opened" 68 func (OpenEvent) Match(eventName, label string) bool { 69 return eventName == "opened" 70 } 71 72 // Opposite is closed 73 func (OpenEvent) Opposite() EventMatcher { 74 return CloseEvent{} 75 } 76 77 // CommentEvent is a "commented" event 78 type CommentEvent struct{} 79 80 var _ EventMatcher = CommentEvent{} 81 82 // Match is "commented" 83 func (CommentEvent) Match(eventName, label string) bool { 84 return eventName == "commented" 85 } 86 87 // Opposite is false 88 func (CommentEvent) Opposite() EventMatcher { 89 return FalseEvent{} 90 } 91 92 // LabelEvent is a "labeled" event 93 type LabelEvent struct { 94 Label string 95 } 96 97 var _ EventMatcher = LabelEvent{} 98 99 // Match is "labeled" with label 100 func (l LabelEvent) Match(eventName, label string) bool { 101 return eventName == "labeled" && label == l.Label 102 } 103 104 // Opposite is unlabel 105 func (l LabelEvent) Opposite() EventMatcher { 106 return UnlabelEvent{Label: l.Label} 107 } 108 109 // UnlabelEvent is an "unlabeled" event 110 type UnlabelEvent struct { 111 Label string 112 } 113 114 var _ EventMatcher = UnlabelEvent{} 115 116 // Match is "unlabeled" 117 func (u UnlabelEvent) Match(eventName, label string) bool { 118 return eventName == "unlabeled" && label == u.Label 119 } 120 121 // Opposite is label 122 func (u UnlabelEvent) Opposite() EventMatcher { 123 return LabelEvent{Label: u.Label} 124 } 125 126 // CloseEvent is a "closed" event 127 type CloseEvent struct{} 128 129 var _ EventMatcher = CloseEvent{} 130 131 // Match is "closed" 132 func (CloseEvent) Match(eventName, label string) bool { 133 return eventName == "closed" 134 } 135 136 // Opposite is reopen 137 func (CloseEvent) Opposite() EventMatcher { 138 return ReopenEvent{} 139 } 140 141 // ReopenEvent is a "reopened" event 142 type ReopenEvent struct{} 143 144 var _ EventMatcher = ReopenEvent{} 145 146 // Match is "reopened" 147 func (ReopenEvent) Match(eventName, label string) bool { 148 return eventName == "reopened" 149 } 150 151 // Opposite is close 152 func (ReopenEvent) Opposite() EventMatcher { 153 return CloseEvent{} 154 } 155 156 // MergeEvent is a "merged" event 157 type MergeEvent struct{} 158 159 var _ EventMatcher = MergeEvent{} 160 161 // Match is "merged" 162 func (MergeEvent) Match(eventName, label string) bool { 163 return eventName == "merged" 164 } 165 166 // Opposite is false 167 func (MergeEvent) Opposite() EventMatcher { 168 // A merge can't be undone. 169 return FalseEvent{} 170 } 171 172 // NewEventMatcher returns the correct EventMatcher based on description 173 // Incoming event should have the following form: 174 // eventName:labelName. If eventName is not label, then the second part 175 // can be omitted. 176 func NewEventMatcher(eventDescription string) EventMatcher { 177 split := strings.SplitN(eventDescription, ":", 2) 178 switch split[0] { 179 case "": 180 return FalseEvent{} 181 case "commented": 182 return CommentEvent{} 183 case "opened": 184 return OpenEvent{} 185 case "reopened": 186 return ReopenEvent{} 187 case "merged": 188 return MergeEvent{} 189 case "closed": 190 return CloseEvent{} 191 case "labeled": 192 if len(split) != 2 { 193 panic(fmt.Errorf("Missing label part of the event")) 194 } 195 return LabelEvent{split[1]} 196 case "unlabeled": 197 if len(split) != 2 { 198 panic(fmt.Errorf("Missing label part of the event")) 199 } 200 return UnlabelEvent{split[1]} 201 default: 202 panic(fmt.Errorf("Unknown type of event: %s", split[0])) 203 } 204 }