Skip to content

Commit

Permalink
Stop Atlantis replying to non-atlantis comments.
Browse files Browse the repository at this point in the history
Fixes #533, a regression where Atlantis will respond with errors to
comments that aren't intended to invoke Atlantis.
  • Loading branch information
lkysow committed Mar 13, 2019
1 parent aab2211 commit 75b1639
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
38 changes: 21 additions & 17 deletions server/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ package events
import (
"fmt"
"github.com/flynn-archive/go-shlex"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/yaml"
"github.com/spf13/pflag"
"io/ioutil"
"net/url"
"path/filepath"
"regexp"
"strings"

"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/yaml"
"github.com/spf13/pflag"
)

const (
Expand Down Expand Up @@ -104,32 +103,37 @@ func (e *CommentParser) Parse(comment string, vcsHost models.VCSHostType) Commen
return CommentParseResult{Ignore: true}
}

args, err := shlex.Split(comment)
if err != nil {
return CommentParseResult{CommentResponse: fmt.Sprintf("```\nError parsing command: %s\n```", err)}
}
// We first use strings.Fields to parse and do an initial evaluation.
// Later we use a proper shell parser and re-parse.
args := strings.Fields(comment)
if len(args) < 1 {
return CommentParseResult{Ignore: true}
}

// Helpfully warn the user if they're using "terraform" instead of "atlantis"
if args[0] == "terraform" {
return CommentParseResult{CommentResponse: DidYouMeanAtlantisComment}
}

// Atlantis can be invoked using the name of the VCS host user we're
// running under. Need to be able to match against that user.
vcsUser := e.GithubUser
if vcsHost == models.Gitlab {
vcsUser = e.GitlabUser
}
executableNames := []string{"run", atlantisExecutable, "@" + vcsUser}

// If the comment doesn't start with the name of our 'executable' then
// ignore it.
executableNames := []string{"run", "terraform", atlantisExecutable, "@" + vcsUser}
if !e.stringInSlice(args[0], executableNames) {
return CommentParseResult{Ignore: true}
}
// Helpfully warn the user if they're using "terraform" instead of "atlantis"
if args[0] == "terraform" {
return CommentParseResult{CommentResponse: DidYouMeanAtlantisComment}
}

// Now that we know Atlantis is being invoked, re-parse using a shell-style
// parser.
args, err := shlex.Split(comment)
if err != nil {
return CommentParseResult{CommentResponse: fmt.Sprintf("```\nError parsing command: %s\n```", err)}
}
if len(args) < 1 {
return CommentParseResult{Ignore: true}
}

// If they've just typed the name of the executable then give them the help
// output.
Expand Down
1 change: 1 addition & 0 deletions server/events/comment_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestParse_Ignored(t *testing.T) {
"abc",
"atlantis plan\nbut with newlines",
"terraform plan\nbut with newlines",
"This shouldn't error, but it does.",
}
for _, c := range ignoreComments {
r := commentParser.Parse(c, models.Github)
Expand Down

0 comments on commit 75b1639

Please sign in to comment.