Skip to content

fix: POST /api/plan panics when ATLANTIS_ENABLE_POLICY_CHECKS=true#6484

Open
Abzaek wants to merge 1 commit into
runatlantis:mainfrom
Abzaek:fix/api-plan-panic-policy-checks
Open

fix: POST /api/plan panics when ATLANTIS_ENABLE_POLICY_CHECKS=true#6484
Abzaek wants to merge 1 commit into
runatlantis:mainfrom
Abzaek:fix/api-plan-panic-policy-checks

Conversation

@Abzaek

@Abzaek Abzaek commented May 16, 2026

Copy link
Copy Markdown

Fixes a panic (index out of range) in POST /api/plan when ATLANTIS_ENABLE_POLICY_CHECKS=true.

Root cause: getCommands() returns cmds (project contexts) and cc (comment commands). When policy checks are enabled, BuildPlanCommands appends a PolicyCheck context alongside Plan context for each project, so cmds has 2xN entries while cc has N entries. The loop indexes both slices with the same variable i, causing panic.

Fix: guard cc[i] with bounds check. Same fix applied to apiApply for consistency.

Fixes #6449

Copilot AI review requested due to automatic review settings May 16, 2026 21:33
@dosubot dosubot Bot added bug Something isn't working go Pull requests that update Go code labels May 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Attempts to fix the panic in POST /api/plan (and symmetrically /api/apply) that occurs when ATLANTIS_ENABLE_POLICY_CHECKS=true. The root cause is that BuildPlanCommands returns 2×N ProjectContexts (Plan + PolicyCheck) while cc only has N CommentCommands, so indexing cc[i] panics. The patch guards the access with a length check and passes nil when out of range.

Changes:

  • Adds a bounds check around cc[i] in the apiPlan loop, passing nil to pre/post hook runners when the index overflows.
  • Applies the same guard in apiApply for symmetry.

Comment thread server/controllers/api_controller.go Outdated
Comment thread server/controllers/api_controller.go
Comment thread server/controllers/api_controller.go Outdated
Comment thread server/controllers/api_controller.go
@chenrui333 chenrui333 force-pushed the fix/api-plan-panic-policy-checks branch 3 times, most recently from 600a786 to 8ffa1bc Compare June 26, 2026 03:06
When ATLANTIS_ENABLE_POLICY_CHECKS=true, BuildPlanCommands can add policy-check contexts in addition to plan contexts. Keep API workflow hooks tied to real plan comment commands, load existing pull status so sticky policy approvals are preserved, route policy checks through the policy-check runner, and include policy-check failures in the API plan result.

For branch-only API requests, return policy-check results without PR comment, status, or database side effects. For /api/apply, refresh PR and pull status after the plan phase, then stop before building apply commands if the plan phase reports failed policy checks.

Fixes runatlantis#6449

Assisted-by: OpenAI <noreply@openai.com>
Signed-off-by: Rui Chen <rui@chenrui.dev>
@chenrui333 chenrui333 force-pushed the fix/api-plan-panic-policy-checks branch from 8ffa1bc to 3b4fd50 Compare June 26, 2026 03:23
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

POST /api/plan panics when ATLANTIS_ENABLE_POLICY_CHECKS=true

2 participants