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