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

feat: customize vcs comment command --executable-name #2805

Merged
merged 1 commit into from
Dec 17, 2022
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
9 changes: 9 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
EnablePolicyChecksFlag = "enable-policy-checks"
EnableRegExpCmdFlag = "enable-regexp-cmd"
EnableDiffMarkdownFormat = "enable-diff-markdown-format"
ExecutableName = "executable-name"
GHHostnameFlag = "gh-hostname"
GHTeamAllowlistFlag = "gh-team-allowlist"
GHTokenFlag = "gh-token"
Expand Down Expand Up @@ -135,6 +136,7 @@ const (
DefaultCheckoutStrategy = "branch"
DefaultBitbucketBaseURL = bitbucketcloud.BaseURL
DefaultDataDir = "~/.atlantis"
DefaultExecutableName = "atlantis"
DefaultMarkdownTemplateOverridesDir = "~/.markdown_templates"
DefaultGHHostname = "github.com"
DefaultGitlabHostname = "gitlab.com"
Expand Down Expand Up @@ -229,6 +231,10 @@ var stringFlags = map[string]stringFlag{
description: "Path to directory to store Atlantis data.",
defaultValue: DefaultDataDir,
},
ExecutableName: {
description: "Comment command executable name.",
defaultValue: DefaultExecutableName,
},
GHHostnameFlag: {
description: "Hostname of your Github Enterprise installation. If using github.com, no need to set.",
defaultValue: DefaultGHHostname,
Expand Down Expand Up @@ -749,6 +755,9 @@ func (s *ServerCmd) setDefaults(c *server.UserConfig) {
if c.BitbucketBaseURL == "" {
c.BitbucketBaseURL = DefaultBitbucketBaseURL
}
if c.ExecutableName == "" {
c.ExecutableName = DefaultExecutableName
}
if c.LockingDBType == "" {
c.LockingDBType = DefaultLockingDBType
}
Expand Down
10 changes: 10 additions & 0 deletions runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ and set `--autoplan-modules` to `false`.

Useful to enable for use with GitHub.

### `--executable-name`
```bash
atlantis server --executable-name="atlantis"
# or
ATLANTIS_EXECUTABLE_NAME="atlantis"
```
Comment command trigger executable name. Defaults to `atlantis`.

This is useful when running multiple Atlantis servers against a single repository.

### `--gh-hostname`
```bash
atlantis server --gh-hostname="my.github.enterprise.com"
Expand Down
5 changes: 3 additions & 2 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,9 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
GitlabToken: "gitlab-token",
}
commentParser := &events.CommentParser{
GithubUser: "github-user",
GitlabUser: "gitlab-user",
GithubUser: "github-user",
GitlabUser: "gitlab-user",
ExecutableName: "atlantis",
}
terraformClient, err := terraform.NewClient(logger, binDir, cacheDir, "", "", "", "default-tf-version", "https://releases.hashicorp.com", &NoopTFDownloader{}, false, projectCmdOutputHandler)
Ok(t, err)
Expand Down
8 changes: 4 additions & 4 deletions server/controllers/events/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestPost_GitlabCommentNotAllowlisted(t *testing.T) {
e := events_controllers.VCSEventsController{
Logger: logger,
Scope: scope,
CommentParser: &events.CommentParser{},
CommentParser: &events.CommentParser{ExecutableName: "atlantis"},
GitlabRequestParserValidator: &events_controllers.DefaultGitlabRequestParserValidator{},
Parser: &events.EventParser{},
SupportedVCSHosts: []models.VCSHostType{models.Gitlab},
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestPost_GitlabCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
e := events_controllers.VCSEventsController{
Logger: logger,
Scope: scope,
CommentParser: &events.CommentParser{},
CommentParser: &events.CommentParser{ExecutableName: "atlantis"},
GitlabRequestParserValidator: &events_controllers.DefaultGitlabRequestParserValidator{},
Parser: &events.EventParser{},
SupportedVCSHosts: []models.VCSHostType{models.Gitlab},
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestPost_GithubCommentNotAllowlisted(t *testing.T) {
Logger: logger,
Scope: scope,
GithubRequestValidator: &events_controllers.DefaultGithubRequestValidator{},
CommentParser: &events.CommentParser{},
CommentParser: &events.CommentParser{ExecutableName: "atlantis"},
Parser: &events.EventParser{},
SupportedVCSHosts: []models.VCSHostType{models.Github},
RepoAllowlistChecker: &events.RepoAllowlistChecker{},
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestPost_GithubCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
Logger: logger,
Scope: scope,
GithubRequestValidator: &events_controllers.DefaultGithubRequestValidator{},
CommentParser: &events.CommentParser{},
CommentParser: &events.CommentParser{ExecutableName: "atlantis"},
Parser: &events.EventParser{},
SupportedVCSHosts: []models.VCSHostType{models.Github},
RepoAllowlistChecker: &events.RepoAllowlistChecker{},
Expand Down
10 changes: 5 additions & 5 deletions server/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
autoMergeDisabledFlagShort = ""
verboseFlagLong = "verbose"
verboseFlagShort = ""
atlantisExecutable = "atlantis"
)

// multiLineRegex is used to ignore multi-line comments since those aren't valid
Expand Down Expand Up @@ -79,6 +78,7 @@ type CommentParser struct {
BitbucketUser string
AzureDevopsUser string
ApplyDisabled bool
ExecutableName string
}

// CommentParseResult describes the result of parsing a comment as a command.
Expand Down Expand Up @@ -144,7 +144,7 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
case models.AzureDevops:
vcsUser = e.AzureDevopsUser
}
executableNames := []string{"run", atlantisExecutable, "@" + vcsUser}
executableNames := []string{"run", e.ExecutableName, "@" + vcsUser}
if !e.stringInSlice(args[0], executableNames) {
return CommentParseResult{Ignore: true}
}
Expand Down Expand Up @@ -290,19 +290,19 @@ func (e *CommentParser) BuildPlanComment(repoRelDir string, workspace string, pr
}
commentFlags = fmt.Sprintf(" -- %s", strings.Join(flagsWithoutQuotes, " "))
}
return fmt.Sprintf("%s %s%s%s", atlantisExecutable, command.Plan.String(), flags, commentFlags)
return fmt.Sprintf("%s %s%s%s", e.ExecutableName, command.Plan.String(), flags, commentFlags)
}

// BuildApplyComment builds an apply comment for the specified args.
func (e *CommentParser) BuildApplyComment(repoRelDir string, workspace string, project string, autoMergeDisabled bool) string {
flags := e.buildFlags(repoRelDir, workspace, project, autoMergeDisabled)
return fmt.Sprintf("%s %s%s", atlantisExecutable, command.Apply.String(), flags)
return fmt.Sprintf("%s %s%s", e.ExecutableName, command.Apply.String(), flags)
}

// BuildVersionComment builds a version comment for the specified args.
func (e *CommentParser) BuildVersionComment(repoRelDir string, workspace string, project string) string {
flags := e.buildFlags(repoRelDir, workspace, project, false)
return fmt.Sprintf("%s %s%s", atlantisExecutable, command.Version.String(), flags)
return fmt.Sprintf("%s %s%s", e.ExecutableName, command.Version.String(), flags)
}

func (e *CommentParser) buildFlags(repoRelDir string, workspace string, project string, autoMergeDisabled bool) string {
Expand Down
30 changes: 28 additions & 2 deletions server/events/comment_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

var commentParser = events.CommentParser{
GithubUser: "github-user",
GitlabUser: "gitlab-user",
GithubUser: "github-user",
GitlabUser: "gitlab-user",
ExecutableName: "atlantis",
}

func TestParse_Ignored(t *testing.T) {
Expand All @@ -44,6 +45,30 @@ func TestParse_Ignored(t *testing.T) {
}
}

func TestParse_ExecutableName(t *testing.T) {
cases := []struct {
user string
expIgnore bool
}{
{"custom-executable-name", false},
{"run", false},
{"@github-user", false},
{"github-user", true},
{"atlantis", true},
}
for _, c := range cases {
t.Run(c.user, func(t *testing.T) {
var commentParser = events.CommentParser{
GithubUser: "github-user",
ExecutableName: "custom-executable-name",
}
comment := fmt.Sprintf("%s help", c.user)
r := commentParser.Parse(comment, models.Github)
Assert(t, r.Ignore == c.expIgnore, "expected Ignore %q, but got %q", c.expIgnore, r.Ignore)
})
}
}

func TestParse_HelpResponse(t *testing.T) {
helpComments := []string{
"run",
Expand Down Expand Up @@ -789,6 +814,7 @@ func TestParse_VCSUsername(t *testing.T) {
GitlabUser: "gl",
BitbucketUser: "bb",
AzureDevopsUser: "ad",
ExecutableName: "atlantis",
}
cases := []struct {
vcs models.VCSHostType
Expand Down
6 changes: 3 additions & 3 deletions server/events/project_command_builder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ projects:
NewDefaultWorkingDirLocker(),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{},
&CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -834,7 +834,7 @@ projects:
NewDefaultWorkingDirLocker(),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{},
&CommentParser{ExecutableName: "atlantis"},
false,
true,
"",
Expand Down Expand Up @@ -1067,7 +1067,7 @@ workflows:
NewDefaultWorkingDirLocker(),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{},
&CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down
22 changes: 11 additions & 11 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -424,7 +424,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
true,
"",
Expand Down Expand Up @@ -597,7 +597,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
true,
"",
Expand Down Expand Up @@ -767,7 +767,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -859,7 +859,7 @@ func TestDefaultProjectCommandBuilder_BuildMultiApply(t *testing.T) {
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -945,7 +945,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -1025,7 +1025,7 @@ func TestDefaultProjectCommandBuilder_EscapeArgs(t *testing.T) {
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -1230,7 +1230,7 @@ projects:
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -1320,7 +1320,7 @@ parallel_plan: true`,
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
true,
false,
"",
Expand Down Expand Up @@ -1380,7 +1380,7 @@ func TestDefaultProjectCommandBuilder_WithPolicyCheckEnabled_BuildAutoplanComman
events.NewDefaultWorkingDirLocker(),
globalCfg,
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down Expand Up @@ -1463,7 +1463,7 @@ func TestDefaultProjectCommandBuilder_BuildVersionCommand(t *testing.T) {
events.NewDefaultWorkingDirLocker(),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
&events.CommentParser{ExecutableName: "atlantis"},
false,
false,
"",
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
BitbucketUser: userConfig.BitbucketUser,
AzureDevopsUser: userConfig.AzureDevopsUser,
ApplyDisabled: userConfig.DisableApply,
ExecutableName: userConfig.ExecutableName,
}
defaultTfVersion := terraformClient.DefaultVersion()
pendingPlanFinder := &events.DefaultPendingPlanFinder{}
Expand Down
1 change: 1 addition & 0 deletions server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type UserConfig struct {
EnablePolicyChecksFlag bool `mapstructure:"enable-policy-checks"`
EnableRegExpCmd bool `mapstructure:"enable-regexp-cmd"`
EnableDiffMarkdownFormat bool `mapstructure:"enable-diff-markdown-format"`
ExecutableName string `mapstructure:"executable-name"`
GithubAllowMergeableBypassApply bool `mapstructure:"gh-allow-mergeable-bypass-apply"`
GithubHostname string `mapstructure:"gh-hostname"`
GithubToken string `mapstructure:"gh-token"`
Expand Down