github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/interactions.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 import ( 20 "regexp" 21 "strings" 22 23 "github.com/google/go-github/github" 24 ) 25 26 // NotificationName identifies notifications by name 27 type NotificationName string 28 29 var _ Matcher = NotificationName("") 30 31 func (NotificationName) MatchEvent(event *github.IssueEvent) bool { 32 return false 33 } 34 35 // Match returns true if the comment is a notification with the given name 36 func (b NotificationName) MatchComment(comment *github.IssueComment) bool { 37 notif := ParseNotification(comment) 38 if notif == nil { 39 return false 40 } 41 42 return strings.ToUpper(notif.Name) == strings.ToUpper(string(b)) 43 } 44 45 // Match returns true if the comment is a notification with the given name 46 func (b NotificationName) MatchReviewComment(review *github.PullRequestComment) bool { 47 return false 48 } 49 50 // CommandName identifies commands by name 51 type CommandName string 52 53 func (CommandName) MatchEvent(event *github.IssueEvent) bool { 54 return false 55 } 56 57 // Match if the comment contains a command with the given name 58 func (c CommandName) MatchComment(comment *github.IssueComment) bool { 59 commands := ParseCommands(comment) 60 for _, command := range commands { 61 if strings.ToUpper(command.Name) == strings.ToUpper(string(c)) { 62 return true 63 } 64 } 65 return false 66 } 67 68 func (CommandName) MatchReviewComment(review *github.PullRequestComment) bool { 69 return false 70 } 71 72 // CommandArguments identifies commands by arguments (with regex) 73 type CommandArguments regexp.Regexp 74 75 func (*CommandArguments) MatchEvent(evnet *github.IssueEvent) bool { 76 return false 77 } 78 79 // Match if the comment contains a command whose arguments match the regexp 80 func (c *CommandArguments) MatchComment(comment *github.IssueComment) bool { 81 commands := ParseCommands(comment) 82 for _, command := range commands { 83 if (*regexp.Regexp)(c).MatchString(command.Arguments) { 84 return true 85 } 86 } 87 return false 88 } 89 90 func (*CommandArguments) MatchReviewComment(review *github.PullRequestComment) bool { 91 return false 92 } 93 94 // JenkinsBotAuthor creates a matcher to find jenkins bot comments 95 func JenkinsBotAuthor() Matcher { 96 return AuthorLogin("k8s-bot") 97 } 98 99 // BotAuthor creates a matcher to find any bot comments 100 func BotAuthor(mungeBotName string) Matcher { 101 return Or( 102 AuthorLogin(mungeBotName), 103 JenkinsBotAuthor(), 104 ) 105 } 106 107 // HumanActor creates a matcher to find non-bot comments. 108 // ValidAuthor is used because a comment that doesn't have "Author" is NOT made by a human 109 func HumanActor(mungeBotName string) Matcher { 110 return And( 111 ValidAuthor(), 112 Not(BotAuthor(mungeBotName)), 113 ) 114 } 115 116 // MungerNotificationName finds notification posted by the munger, based on name 117 func MungerNotificationName(notif, mungeBotName string) Matcher { 118 return And( 119 AuthorLogin(mungeBotName), 120 NotificationName(notif), 121 ) 122 }