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