Skip to content

fix: API Controller CommentCommand.Name defaults to Apply instead of Plan#6090

Merged
jamengual merged 8 commits intorunatlantis:mainfrom
rjmsilveira:fix-pre-workflow-wrong-action
Jan 26, 2026
Merged

fix: API Controller CommentCommand.Name defaults to Apply instead of Plan#6090
jamengual merged 8 commits intorunatlantis:mainfrom
rjmsilveira:fix-pre-workflow-wrong-action

Conversation

@rjmsilveira
Copy link
Contributor

@rjmsilveira rjmsilveira commented Jan 19, 2026

What

Fixed a bug in the API controller where CommentCommand.Name was not being set when creating commands, causing the command type to default to Apply (value 0) instead of the intended command type (Plan or Apply).

Changes:

  • Added cmdName command.Name parameter to the getCommands() function
  • Set Name: cmdName on each CommentCommand struct created for projects and paths
  • Updated apiPlan() to pass command.Plan to getCommands()
  • Updated apiApply() to pass command.Apply to getCommands()

Why

The command.Name type is defined using iota:

const (
    Apply Name = iota  // = 0
    Plan               // = 1
    // ...
)

When the API controller created CommentCommand structs, the Name field was never set:

cc = append(cc, &events.CommentCommand{
    ProjectName: project,  // Name field missing!
})

Since Go initializes unset fields to their zero value, Name defaulted to 0, which is Apply. This caused:

  • API plan requests to have their CommentCommand.Name incorrectly set to Apply
  • The downstream BuildPlanCommands() function received the wrong command name via cmd.CommandName()
  • Pre-workflow hooks and other logic that depends on the command name would behave incorrectly

How the issue was detected

The bug was discovered when using pre_workflow_hooks with the commands filter set to plan:

repos:
  - id: /.*/
    pre_workflow_hooks:
      - run: bash /path/to/script.sh
        description: Running script for plan only
        commands: plan

When calling the API /api/plan endpoint, the pre-workflow hook was not executing even though it should only run for plan commands. Investigation revealed that the CommentCommand.Name was incorrectly set to Apply (the zero value) instead of Plan, causing the commands: plan filter to skip the hook.

Tests

Existing tests in server/controllers/api_controller_test.go cover this functionality:

  • TestAPIController_Plan - verifies BuildPlanCommands is called correctly
  • TestAPIController_Apply - verifies BuildApplyCommands is called correctly
  • Both tests verify the correct number of calls and successful responses

New tests added to specifically verify the CommentCommand.Name is correctly set for pre-workflow hooks:

  • TestAPIController_Plan_PreWorkflowHooksReceiveCorrectCommand - verifies that when calling the Plan API endpoint, the pre-workflow hooks receive a CommentCommand with Name set to command.Plan
  • TestAPIController_Apply_PreWorkflowHooksReceiveCorrectCommand - verifies that when calling the Apply API endpoint, the pre-workflow hooks receive the correct CommentCommand.Name for each phase (command.Plan for the plan phase, command.Apply for the apply phase)

These tests use pegomock's GetCapturedArguments() to capture the actual CommentCommand passed to RunPreHooks() and assert that the Name field is correctly set.

To run the tests:

go test ./server/controllers/... -run "TestAPIController" -v

References

  • command.Name iota definition: server/events/command/name.go
  • CommentCommand struct: server/events/comment_command.go
  • BuildPlanCommands uses cmd.CommandName(): server/events/project_command_builder.go:279
  • getCommands() function: server/controllers/api_controller.go:59
  • Pre-workflow hooks commands filter: server/events/pre_workflow_hooks_command_runner.go

ricardosilveiraolx and others added 4 commits October 31, 2025 16:26
… be applied

Signed-off-by: Ricardo Silveira <ricardo.silveira@olx.com>
Signed-off-by: Ricardo Silveira <ricardo.silveira@olx.com>
@dosubot dosubot bot added bug Something isn't working go Pull requests that update Go code labels Jan 19, 2026
Signed-off-by: Ricardo Silveira <ricardo.silveira@olx.com>
@rjmsilveira rjmsilveira changed the title Fix pre workflow wrong action Fix: API Controller CommentCommand.Name defaults to Apply instead of Plan Jan 19, 2026
@rjmsilveira rjmsilveira changed the title Fix: API Controller CommentCommand.Name defaults to Apply instead of Plan fix: API Controller CommentCommand.Name defaults to Apply instead of Plan Jan 19, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 26, 2026
@jamengual jamengual merged commit 99db910 into runatlantis:main Jan 26, 2026
39 of 40 checks passed
@jamengual
Copy link
Contributor

Thanks @rjmsilveira for the contribution

@albertorm95 albertorm95 mentioned this pull request Feb 8, 2026
aidansteele pushed a commit to aidansteele/atlantis that referenced this pull request Mar 12, 2026
…Plan (runatlantis#6090)

Signed-off-by: Ricardo Silveira <ricardo.silveira@olx.com>
Co-authored-by: Ricardo Silveira <ricardo.silveira@olx.com>
Co-authored-by: PePe Amengual <2208324+jamengual@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update Go code lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants