From 9295fabb9fcb09ffa0556b0535a1f5a617f834d3 Mon Sep 17 00:00:00 2001 From: Xavi Mabras Date: Thu, 4 May 2023 14:33:47 +0200 Subject: [PATCH] feat: case insensitive pr comments (#3361) * Basic case insensitive for pr comments. * Add test for uppercase/lowercase case. * Merge test functions that check similar behaviors. --------- Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com> Co-authored-by: PePe Amengual --- server/events/comment_parser.go | 13 +++++++++---- server/events/comment_parser_test.go | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/server/events/comment_parser.go b/server/events/comment_parser.go index 7eed241dcb..d2336102bf 100644 --- a/server/events/comment_parser.go +++ b/server/events/comment_parser.go @@ -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])} } @@ -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} } @@ -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"}) { diff --git a/server/events/comment_parser_test.go b/server/events/comment_parser_test.go index 249331412d..9c4b19d4f5 100644 --- a/server/events/comment_parser_test.go +++ b/server/events/comment_parser_test.go @@ -261,7 +261,6 @@ func TestParse_InvalidCommand(t *testing.T) { "a warning.") comments := []string{ "atlantis paln", - "atlantis Plan", "atlantis appely apply", } cp := events.NewCommentParser( @@ -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", @@ -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) {