Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Atlantis replying to non-atlantis comments. #534

Merged
merged 2 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 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 @@ -65,10 +64,9 @@ type CommentBuilder interface {

// CommentParser implements CommentParsing
type CommentParser struct {
GithubUser string
GithubToken string
GitlabUser string
GitlabToken string
GithubUser string
GitlabUser string
BitbucketUser string
}

// CommentParseResult describes the result of parsing a comment as a command.
Expand Down Expand Up @@ -104,10 +102,9 @@ 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}
}
Expand All @@ -119,18 +116,30 @@ func (e *CommentParser) Parse(comment string, vcsHost models.VCSHostType) Commen

// 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 {
var vcsUser string
switch vcsHost {
case models.Github:
vcsUser = e.GithubUser
case models.Gitlab:
vcsUser = e.GitlabUser
case models.BitbucketCloud, models.BitbucketServer:
vcsUser = e.BitbucketUser
}
executableNames := []string{"run", atlantisExecutable, "@" + vcsUser}

// If the comment doesn't start with the name of our 'executable' then
// ignore it.
if !e.stringInSlice(args[0], executableNames) {
return CommentParseResult{Ignore: true}
}

// 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.
if len(args) == 1 {
Expand Down
43 changes: 39 additions & 4 deletions server/events/comment_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import (
)

var commentParser = events.CommentParser{
GithubUser: "github-user",
GithubToken: "github-token",
GitlabUser: "gitlab-user",
GitlabToken: "gitlab-token",
GithubUser: "github-user",
GitlabUser: "gitlab-user",
}

func TestParse_Ignored(t *testing.T) {
Expand All @@ -37,6 +35,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 Expand Up @@ -643,6 +642,42 @@ func TestBuildPlanApplyComment(t *testing.T) {
}
}

func TestParse_VCSUsername(t *testing.T) {
cp := events.CommentParser{
GithubUser: "gh",
GitlabUser: "gl",
BitbucketUser: "bb",
}
cases := []struct {
vcs models.VCSHostType
user string
}{
{
vcs: models.Github,
user: "gh",
},
{
vcs: models.Gitlab,
user: "gl",
},
{
vcs: models.BitbucketServer,
user: "bb",
},
{
vcs: models.BitbucketCloud,
user: "bb",
},
}

for _, c := range cases {
t.Run(c.vcs.String(), func(t *testing.T) {
r := cp.Parse(fmt.Sprintf("@%s %s", c.user, "help"), c.vcs)
Equals(t, events.HelpComment, r.CommentResponse)
})
}
}

var PlanUsage = `Usage of plan:
-d, --dir string Which directory to run plan in relative to root of repo,
ex. 'child/dir'.
Expand Down
6 changes: 2 additions & 4 deletions server/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,8 @@ func setupE2E(t *testing.T) (server.EventsController, *vcsmocks.MockClient, *moc
GitlabToken: "gitlab-token",
}
commentParser := &events.CommentParser{
GithubUser: "github-user",
GithubToken: "github-token",
GitlabUser: "gitlab-user",
GitlabToken: "gitlab-token",
GithubUser: "github-user",
GitlabUser: "gitlab-user",
}
terraformClient, err := terraform.NewClient(dataDir, "")
Ok(t, err)
Expand Down
7 changes: 3 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
BitbucketServerURL: userConfig.BitbucketBaseURL,
}
commentParser := &events.CommentParser{
GithubUser: userConfig.GithubUser,
GithubToken: userConfig.GithubToken,
GitlabUser: userConfig.GitlabUser,
GitlabToken: userConfig.GitlabToken,
GithubUser: userConfig.GithubUser,
GitlabUser: userConfig.GitlabUser,
BitbucketUser: userConfig.BitbucketUser,
}
defaultTfVersion := terraformClient.Version()
pendingPlanFinder := &events.DefaultPendingPlanFinder{}
Expand Down