From 8612bb85e879e5450b104e101447cc1ab6c96b5f Mon Sep 17 00:00:00 2001 From: X-Guardian Date: Mon, 3 Jul 2023 20:05:36 +0100 Subject: [PATCH 1/3] Add Workflow Command Name env var --- runatlantis.io/docs/post-workflow-hooks.md | 1 + runatlantis.io/docs/pre-workflow-hooks.md | 1 + server/core/runtime/post_workflow_hook_runner.go | 1 + server/core/runtime/pre_workflow_hook_runner.go | 1 + server/events/models/models.go | 2 ++ server/events/post_workflow_hooks_command_runner.go | 1 + server/events/pre_workflow_hooks_command_runner.go | 1 + 7 files changed, 8 insertions(+) diff --git a/runatlantis.io/docs/post-workflow-hooks.md b/runatlantis.io/docs/post-workflow-hooks.md index 0837275f23..ac4341b71e 100644 --- a/runatlantis.io/docs/post-workflow-hooks.md +++ b/runatlantis.io/docs/post-workflow-hooks.md @@ -77,5 +77,6 @@ command](custom-workflows.html#custom-run-command). * `USER_NAME` - Username of the VCS user running command, ex. `acme-user`. During an autoplan, the user will be the Atlantis API user, ex. `atlantis`. * `COMMENT_ARGS` - Any additional flags passed in the comment on the pull request. Flags are separated by commas and every character is escaped, ex. `atlantis plan -- arg1 arg2` will result in `COMMENT_ARGS=\a\r\g\1,\a\r\g\2`. + * `COMMAND_NAME` - The name of the command that is being executed, i.e. `plan`, `apply` etc. * `OUTPUT_STATUS_FILE` - An output file to customize the success or failure status. ex. `echo 'failure' > $OUTPUT_STATUS_FILE`. ::: diff --git a/runatlantis.io/docs/pre-workflow-hooks.md b/runatlantis.io/docs/pre-workflow-hooks.md index 5d5d467f27..1dde7c3a8e 100644 --- a/runatlantis.io/docs/pre-workflow-hooks.md +++ b/runatlantis.io/docs/pre-workflow-hooks.md @@ -61,6 +61,7 @@ command](custom-workflows.html#custom-run-command). * `USER_NAME` - Username of the VCS user running command, ex. `acme-user`. During an autoplan, the user will be the Atlantis API user, ex. `atlantis`. * `COMMENT_ARGS` - Any additional flags passed in the comment on the pull request. Flags are separated by commas and every character is escaped, ex. `atlantis plan -- arg1 arg2` will result in `COMMENT_ARGS=\a\r\g\1,\a\r\g\2`. + * `COMMAND_NAME` - The name of the command that is being executed, i.e. `plan`, `apply` etc. * `OUTPUT_STATUS_FILE` - An output file to customize the success or failure status. ex. `echo 'failure' > $OUTPUT_STATUS_FILE`. ::: diff --git a/server/core/runtime/post_workflow_hook_runner.go b/server/core/runtime/post_workflow_hook_runner.go index 2261f72ceb..1835d50bb7 100644 --- a/server/core/runtime/post_workflow_hook_runner.go +++ b/server/core/runtime/post_workflow_hook_runner.go @@ -42,6 +42,7 @@ func (wh DefaultPostWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContex "PULL_URL": ctx.Pull.URL, "USER_NAME": ctx.User.Username, "OUTPUT_STATUS_FILE": outputFilePath, + "COMMAND_NAME": ctx.CommandName, } finalEnvVars := baseEnvVars diff --git a/server/core/runtime/pre_workflow_hook_runner.go b/server/core/runtime/pre_workflow_hook_runner.go index 04d171062a..c80542c5e1 100644 --- a/server/core/runtime/pre_workflow_hook_runner.go +++ b/server/core/runtime/pre_workflow_hook_runner.go @@ -42,6 +42,7 @@ func (wh DefaultPreWorkflowHookRunner) Run(ctx models.WorkflowHookCommandContext "PULL_URL": ctx.Pull.URL, "USER_NAME": ctx.User.Username, "OUTPUT_STATUS_FILE": outputFilePath, + "COMMAND_NAME": ctx.CommandName, } finalEnvVars := baseEnvVars diff --git a/server/events/models/models.go b/server/events/models/models.go index 43c2a78da5..c5d89b99b0 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -625,6 +625,8 @@ type WorkflowHookCommandContext struct { EscapedCommentArgs []string // UUID for reference HookID string + // The name of the command that is being executed, i.e. 'plan', 'apply' etc. + CommandName string } // PlanSuccessStats holds stats for a plan. diff --git a/server/events/post_workflow_hooks_command_runner.go b/server/events/post_workflow_hooks_command_runner.go index ec8ef12aa3..12f7e9e1b8 100644 --- a/server/events/post_workflow_hooks_command_runner.go +++ b/server/events/post_workflow_hooks_command_runner.go @@ -85,6 +85,7 @@ func (w *DefaultPostWorkflowHooksCommandRunner) RunPostHooks( User: user, Verbose: false, EscapedCommentArgs: escapedArgs, + CommandName: cmd.Name.String(), }, postWorkflowHooks, repoDir) diff --git a/server/events/pre_workflow_hooks_command_runner.go b/server/events/pre_workflow_hooks_command_runner.go index ccd513a410..1cc2b4daba 100644 --- a/server/events/pre_workflow_hooks_command_runner.go +++ b/server/events/pre_workflow_hooks_command_runner.go @@ -83,6 +83,7 @@ func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks(ctx *command.Context, User: user, Verbose: false, EscapedCommentArgs: escapedArgs, + CommandName: cmd.Name.String(), }, preWorkflowHooks, repoDir) From 4dc1c118de3463194b2efd4a077a52c3a0704e37 Mon Sep 17 00:00:00 2001 From: X-Guardian Date: Mon, 3 Jul 2023 21:16:48 +0100 Subject: [PATCH 2/3] Update tests --- .../runtime/post_workflow_hook_runner_test.go | 3 +- .../runtime/pre_workflow_hook_runner_test.go | 3 +- ...post_workflow_hooks_command_runner_test.go | 31 +++++++++++-------- .../pre_workflow_hooks_command_runner_test.go | 28 ++++++++++------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/server/core/runtime/post_workflow_hook_runner_test.go b/server/core/runtime/post_workflow_hook_runner_test.go index 13e2561b53..d3ab1f5d1b 100644 --- a/server/core/runtime/post_workflow_hook_runner_test.go +++ b/server/core/runtime/post_workflow_hook_runner_test.go @@ -121,7 +121,8 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) { User: models.User{ Username: "acme-user", }, - Log: logger, + Log: logger, + CommandName: "plan", } _, desc, err := r.Run(ctx, c.Command, tmpDir) if c.ExpErr != "" { diff --git a/server/core/runtime/pre_workflow_hook_runner_test.go b/server/core/runtime/pre_workflow_hook_runner_test.go index 154d0d20c9..ad608954cc 100644 --- a/server/core/runtime/pre_workflow_hook_runner_test.go +++ b/server/core/runtime/pre_workflow_hook_runner_test.go @@ -121,7 +121,8 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) { User: models.User{ Username: "acme-user", }, - Log: logger, + Log: logger, + CommandName: "plan", } _, desc, err := r.Run(ctx, c.Command, tmpDir) if c.ExpErr != "" { diff --git a/server/events/post_workflow_hooks_command_runner_test.go b/server/events/post_workflow_hooks_command_runner_test.go index 061212d951..c812ef6708 100644 --- a/server/events/post_workflow_hooks_command_runner_test.go +++ b/server/events/post_workflow_hooks_command_runner_test.go @@ -67,13 +67,18 @@ func TestRunPostHooks_Clone(t *testing.T) { runtimeDesc := "" pCtx := models.WorkflowHookCommandContext{ - BaseRepo: testdata.GithubRepo, - HeadRepo: testdata.GithubRepo, - Pull: newPull, - Log: log, - User: testdata.User, - Verbose: false, - HookID: uuid.NewString(), + BaseRepo: testdata.GithubRepo, + HeadRepo: testdata.GithubRepo, + Pull: newPull, + Log: log, + User: testdata.User, + Verbose: false, + HookID: uuid.NewString(), + CommandName: "plan", + } + + cmd := &events.CommentCommand{ + Name: command.Plan, } t.Run("success hooks in cfg", func(t *testing.T) { @@ -100,8 +105,7 @@ func TestRunPostHooks_Clone(t *testing.T) { When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil) When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil) When(whPostWorkflowHookRunner.Run(pCtx, testHook.RunCommand, repoDir)).ThenReturn(result, runtimeDesc, nil) - - err := postWh.RunPostHooks(ctx, nil) + err := postWh.RunPostHooks(ctx, cmd) Ok(t, err) whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir)) @@ -128,7 +132,7 @@ func TestRunPostHooks_Clone(t *testing.T) { postWh.GlobalCfg = globalCfg - err := postWh.RunPostHooks(ctx, nil) + err := postWh.RunPostHooks(ctx, cmd) Ok(t, err) @@ -154,7 +158,7 @@ func TestRunPostHooks_Clone(t *testing.T) { When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(func() {}, errors.New("some error")) - err := postWh.RunPostHooks(ctx, nil) + err := postWh.RunPostHooks(ctx, cmd) Assert(t, err != nil, "error not nil") postWhWorkingDir.VerifyWasCalled(Never()).Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace) @@ -185,7 +189,7 @@ func TestRunPostHooks_Clone(t *testing.T) { When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil) When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, errors.New("some error")) - err := postWh.RunPostHooks(ctx, nil) + err := postWh.RunPostHooks(ctx, cmd) Assert(t, err != nil, "error not nil") @@ -218,7 +222,7 @@ func TestRunPostHooks_Clone(t *testing.T) { When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil) When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error")) - err := postWh.RunPostHooks(ctx, nil) + err := postWh.RunPostHooks(ctx, cmd) Assert(t, err != nil, "error not nil") Assert(t, *unlockCalled == true, "unlock function called") @@ -244,6 +248,7 @@ func TestRunPostHooks_Clone(t *testing.T) { } cmd := &events.CommentCommand{ + Name: command.Plan, Flags: []string{"comment", "args"}, } diff --git a/server/events/pre_workflow_hooks_command_runner_test.go b/server/events/pre_workflow_hooks_command_runner_test.go index aa5286f7de..e9660db6e2 100644 --- a/server/events/pre_workflow_hooks_command_runner_test.go +++ b/server/events/pre_workflow_hooks_command_runner_test.go @@ -70,12 +70,17 @@ func TestRunPreHooks_Clone(t *testing.T) { runtimeDesc := "" pCtx := models.WorkflowHookCommandContext{ - BaseRepo: testdata.GithubRepo, - HeadRepo: testdata.GithubRepo, - Pull: newPull, - Log: log, - User: testdata.User, - Verbose: false, + BaseRepo: testdata.GithubRepo, + HeadRepo: testdata.GithubRepo, + Pull: newPull, + Log: log, + User: testdata.User, + Verbose: false, + CommandName: "plan", + } + + cmd := &events.CommentCommand{ + Name: command.Plan, } t.Run("success hooks in cfg", func(t *testing.T) { @@ -103,7 +108,7 @@ func TestRunPreHooks_Clone(t *testing.T) { When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil) When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil) - err := preWh.RunPreHooks(ctx, nil) + err := preWh.RunPreHooks(ctx, cmd) Ok(t, err) whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir)) @@ -131,7 +136,7 @@ func TestRunPreHooks_Clone(t *testing.T) { preWh.GlobalCfg = globalCfg - err := preWh.RunPreHooks(ctx, nil) + err := preWh.RunPreHooks(ctx, cmd) Ok(t, err) @@ -158,7 +163,7 @@ func TestRunPreHooks_Clone(t *testing.T) { When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(func() {}, errors.New("some error")) - err := preWh.RunPreHooks(ctx, nil) + err := preWh.RunPreHooks(ctx, cmd) Assert(t, err != nil, "error not nil") preWhWorkingDir.VerifyWasCalled(Never()).Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace) @@ -189,7 +194,7 @@ func TestRunPreHooks_Clone(t *testing.T) { When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil) When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, errors.New("some error")) - err := preWh.RunPreHooks(ctx, nil) + err := preWh.RunPreHooks(ctx, cmd) Assert(t, err != nil, "error not nil") @@ -222,7 +227,7 @@ func TestRunPreHooks_Clone(t *testing.T) { When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil) When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error")) - err := preWh.RunPreHooks(ctx, nil) + err := preWh.RunPreHooks(ctx, cmd) Assert(t, err != nil, "error not nil") Assert(t, *unlockCalled == true, "unlock function called") @@ -248,6 +253,7 @@ func TestRunPreHooks_Clone(t *testing.T) { } cmd := &events.CommentCommand{ + Name: command.Plan, Flags: []string{"comment", "args"}, } From 3d81b3997b8de1a17ab51f5a0550da52a52ebb43 Mon Sep 17 00:00:00 2001 From: X-Guardian Date: Mon, 3 Jul 2023 21:42:36 +0100 Subject: [PATCH 3/3] Update command runner --- server/events/command_runner.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/events/command_runner.go b/server/events/command_runner.go index 680afb425b..e6d49e9a07 100644 --- a/server/events/command_runner.go +++ b/server/events/command_runner.go @@ -163,7 +163,10 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo return } - err = c.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, nil) + cmd := &CommentCommand{ + Name: command.Autoplan, + } + err = c.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, cmd) if err != nil { ctx.Log.Err("Error running pre-workflow hooks %s. Proceeding with %s command.", err, command.Plan) @@ -173,7 +176,7 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo autoPlanRunner.Run(ctx, nil) - err = c.PostWorkflowHooksCommandRunner.RunPostHooks(ctx, nil) + err = c.PostWorkflowHooksCommandRunner.RunPostHooks(ctx, cmd) if err != nil { ctx.Log.Err("Error running post-workflow hooks %s.", err)