Skip to content

Commit

Permalink
feat: Add Command Name Environment Variable to Pre/Post Workflow Exec…
Browse files Browse the repository at this point in the history
…utions (runatlantis#3571)

* Add Workflow Command Name env var

* Update tests

* Update command runner

---------

Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
2 people authored and terakoya76 committed Dec 31, 2024
1 parent cc24f2a commit 77e7ce5
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions runatlantis.io/docs/post-workflow-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
:::
1 change: 1 addition & 0 deletions runatlantis.io/docs/pre-workflow-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
:::

1 change: 1 addition & 0 deletions server/core/runtime/post_workflow_hook_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion server/core/runtime/post_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
1 change: 1 addition & 0 deletions server/core/runtime/pre_workflow_hook_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion server/core/runtime/pre_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
7 changes: 5 additions & 2 deletions server/events/command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions server/events/post_workflow_hooks_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (w *DefaultPostWorkflowHooksCommandRunner) RunPostHooks(
User: user,
Verbose: false,
EscapedCommentArgs: escapedArgs,
CommandName: cmd.Name.String(),
},
postWorkflowHooks, repoDir)

Expand Down
31 changes: 18 additions & 13 deletions server/events/post_workflow_hooks_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand All @@ -244,6 +248,7 @@ func TestRunPostHooks_Clone(t *testing.T) {
}

cmd := &events.CommentCommand{
Name: command.Plan,
Flags: []string{"comment", "args"},
}

Expand Down
1 change: 1 addition & 0 deletions server/events/pre_workflow_hooks_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks(ctx *command.Context,
User: user,
Verbose: false,
EscapedCommentArgs: escapedArgs,
CommandName: cmd.Name.String(),
},
preWorkflowHooks, repoDir)

Expand Down
28 changes: 17 additions & 11 deletions server/events/pre_workflow_hooks_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand All @@ -248,6 +253,7 @@ func TestRunPreHooks_Clone(t *testing.T) {
}

cmd := &events.CommentCommand{
Name: command.Plan,
Flags: []string{"comment", "args"},
}

Expand Down

0 comments on commit 77e7ce5

Please sign in to comment.