github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/matchers.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 matchers 18 19 // Matcher is an interface to match an event 20 import ( 21 "strings" 22 "time" 23 24 "fmt" 25 26 "github.com/google/go-github/github" 27 ) 28 29 // Matcher matches against a comment or an event 30 type Matcher interface { 31 MatchEvent(event *github.IssueEvent) bool 32 MatchComment(comment *github.IssueComment) bool 33 MatchReviewComment(comment *github.PullRequestComment) bool 34 } 35 36 // CreatedAfter matches comments created after the time 37 type CreatedAfter time.Time 38 39 var _ Matcher = CreatedAfter{} 40 41 // MatchComment returns true if the comment is created after the time 42 func (c CreatedAfter) MatchComment(comment *github.IssueComment) bool { 43 if comment == nil || comment.CreatedAt == nil { 44 return false 45 } 46 return comment.CreatedAt.After(time.Time(c)) 47 } 48 49 // MatchEvent returns true if the event is created after the time 50 func (c CreatedAfter) MatchEvent(event *github.IssueEvent) bool { 51 if event == nil || event.CreatedAt == nil { 52 return false 53 } 54 return event.CreatedAt.After(time.Time(c)) 55 } 56 57 // MatchReviewComment returns true if the review comment is created after the time 58 func (c CreatedAfter) MatchReviewComment(review *github.PullRequestComment) bool { 59 if review == nil || review.CreatedAt == nil { 60 return false 61 } 62 return review.CreatedAt.After(time.Time(c)) 63 } 64 65 // CreatedBefore matches Items created before the time 66 type CreatedBefore time.Time 67 68 var _ Matcher = CreatedBefore{} 69 70 // MatchComment returns true if the comment is created before the time 71 func (c CreatedBefore) MatchComment(comment *github.IssueComment) bool { 72 if comment == nil || comment.CreatedAt == nil { 73 return false 74 } 75 return comment.CreatedAt.Before(time.Time(c)) 76 } 77 78 // MatchEvent returns true if the event is created before the time 79 func (c CreatedBefore) MatchEvent(event *github.IssueEvent) bool { 80 if event == nil || event.CreatedAt == nil { 81 return false 82 } 83 return event.CreatedAt.Before(time.Time(c)) 84 } 85 86 // MatchReviewComment returns true if the review comment is created before the time 87 func (c CreatedBefore) MatchReviewComment(review *github.PullRequestComment) bool { 88 if review == nil || review.CreatedAt == nil { 89 return false 90 } 91 return review.CreatedAt.Before(time.Time(c)) 92 } 93 94 // UpdatedAfter matches comments updated after the time 95 type UpdatedAfter time.Time 96 97 var _ Matcher = UpdatedAfter{} 98 99 // MatchComment returns true if the comment is updated after the time 100 func (u UpdatedAfter) MatchComment(comment *github.IssueComment) bool { 101 if comment == nil || comment.UpdatedAt == nil { 102 return false 103 } 104 return comment.UpdatedAt.After(time.Time(u)) 105 } 106 107 // MatchEvent returns true if the event is updated after the time 108 func (u UpdatedAfter) MatchEvent(event *github.IssueEvent) bool { 109 if event == nil || event.CreatedAt == nil { 110 return false 111 } 112 return event.CreatedAt.Before(time.Time(u)) 113 } 114 115 // MatchReviewComment returns true if the review comment is updated after the time 116 func (u UpdatedAfter) MatchReviewComment(review *github.PullRequestComment) bool { 117 if review == nil || review.UpdatedAt == nil { 118 return false 119 } 120 return review.UpdatedAt.After(time.Time(u)) 121 } 122 123 // UpdatedBefore matches Items updated before the time 124 type UpdatedBefore time.Time 125 126 var _ Matcher = UpdatedBefore{} 127 128 // MatchComment returns true if the comment is created before the time 129 func (u UpdatedBefore) MatchComment(comment *github.IssueComment) bool { 130 if comment == nil || comment.UpdatedAt == nil { 131 return false 132 } 133 return comment.UpdatedAt.Before(time.Time(u)) 134 } 135 136 // MatchEvent returns true if the event is created before the time 137 func (u UpdatedBefore) MatchEvent(event *github.IssueEvent) bool { 138 if event == nil || event.CreatedAt == nil { 139 return false 140 } 141 return event.CreatedAt.Before(time.Time(u)) 142 } 143 144 // MatchReviewComment returns true if the review comment is created before the time 145 func (u UpdatedBefore) MatchReviewComment(review *github.PullRequestComment) bool { 146 if review == nil || review.UpdatedAt == nil { 147 return false 148 } 149 return review.UpdatedAt.Before(time.Time(u)) 150 } 151 152 type validAuthorMatcher struct{} 153 154 func ValidAuthor() Matcher { 155 return validAuthorMatcher{} 156 } 157 158 func (v validAuthorMatcher) MatchEvent(event *github.IssueEvent) bool { 159 return event != nil && event.Actor != nil && event.Actor.Login != nil 160 } 161 162 func (v validAuthorMatcher) MatchComment(comment *github.IssueComment) bool { 163 return comment != nil && comment.User != nil && comment.User.Login != nil 164 } 165 166 func (v validAuthorMatcher) MatchReviewComment(review *github.PullRequestComment) bool { 167 return review != nil && review.User != nil && review.User.Login != nil 168 } 169 170 type AuthorLogin string 171 172 var _ Matcher = AuthorLogin("") 173 174 func (a AuthorLogin) MatchEvent(event *github.IssueEvent) bool { 175 if !(ValidAuthor()).MatchEvent(event) { 176 return false 177 } 178 179 return strings.ToLower(*event.Actor.Login) == strings.ToLower(string(a)) 180 } 181 182 func (a AuthorLogin) MatchComment(comment *github.IssueComment) bool { 183 fmt.Printf("matching comment: %v\n", comment) 184 if !(ValidAuthor()).MatchComment(comment) { 185 fmt.Println("comment does not have a valid author") 186 return false 187 } 188 189 fmt.Printf("comparing %s from comment to %s from matcher\n", strings.ToLower(*comment.User.Login), strings.ToLower(string(a))) 190 return strings.ToLower(*comment.User.Login) == strings.ToLower(string(a)) 191 } 192 193 func (a AuthorLogin) MatchReviewComment(review *github.PullRequestComment) bool { 194 if !(ValidAuthor()).MatchReviewComment(review) { 195 return false 196 } 197 198 return strings.ToLower(*review.User.Login) == strings.ToLower(string(a)) 199 } 200 201 func AuthorLogins(authors ...string) Matcher { 202 matchers := []Matcher{} 203 204 for _, author := range authors { 205 matchers = append(matchers, AuthorLogin(author)) 206 } 207 208 return Or(matchers...) 209 } 210 211 func AuthorUsers(users ...*github.User) Matcher { 212 authors := []string{} 213 214 for _, user := range users { 215 if user == nil || user.Login == nil { 216 continue 217 } 218 authors = append(authors, *user.Login) 219 } 220 221 return AuthorLogins(authors...) 222 } 223 224 // addLabelMatcher searches for "labeled" event. 225 type addLabelMatcher struct{} 226 227 func AddLabel() Matcher { 228 return addLabelMatcher{} 229 } 230 231 // Match if the event is of type "labeled" 232 func (a addLabelMatcher) MatchEvent(event *github.IssueEvent) bool { 233 if event == nil || event.Event == nil { 234 return false 235 } 236 return *event.Event == "labeled" 237 } 238 239 func (a addLabelMatcher) MatchComment(comment *github.IssueComment) bool { 240 return false 241 } 242 243 func (a addLabelMatcher) MatchReviewComment(review *github.PullRequestComment) bool { 244 return false 245 } 246 247 // LabelName searches for event whose label starts with the string 248 type LabelName string 249 250 var _ Matcher = LabelName("") 251 252 // Match if the label starts with the string 253 func (l LabelName) MatchEvent(event *github.IssueEvent) bool { 254 if event == nil || event.Label == nil || event.Label.Name == nil { 255 return false 256 } 257 return *event.Label.Name == string(l) 258 } 259 260 func (l LabelName) MatchComment(comment *github.IssueComment) bool { 261 return false 262 } 263 264 func (l LabelName) MatchReviewComment(review *github.PullRequestComment) bool { 265 return false 266 } 267 268 // LabelPrefix searches for event whose label starts with the string 269 type LabelPrefix string 270 271 var _ Matcher = LabelPrefix("") 272 273 // Match if the label starts with the string 274 func (l LabelPrefix) MatchEvent(event *github.IssueEvent) bool { 275 if event == nil || event.Label == nil || event.Label.Name == nil { 276 return false 277 } 278 return strings.HasPrefix(*event.Label.Name, string(l)) 279 } 280 281 func (l LabelPrefix) MatchComment(comment *github.IssueComment) bool { 282 return false 283 } 284 285 func (l LabelPrefix) MatchReviewComment(review *github.PullRequestComment) bool { 286 return false 287 } 288 289 type eventTypeMatcher struct{} 290 291 func EventType() Matcher { 292 return eventTypeMatcher{} 293 } 294 295 func (c eventTypeMatcher) MatchEvent(event *github.IssueEvent) bool { 296 return true 297 } 298 299 func (c eventTypeMatcher) MatchComment(comment *github.IssueComment) bool { 300 return false 301 } 302 303 func (c eventTypeMatcher) MatchReviewComment(review *github.PullRequestComment) bool { 304 return false 305 } 306 307 type commentTypeMatcher struct{} 308 309 func CommentType() Matcher { 310 return commentTypeMatcher{} 311 } 312 313 func (c commentTypeMatcher) MatchEvent(event *github.IssueEvent) bool { 314 return false 315 } 316 317 func (c commentTypeMatcher) MatchComment(comment *github.IssueComment) bool { 318 return true 319 } 320 321 func (c commentTypeMatcher) MatchReviewComment(review *github.PullRequestComment) bool { 322 return false 323 } 324 325 type reviewCommentTypeMatcher struct{} 326 327 func ReviewCommentType() Matcher { 328 return reviewCommentTypeMatcher{} 329 } 330 331 func (c reviewCommentTypeMatcher) MatchEvent(event *github.IssueEvent) bool { 332 return false 333 } 334 335 func (c reviewCommentTypeMatcher) MatchComment(comment *github.IssueComment) bool { 336 return false 337 } 338 339 func (c reviewCommentTypeMatcher) MatchReviewComment(review *github.PullRequestComment) bool { 340 return true 341 }