Skip to content

Instantly share code, notes, and snippets.

@Mitsuwa
Created July 6, 2020 22:20
Show Gist options
  • Save Mitsuwa/6cbefc7631bba927baea62eef6c580c2 to your computer and use it in GitHub Desktop.
Save Mitsuwa/6cbefc7631bba927baea62eef6c580c2 to your computer and use it in GitHub Desktop.
Atlantis Squelch Diff Test
diff --git a/server/events/command_runner.go b/server/events/command_runner.go
index 1c4f0250..8a8a526f 100644
--- a/server/events/command_runner.go
+++ b/server/events/command_runner.go
@@ -23,6 +23,7 @@ import (
"github.com/remeh/sizedwaitgroup"
"github.com/runatlantis/atlantis/server/events/db"
"github.com/runatlantis/atlantis/server/events/models"
+ "github.com/runatlantis/atlantis/server/events/runtime"
"github.com/runatlantis/atlantis/server/events/vcs"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/recovery"
@@ -173,13 +174,22 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
c.deletePlans(ctx)
result.PlansDeleted = true
}
- c.updatePull(ctx, AutoplanCommand{}, result)
+
+ // Do not comment if our run commands exit with a SquelchExitCode
+ if result.Error != runtime.ErrSquelchCode {
+ c.updatePull(ctx, AutoplanCommand{}, result)
+ }
+
pullStatus, err := c.updateDB(ctx, ctx.Pull, result.ProjectResults)
if err != nil {
c.Logger.Err("writing results: %s", err)
}
- c.updateCommitStatus(ctx, models.PlanCommand, pullStatus)
+ // Do not update status if our run commands exit with a SquelchExitCode
+ if result.Error != runtime.ErrSquelchCode {
+ c.updateCommitStatus(ctx, models.PlanCommand, pullStatus)
+ }
+
}
// RunCommentCommand executes the command.
@@ -317,10 +327,13 @@ func (c *DefaultCommandRunner) RunCommentCommand(baseRepo models.Repo, maybeHead
result.PlansDeleted = true
}
- c.updatePull(
- ctx,
- cmd,
- result)
+ // Do not update pull if runtime.ErrSquelchCode is returned
+ if result.Error != runtime.ErrSquelchCode {
+ c.updatePull(
+ ctx,
+ cmd,
+ result)
+ }
pullStatus, err := c.updateDB(ctx, pull, result.ProjectResults)
if err != nil {
@@ -328,9 +341,12 @@ func (c *DefaultCommandRunner) RunCommentCommand(baseRepo models.Repo, maybeHead
return
}
- c.updateCommitStatus(ctx, cmd.Name, pullStatus)
+ // Do not update commit status if runtime.ErrSquelchCode is returned
+ if result.Error != runtime.ErrSquelchCode {
+ c.updateCommitStatus(ctx, cmd.Name, pullStatus)
+ }
- if cmd.Name == models.ApplyCommand && c.automergeEnabled(ctx, projectCmds) {
+ if cmd.Name == models.ApplyCommand && c.automergeEnabled(ctx, projectCmds) && result.Error != runtime.ErrSquelchCode {
c.automerge(ctx, pullStatus)
}
}
diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go
index e4d0015b..fa6d4663 100644
--- a/server/events/runtime/run_step_runner.go
+++ b/server/events/runtime/run_step_runner.go
@@ -1,6 +1,7 @@
package runtime
import (
+ "errors"
"fmt"
"os"
"os/exec"
@@ -19,6 +20,13 @@ type RunStepRunner struct {
TerraformBinDir string
}
+var (
+ // ErrSquelchCode is returned when commands from Run() exit with code == SquelchExitCode
+ ErrSquelchCode = errors.New("Comment Squelch Exit Code Detected")
+ // SquelchExitCode
+ SquelchExitCode int = 66
+)
+
func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, path string, envs map[string]string) (string, error) {
tfVersion := r.DefaultTFVersion
if ctx.TerraformVersion != nil {
@@ -66,11 +74,14 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()
- if err != nil {
+ if err != nil && cmd.ProcessState.ExitCode() == SquelchExitCode {
+ return string(out), ErrSquelchCode
+ } else if err != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, command, path, out)
ctx.Log.Debug("error: %s", err)
return "", err
}
ctx.Log.Info("successfully ran %q in %q", command, path)
+
return string(out), nil
}
diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go
index f662fbf1..58f25337 100644
--- a/server/events/runtime/run_step_runner_test.go
+++ b/server/events/runtime/run_step_runner_test.go
@@ -79,6 +79,11 @@ func TestRunStepRunner_Run(t *testing.T) {
Command: "echo args=$COMMENT_ARGS",
ExpOut: "args=-target=resource1,-target=resource2\n",
},
+ {
+ Command: fmt.Sprintf("exit %d", runtime.SquelchExitCode),
+ ExpOut: fmt.Sprintf("exit status %d: running \"exit %d\" in", runtime.SquelchExitCode, runtime.SquelchExitCode),
+ ExpErr: runtime.ErrSquelchCode.Error(),
+ },
}
for _, c := range cases {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment