From 34f9f3f3489aa8a015bad633c7a8f240c5b7d342 Mon Sep 17 00:00:00 2001
From: ValdirGuerra <valdir.guerra@gmail.com>
Date: Tue, 2 Jun 2020 19:42:55 -0400
Subject: [PATCH 1/2] Add support for disable autoplan from server config

---
 cmd/server.go                               |  5 +++++
 cmd/server_test.go                          |  1 +
 runatlantis.io/docs/server-configuration.md |  7 +++++++
 server/events/command_runner.go             |  4 ++++
 server/events/command_runner_test.go        | 16 ++++++++++++++++
 server/server.go                            |  1 +
 server/user_config.go                       |  1 +
 7 files changed, 35 insertions(+)

diff --git a/cmd/server.go b/cmd/server.go
index 1aecb70045..59a66073be 100644
--- a/cmd/server.go
+++ b/cmd/server.go
@@ -53,6 +53,7 @@ const (
 	DataDirFlag                = "data-dir"
 	DefaultTFVersionFlag       = "default-tf-version"
 	DisableApplyAllFlag        = "disable-apply-all"
+	DisableAutoplanFlag        = "disable-autoplan"
 	DisableMarkdownFoldingFlag = "disable-markdown-folding"
 	GHHostnameFlag             = "gh-hostname"
 	GHTokenFlag                = "gh-token"
@@ -274,6 +275,10 @@ var boolFlags = map[string]boolFlag{
 		description:  "Disable \"atlantis apply\" command so a specific project/workspace/directory has to be specified for applies.",
 		defaultValue: false,
 	},
+	DisableAutoplanFlag: {
+		description:  "Disable atlantis auto planning feature",
+		defaultValue: false,
+	},
 	AllowDraftPRs: {
 		description:  "Enable autoplan for Github Draft Pull Requests",
 		defaultValue: false,
diff --git a/cmd/server_test.go b/cmd/server_test.go
index 9569c5ac2a..0e8bfaa07c 100644
--- a/cmd/server_test.go
+++ b/cmd/server_test.go
@@ -94,6 +94,7 @@ var testFlags = map[string]interface{}{
 	TFETokenFlag:               "my-token",
 	VCSStatusName:              "my-status",
 	WriteGitCredsFlag:          true,
+	DisableAutoplanFlag:        false,
 }
 
 func TestExecute_Defaults(t *testing.T) {
diff --git a/runatlantis.io/docs/server-configuration.md b/runatlantis.io/docs/server-configuration.md
index af821bad2a..a85e37531f 100644
--- a/runatlantis.io/docs/server-configuration.md
+++ b/runatlantis.io/docs/server-configuration.md
@@ -208,6 +208,13 @@ Values are chosen in this order:
   Disable \"atlantis apply\" command so a specific project/workspace/directory has to
   be specified for applies.
 
+* ### `--disable-autoplan`
+  ```bash
+  atlantis server --disable-autoplan
+  ```
+  Disable atlantis auto planning    
+
+
 * ### `--gh-hostname`
   ```bash
   atlantis server --gh-hostname="my.github.enterprise.com"
diff --git a/server/events/command_runner.go b/server/events/command_runner.go
index 910885267d..e0400054ed 100644
--- a/server/events/command_runner.go
+++ b/server/events/command_runner.go
@@ -76,6 +76,7 @@ type DefaultCommandRunner struct {
 	GitlabMergeRequestGetter GitlabMergeRequestGetter
 	CommitStatusUpdater      CommitStatusUpdater
 	DisableApplyAll          bool
+	DisableAutoplan          bool
 	EventParser              EventParsing
 	MarkdownRenderer         *MarkdownRenderer
 	Logger                   logging.SimpleLogging
@@ -130,6 +131,9 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
 	if !c.validateCtxAndComment(ctx) {
 		return
 	}
+	if c.DisableAutoplan {
+		return
+	}
 
 	projectCmds, err := c.ProjectCommandBuilder.BuildAutoplanCommands(ctx)
 	if err != nil {
diff --git a/server/events/command_runner_test.go b/server/events/command_runner_test.go
index be5cbc70f0..45d35fa7d9 100644
--- a/server/events/command_runner_test.go
+++ b/server/events/command_runner_test.go
@@ -196,6 +196,22 @@ func TestRunCommentCommand_DisableApplyAllDisabled(t *testing.T) {
 	vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, "**Error:** Running `atlantis apply` without flags is disabled. You must specify which project to apply via the `-d <dir>`, `-w <workspace>` or `-p <project name>` flags.", "apply")
 }
 
+func TestRunCommentCommand_DisableDisableAutoplan(t *testing.T) {
+	t.Log("if \"DisableAutoplan is true\" are disabled and we are silencing return and do not comment with error")
+	setup(t)
+	ch.DisableAutoplan = true
+	defer func() { ch.DisableAutoplan = false }()
+
+	When(projectCommandBuilder.BuildAutoplanCommands(matchers.AnyPtrToEventsCommandContext())).
+		ThenReturn([]models.ProjectCommandContext{
+			{},
+			{},
+		}, nil)
+
+	ch.RunAutoplanCommand(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
+	projectCommandBuilder.VerifyWasCalled(Never()).BuildAutoplanCommands(matchers.AnyPtrToEventsCommandContext())
+}
+
 func TestRunCommentCommand_ClosedPull(t *testing.T) {
 	t.Log("if a command is run on a closed pull request atlantis should" +
 		" comment saying that this is not allowed")
diff --git a/server/server.go b/server/server.go
index 9a33e703ac..b7ce45978c 100644
--- a/server/server.go
+++ b/server/server.go
@@ -369,6 +369,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
 		SilenceForkPRErrorsFlag:  config.SilenceForkPRErrorsFlag,
 		SilenceVCSStatusNoPlans:  userConfig.SilenceVCSStatusNoPlans,
 		DisableApplyAll:          userConfig.DisableApplyAll,
+		DisableAutoplan:          userConfig.DisableAutoplan,
 		ProjectCommandBuilder: &events.DefaultProjectCommandBuilder{
 			ParserValidator:   validator,
 			ProjectFinder:     &events.DefaultProjectFinder{},
diff --git a/server/user_config.go b/server/user_config.go
index 04acc6d6f0..60e32330f5 100644
--- a/server/user_config.go
+++ b/server/user_config.go
@@ -23,6 +23,7 @@ type UserConfig struct {
 	CheckoutStrategy           string `mapstructure:"checkout-strategy"`
 	DataDir                    string `mapstructure:"data-dir"`
 	DisableApplyAll            bool   `mapstructure:"disable-apply-all"`
+	DisableAutoplan            bool   `mapstructure:"disable-autoplan"`
 	DisableMarkdownFolding     bool   `mapstructure:"disable-markdown-folding"`
 	GithubHostname             string `mapstructure:"gh-hostname"`
 	GithubToken                string `mapstructure:"gh-token"`

From 1b374fa3e600d4d2da7d060065481dc6518b0ef0 Mon Sep 17 00:00:00 2001
From: Luke Kysow <1034429+lkysow@users.noreply.github.com>
Date: Tue, 18 Aug 2020 15:53:03 -0700
Subject: [PATCH 2/2] Small refactor to disable-autoplan

---
 cmd/server_test.go                          | 2 +-
 runatlantis.io/docs/server-configuration.md | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/cmd/server_test.go b/cmd/server_test.go
index 0e8bfaa07c..987adbb2c5 100644
--- a/cmd/server_test.go
+++ b/cmd/server_test.go
@@ -94,7 +94,7 @@ var testFlags = map[string]interface{}{
 	TFETokenFlag:               "my-token",
 	VCSStatusName:              "my-status",
 	WriteGitCredsFlag:          true,
-	DisableAutoplanFlag:        false,
+	DisableAutoplanFlag:        true,
 }
 
 func TestExecute_Defaults(t *testing.T) {
diff --git a/runatlantis.io/docs/server-configuration.md b/runatlantis.io/docs/server-configuration.md
index a85e37531f..dd100aa6df 100644
--- a/runatlantis.io/docs/server-configuration.md
+++ b/runatlantis.io/docs/server-configuration.md
@@ -214,7 +214,6 @@ Values are chosen in this order:
   ```
   Disable atlantis auto planning    
 
-
 * ### `--gh-hostname`
   ```bash
   atlantis server --gh-hostname="my.github.enterprise.com"