github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/command.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 // Command is a way for human to interact with the bot 27 type Command struct { 28 Name string 29 Arguments string 30 } 31 32 var ( 33 // Matches a command: 34 // - Line that starts with slash 35 // - followed by non-space characteres, 36 // - (optional) followed by space and arguments 37 commandRegex = regexp.MustCompile(`(?m)^/([^\s]+)[\t ]*([^\n\r]*)`) 38 ) 39 40 // ParseCommands attempts to read a command from a comment 41 // Returns nil if the comment doesn't contain a command 42 func ParseCommands(comment *github.IssueComment) []*Command { 43 if comment == nil || comment.Body == nil { 44 return nil 45 } 46 47 commands := []*Command{} 48 matches := commandRegex.FindAllStringSubmatch(*comment.Body, -1) 49 for _, match := range matches { 50 commands = append(commands, &Command{ 51 Name: strings.ToUpper(match[1]), 52 Arguments: strings.TrimSpace(match[2]), 53 }) 54 } 55 56 return commands 57 } 58 59 // String displays the command 60 func (n *Command) String() string { 61 str := "/" + strings.ToUpper(n.Name) 62 args := strings.TrimSpace(n.Arguments) 63 if args != "" { 64 str += " " + args 65 } 66 return str 67 }