Skip to content

Commit

Permalink
Merge branch 'main' into issue-1852
Browse files Browse the repository at this point in the history
  • Loading branch information
jamengual authored May 24, 2023
2 parents 2f2a8f1 + 4eee2e9 commit a31d3ab
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 63 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/hashicorp/go-getter/v2 v2.2.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20230413234026-f1617e8a5fcc
github.com/hashicorp/terraform-config-inspect v0.0.0-20230522202058-dbe9bfcbfe7a
github.com/kr/pretty v0.3.1
github.com/mcdafydd/go-azuredevops v0.12.1
github.com/microcosm-cc/bluemonday v1.0.24
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.16.2 h1:mpkHZh/Tv+xet3sy3F9Ld4FyI2tUpWe9x3XtPx9f1a0=
github.com/hashicorp/hcl/v2 v2.16.2/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
github.com/hashicorp/terraform-config-inspect v0.0.0-20230413234026-f1617e8a5fcc h1:Nu4cU0SZXU79TSjpjV6dmuBneDUFphA5EJjmetwi8sE=
github.com/hashicorp/terraform-config-inspect v0.0.0-20230413234026-f1617e8a5fcc/go.mod h1:l8HcFPm9cQh6Q0KSWoYPiePqMvRFenybP1CH2MjKdlg=
github.com/hashicorp/terraform-config-inspect v0.0.0-20230522202058-dbe9bfcbfe7a h1:zKVsrHhIOWiXHYqFrOXfHacOOVDMYv+v/Wx8DUeyySs=
github.com/hashicorp/terraform-config-inspect v0.0.0-20230522202058-dbe9bfcbfe7a/go.mod h1:l8HcFPm9cQh6Q0KSWoYPiePqMvRFenybP1CH2MjKdlg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
Expand Down
16 changes: 16 additions & 0 deletions runatlantis.io/docs/custom-workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,21 @@ workflows:
- env:
name: TERRAGRUNT_TFPATH
command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
- env:
# Reduce Terraform suggestion output
name: TF_IN_AUTOMATION
value: 'true'
- run: terragrunt plan -input=false -out=$PLANFILE
- run: terragrunt show -json $PLANFILE > $SHOWFILE
apply:
steps:
- env:
name: TERRAGRUNT_TFPATH
command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
- env:
# Reduce Terraform suggestion output
name: TF_IN_AUTOMATION
value: 'true'
- run: terragrunt apply -input=false $PLANFILE
```

Expand All @@ -285,12 +293,20 @@ workflows:
- env:
name: TERRAGRUNT_TFPATH
command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
- env:
# Reduce Terraform suggestion output
name: TF_IN_AUTOMATION
value: 'true'
- run: terragrunt plan -out $PLANFILE
apply:
steps:
- env:
name: TERRAGRUNT_TFPATH
command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
- env:
# Reduce Terraform suggestion output
name: TF_IN_AUTOMATION
value: 'true'
- run: terragrunt apply $PLANFILE
```

Expand Down
10 changes: 5 additions & 5 deletions server/core/runtime/post_workflow_hook_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func (wh DefaultPostWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContex
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()

wh.OutputHandler.SendWorkflowHook(ctx, string(out), false)
wh.OutputHandler.SendWorkflowHook(ctx, "\n", true)

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
return string(out), "", err
}

// Read the value from the "outputFilePath" file
Expand All @@ -67,13 +70,10 @@ func (wh DefaultPostWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContex
if customStatusErr != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, command, path, out)
ctx.Log.Debug("error: %s", err)
return "", "", err
return string(out), "", err
}
}

wh.OutputHandler.SendWorkflowHook(ctx, string(out), false)
wh.OutputHandler.SendWorkflowHook(ctx, "\n", true)

ctx.Log.Info("successfully ran %q in %q", command, path)
return string(out), strings.Trim(string(customStatusOut), "\n"), nil
}
68 changes: 46 additions & 22 deletions server/core/runtime/post_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/core/runtime"
runtimematchers "github.com/runatlantis/atlantis/server/core/runtime/mocks/matchers"
"github.com/runatlantis/atlantis/server/core/terraform/mocks"
matchers2 "github.com/runatlantis/atlantis/server/core/terraform/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
Expand All @@ -23,43 +24,63 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {
ExpDescription string
}{
{
Command: "",
ExpOut: "",
Command: "",
ExpOut: "",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo hi",
ExpOut: "hi\n",
Command: "echo hi",
ExpOut: "hi\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: `printf \'your main.tf file does not provide default region.\\ncheck\'`,
ExpOut: `'your`,
Command: `printf \'your main.tf file does not provide default region.\\ncheck\'`,
ExpOut: `'your`,
ExpErr: "",
ExpDescription: "",
},
{
Command: `printf 'your main.tf file does not provide default region.\ncheck'`,
ExpOut: "your main.tf file does not provide default region.\ncheck",
Command: `printf 'your main.tf file does not provide default region.\ncheck'`,
ExpOut: "your main.tf file does not provide default region.\ncheck",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo 'a",
ExpErr: "exit status 2: running \"echo 'a\" in",
Command: "echo 'a",
ExpOut: "sh: 1: Syntax error: Unterminated quoted string\n",
ExpErr: "exit status 2: running \"echo 'a\" in",
ExpDescription: "",
},
{
Command: "echo hi >> file && cat file",
ExpOut: "hi\n",
Command: "echo hi >> file && cat file",
ExpOut: "hi\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "lkjlkj",
ExpErr: "exit status 127: running \"lkjlkj\" in",
Command: "lkjlkj",
ExpOut: "sh: 1: lkjlkj: not found\n",
ExpErr: "exit status 127: running \"lkjlkj\" in",
ExpDescription: "",
},
{
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME head_commit=$HEAD_COMMIT base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_url=$PULL_URL pull_author=$PULL_AUTHOR",
ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat head_commit=12345abcdef base_branch_name=main pull_num=2 pull_url=https://github.com/runatlantis/atlantis/pull/2 pull_author=acme\n",
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME head_commit=$HEAD_COMMIT base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_url=$PULL_URL pull_author=$PULL_AUTHOR",
ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat head_commit=12345abcdef base_branch_name=main pull_num=2 pull_url=https://github.com/runatlantis/atlantis/pull/2 pull_author=acme\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo something > $OUTPUT_STATUS_FILE",
ExpOut: "",
ExpErr: "",
ExpDescription: "something",
},
}
Expand All @@ -77,8 +98,9 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmpDir := t.TempDir()

r := runtime.DefaultPostWorkflowHookRunner{
OutputHandler: jobmocks.NewMockProjectCommandOutputHandler(),
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()
r := runtime.DefaultPreWorkflowHookRunner{
OutputHandler: projectCmdOutputHandler,
}
t.Run(c.Command, func(t *testing.T) {
ctx := models.WorkflowHookCommandContext{
Expand Down Expand Up @@ -106,15 +128,17 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {
out, desc, err := r.Run(ctx, c.Command, tmpDir)
if c.ExpErr != "" {
ErrContains(t, c.ExpErr, err)
return
} else {
Ok(t, err)
}
Ok(t, err)
// Replace $DIR in the exp with the actual temp dir. We do this
// here because when constructing the cases we don't yet know the
// temp dir.
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
Equals(t, expOut, out)
Equals(t, c.ExpDescription, desc)
projectCmdOutputHandler.VerifyWasCalledOnce().SendWorkflowHook(
runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(expOut), EqBool(false))
})
}
}
10 changes: 5 additions & 5 deletions server/core/runtime/pre_workflow_hook_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func (wh DefaultPreWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContext
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()

wh.OutputHandler.SendWorkflowHook(ctx, string(out), false)
wh.OutputHandler.SendWorkflowHook(ctx, "\n", true)

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
return string(out), "", err
}

// Read the value from the "outputFilePath" file
Expand All @@ -67,13 +70,10 @@ func (wh DefaultPreWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContext
if customStatusErr != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, command, path, out)
ctx.Log.Debug("error: %s", err)
return "", "", err
return string(out), "", err
}
}

