Skip to content

Commit

Permalink
feat: case insensitive pr comments (runatlantis#3361)
Browse files Browse the repository at this point in the history
* Basic case insensitive for pr comments.

* Add test for uppercase/lowercase case.

* Merge test functions that check similar behaviors.

---------

Co-authored-by: nitrocode <[email protected]>
Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
3 people authored and Marcelo Medeiros committed Jul 3, 2023
1 parent cb5bb0f commit daec8c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions server/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
return CommentParseResult{Ignore: true}
}

// Lowercase it to avoid autocorrect issues with browsers.
executableName := strings.ToLower(args[0])

// Helpfully warn the user if they're using "terraform" instead of "atlantis"
if args[0] == "terraform" && e.ExecutableName != "terraform" {
if executableName == "terraform" && e.ExecutableName != "terraform" {
return CommentParseResult{CommentResponse: fmt.Sprintf(DidYouMeanAtlantisComment, e.ExecutableName, "terraform")}
}

// Helpfully warn the user that the command might be misspelled
if utils.IsSimilarWord(args[0], e.ExecutableName) {
if utils.IsSimilarWord(executableName, e.ExecutableName) {
return CommentParseResult{CommentResponse: fmt.Sprintf(DidYouMeanAtlantisComment, e.ExecutableName, args[0])}
}

Expand All @@ -177,7 +180,7 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
vcsUser = e.AzureDevopsUser
}
executableNames := []string{"run", e.ExecutableName, "@" + vcsUser}
if !e.stringInSlice(args[0], executableNames) {
if !e.stringInSlice(executableName, executableNames) {
return CommentParseResult{Ignore: true}
}

Expand All @@ -196,7 +199,9 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
if len(args) == 1 {
return CommentParseResult{CommentResponse: e.HelpComment()}
}
cmd := args[1]

// Lowercase it to avoid autocorrect issues with browsers.
cmd := strings.ToLower(args[1])

// Help output.
if e.stringInSlice(cmd, []string{"help", "-h", "--help"}) {
Expand Down
9 changes: 5 additions & 4 deletions server/events/comment_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ func TestParse_InvalidCommand(t *testing.T) {
"a warning.")
comments := []string{
"atlantis paln",
"atlantis Plan",
"atlantis appely apply",
}
cp := events.NewCommentParser(
Expand Down Expand Up @@ -383,9 +382,7 @@ func TestParse_RelativeDirPath(t *testing.T) {
}
}

// If there's multiple lines but it's whitespace, allow the command. This
// occurs when you copy and paste via GitHub.
func TestParse_Multiline(t *testing.T) {
func TestParse_ValidCommand(t *testing.T) {
comments := []string{
"atlantis plan\n",
"atlantis plan\n\n",
Expand All @@ -395,6 +392,10 @@ func TestParse_Multiline(t *testing.T) {
"\r\natlantis plan",
"\natlantis plan\n",
"\r\natlantis plan\r\n",
"atlantis plan",
"Atlantis plan",
"Atlantis Plan",
"ATLANTIS PLAN",
}
for _, comment := range comments {
t.Run(comment, func(t *testing.T) {
Expand Down

0 comments on commit daec8c8

Please sign in to comment.