From a201c8a1bb1c31ed0c94f68806aec53e2cf0a1ce Mon Sep 17 00:00:00 2001 From: Luke Kysow Date: Thu, 2 Aug 2018 20:43:09 -0700 Subject: [PATCH] Ignore extra newlines in comments. Accept comments like ``` atlantis plan ``` So that when a comment is copy-pasted in GitHub and GitHub adds two newlines, Atlantis still parses that comment. --- server/events/comment_parser.go | 8 ++++++++ server/events/comment_parser_test.go | 25 +++++++++++++++++++++++++ server/events/event_parser.go | 5 ----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/events/comment_parser.go b/server/events/comment_parser.go index 00fb8a308..c594efca4 100644 --- a/server/events/comment_parser.go +++ b/server/events/comment_parser.go @@ -18,6 +18,7 @@ import ( "io/ioutil" "net/url" "path/filepath" + "regexp" "strings" "github.com/runatlantis/atlantis/server/events/models" @@ -38,6 +39,13 @@ const ( DefaultDir = "." ) +// multiLineRegex is used to ignore multi-line comments since those aren't valid +// Atlantis commands. If the second line just has newlines then we let it pass +// through because when you double click on a comment in GitHub and then you +// paste it again, GitHub adds two newlines and so we wanted to allow copying +// and pasting GitHub comments. +var multiLineRegex = regexp.MustCompile(`.*\r?\n[^\r\n]+`) + //go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_comment_parsing.go CommentParsing // CommentParsing handles parsing pull request comments. diff --git a/server/events/comment_parser_test.go b/server/events/comment_parser_test.go index 2049b6bd3..85342adc2 100644 --- a/server/events/comment_parser_test.go +++ b/server/events/comment_parser_test.go @@ -235,6 +235,31 @@ 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) { + comments := []string{ + "atlantis plan\n", + "atlantis plan\n\n", + "atlantis plan\r\n", + "atlantis plan\r\n\r\n", + } + for _, comment := range comments { + t.Run(comment, func(t *testing.T) { + r := commentParser.Parse(comment, models.Github) + Equals(t, "", r.CommentResponse) + Equals(t, &events.CommentCommand{ + RepoRelDir: ".", + Flags: nil, + Name: events.PlanCommand, + Verbose: false, + Workspace: "default", + ProjectName: "", + }, r.Command) + }) + } +} + func TestParse_InvalidWorkspace(t *testing.T) { t.Log("if -w is used with '..' or '/', should return an error") comments := []string{ diff --git a/server/events/event_parser.go b/server/events/event_parser.go index 02f786bf1..af5f8c178 100644 --- a/server/events/event_parser.go +++ b/server/events/event_parser.go @@ -17,7 +17,6 @@ import ( "encoding/json" "fmt" "path" - "regexp" "strings" "github.com/google/go-github/github" @@ -32,10 +31,6 @@ import ( const gitlabPullOpened = "opened" const usagesCols = 90 -// multiLineRegex is used to ignore multi-line comments since those aren't valid -// Atlantis commands. -var multiLineRegex = regexp.MustCompile(`.*\r?\n.+`) - type CommandInterface interface { CommandName() CommandName IsVerbose() bool