wh.OutputHandler.SendWorkflowHook(ctx, string(out), false)
wh.OutputHandler.SendWorkflowHook(ctx, "\n", true)

ctx.Log.Info("successfully ran %q in %q", command, path)
return string(out), strings.Trim(string(customStatusOut), "\n"), nil
}
66 changes: 45 additions & 21 deletions server/core/runtime/pre_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/core/runtime"
runtimematchers "github.com/runatlantis/atlantis/server/core/runtime/mocks/matchers"
"github.com/runatlantis/atlantis/server/core/terraform/mocks"
matchers2 "github.com/runatlantis/atlantis/server/core/terraform/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
Expand All @@ -23,43 +24,63 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) {
ExpDescription string
}{
{
Command: "",
ExpOut: "",
Command: "",
ExpOut: "",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo hi",
ExpOut: "hi\n",
Command: "echo hi",
ExpOut: "hi\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: `printf \'your main.tf file does not provide default region.\\ncheck\'`,
ExpOut: `'your`,
Command: `printf \'your main.tf file does not provide default region.\\ncheck\'`,
ExpOut: `'your`,
ExpErr: "",
ExpDescription: "",
},
{
Command: `printf 'your main.tf file does not provide default region.\ncheck'`,
ExpOut: "your main.tf file does not provide default region.\ncheck",
Command: `printf 'your main.tf file does not provide default region.\ncheck'`,
ExpOut: "your main.tf file does not provide default region.\ncheck",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo 'a",
ExpErr: "exit status 2: running \"echo 'a\" in",
Command: "echo 'a",
ExpOut: "sh: 1: Syntax error: Unterminated quoted string\n",
ExpErr: "exit status 2: running \"echo 'a\" in",
ExpDescription: "",
},
{
Command: "echo hi >> file && cat file",
ExpOut: "hi\n",
Command: "echo hi >> file && cat file",
ExpOut: "hi\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "lkjlkj",
ExpErr: "exit status 127: running \"lkjlkj\" in",
Command: "lkjlkj",
ExpOut: "sh: 1: lkjlkj: not found\n",
ExpErr: "exit status 127: running \"lkjlkj\" in",
ExpDescription: "",
},
{
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME head_commit=$HEAD_COMMIT base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_url=$PULL_URL pull_author=$PULL_AUTHOR",
ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat head_commit=12345abcdef base_branch_name=main pull_num=2 pull_url=https://github.com/runatlantis/atlantis/pull/2 pull_author=acme\n",
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME head_commit=$HEAD_COMMIT base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_url=$PULL_URL pull_author=$PULL_AUTHOR",
ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat head_commit=12345abcdef base_branch_name=main pull_num=2 pull_url=https://github.com/runatlantis/atlantis/pull/2 pull_author=acme\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
ExpErr: "",
ExpDescription: "",
},
{
Command: "echo something > $OUTPUT_STATUS_FILE",
ExpOut: "",
ExpErr: "",
ExpDescription: "something",
},
}
Expand All @@ -77,8 +98,9 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmpDir := t.TempDir()

projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()
r := runtime.DefaultPreWorkflowHookRunner{
OutputHandler: jobmocks.NewMockProjectCommandOutputHandler(),
OutputHandler: projectCmdOutputHandler,
}
t.Run(c.Command, func(t *testing.T) {
ctx := models.WorkflowHookCommandContext{
Expand Down Expand Up @@ -106,15 +128,17 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) {
out, desc, err := r.Run(ctx, c.Command, tmpDir)
if c.ExpErr != "" {
ErrContains(t, c.ExpErr, err)
return
} else {
Ok(t, err)
}
Ok(t, err)
// Replace $DIR in the exp with the actual temp dir. We do this
// here because when constructing the cases we don't yet know the
// temp dir.
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
Equals(t, expOut, out)
Equals(t, c.ExpDescription, desc)
projectCmdOutputHandler.VerifyWasCalledOnce().SendWorkflowHook(
runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(expOut), EqBool(false))
})
}
}
Loading

0 comments on commit a31d3ab

Please sign in to comment.