This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1582 from apelisse/human-machine-interaction
Create Human/Bot interaction tools
- Loading branch information
Showing
9 changed files
with
697 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package comment | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/google/go-github/github" | ||
) | ||
|
||
// Command is a way for human to interact with the bot | ||
type Command struct { | ||
Name string | ||
Arguments string | ||
} | ||
|
||
var ( | ||
// Matches a command: | ||
// - Line that starts with slash | ||
// - followed by non-space characteres, | ||
// - (optional) followed by space and arguments | ||
commandRegex = regexp.MustCompile(`^/([^\s]+)\s?(.*)$`) | ||
) | ||
|
||
// ParseCommand attempts to read a command from a comment | ||
// Returns nil if the comment doesn't contain a command | ||
func ParseCommand(comment *github.IssueComment) *Command { | ||
if comment == nil || comment.Body == nil { | ||
return nil | ||
} | ||
|
||
match := commandRegex.FindStringSubmatch(*comment.Body) | ||
if match == nil { | ||
return nil | ||
} | ||
|
||
return &Command{ | ||
Name: strings.ToUpper(match[1]), | ||
Arguments: strings.TrimSpace(match[2]), | ||
} | ||
} | ||
|
||
// String displays the command | ||
func (n *Command) String() string { | ||
str := "/" + strings.ToUpper(n.Name) | ||
args := strings.TrimSpace(n.Arguments) | ||
if args != "" { | ||
str += " " + args | ||
} | ||
return str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package comment | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-github/github" | ||
) | ||
|
||
func TestParseCommand(t *testing.T) { | ||
tests := []struct { | ||
expectedCommand *Command | ||
comment string | ||
}{ | ||
{ | ||
expectedCommand: nil, | ||
comment: "I have nothing to do with a command", | ||
}, | ||
{ | ||
expectedCommand: nil, | ||
comment: " /COMMAND Line can't start with spaces", | ||
}, | ||
{ | ||
expectedCommand: &Command{Name: "COMMAND"}, | ||
comment: "/COMMAND", | ||
}, | ||
{ | ||
expectedCommand: &Command{Name: "COMMAND", Arguments: "Valid command"}, | ||
comment: "/COMMAND Valid command", | ||
}, | ||
{ | ||
expectedCommand: &Command{Name: "COMMAND", Arguments: "Command name is upper-cased"}, | ||
comment: "/command Command name is upper-cased", | ||
}, | ||
{ | ||
expectedCommand: &Command{Name: "COMMAND", Arguments: "Arguments is trimmed"}, | ||
comment: "/COMMAND Arguments is trimmed ", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
actualCommand := ParseCommand(&github.IssueComment{Body: &test.comment}) | ||
if !reflect.DeepEqual(actualCommand, test.expectedCommand) { | ||
t.Error(actualCommand, "doesn't match expected command:", test.expectedCommand) | ||
} | ||
} | ||
} | ||
|
||
func TestStringCommand(t *testing.T) { | ||
tests := []struct { | ||
command *Command | ||
expectedString string | ||
}{ | ||
{ | ||
command: &Command{Name: "COMMAND", Arguments: "Argument"}, | ||
expectedString: "/COMMAND Argument", | ||
}, | ||
{ | ||
command: &Command{Name: "command", Arguments: " Argument "}, | ||
expectedString: "/COMMAND Argument", | ||
}, | ||
{ | ||
command: &Command{Name: "command"}, | ||
expectedString: "/COMMAND", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
actualString := test.command.String() | ||
if actualString != test.expectedString { | ||
t.Error(actualString, "doesn't match expected string:", test.expectedString) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.