Skip to content

feat: discard pull request approval status on autoplan#6281

Open
verdel wants to merge 4 commits intorunatlantis:mainfrom
verdel:feat-discard-pr-approval-on-autoplan
Open

feat: discard pull request approval status on autoplan#6281
verdel wants to merge 4 commits intorunatlantis:mainfrom
verdel:feat-discard-pr-approval-on-autoplan

Conversation

@verdel
Copy link

@verdel verdel commented Mar 3, 2026

what

I am adding functionality to discard PR approvals when the autoplan runs.
This means approvals will be discarded not only when the atlantis plan command is triggered via a PR comment, but also when autoplan runs automatically on PR updates (such as new or modified commits).

why

Atlantis has an option --discard-approval-on-plan, which is responsible for discarding approvals in a PR when the plan command is executed. However, if changes are made to the PR after the required approvals have been obtained, their status is not reset.

In this case, unwanted changes could be introduced into the PR after approvals, which would later be applied to the Terraform configuration.

I believe it makes more sense to also reset approval status when autoplan runs.

If needed, I can add an additional option --discard-approval-on-autoplan, which would separately control the functionality of discarding approvals when autoplan is executed.

tests

  • I have tested my changes by unit tests

references

This PR closes #6280

verdel added 2 commits March 4, 2026 00:36
Discard existing pull request approvals when autoplan runs on PR updates. This guarantees that any code changes introduced in the PR must be reviewed and approved again.

Signed-off-by: Vadim Aleksandrov <valeksandrov@me.com>
Signed-off-by: Vadim Aleksandrov <valeksandrov@me.com>
Copilot AI review requested due to automatic review settings March 3, 2026 22:09
@dosubot dosubot bot added feature New functionality/enhancement go Pull requests that update Go code labels Mar 3, 2026
@github-actions github-actions bot removed the feature New functionality/enhancement label Mar 3, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds parity between manual plan runs and autoplan by dismissing existing PR approvals when an autoplan-triggered plan runs, helping ensure approvals reflect the latest Terraform-relevant changes.

Changes:

  • Invoke DiscardReviews during autoplan execution when --discard-approval-on-plan is enabled.
  • Add a unit test intended to verify approvals are discarded on autoplan.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
server/events/plan_command_runner.go Adds approval-discard logic to runAutoplan.
server/events/command_runner_test.go Adds a test for the new autoplan approval-discard behavior.

Comment on lines +104 to +108
if p.DiscardApprovalOnPlan {
if err := p.pullUpdater.VCSClient.DiscardReviews(ctx.Log, baseRepo, pull); err != nil {
ctx.Log.Err("failed to remove approvals: %s", err)
}
}
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

DiscardReviews is called before building autoplan commands, so approvals will be dismissed even when autoplan ends up running no plans (e.g., BuildAutoplanCommands returns an empty list / no Terraform changes). This can unnecessarily discard approvals on unrelated PR updates. Consider moving the discard logic to after BuildAutoplanCommands succeeds and only when there is at least one plan command to run.

Copilot uses AI. Check for mistakes.
Comment on lines +1054 to +1071
func TestRunAutoplanCommand_DiscardApprovals(t *testing.T) {
vcsClient := setup(t, func(testConfig *TestConfig) {
testConfig.discardApprovalOnPlan = true
})

tmp := t.TempDir()
boltDB, err := boltdb.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Database = boltDB
applyCommandRunner.Database = boltDB

testdata.Pull.BaseRepo = testdata.GithubRepo
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, testdata.Pull, testdata.User)
vcsClient.VerifyWasCalledOnce().DiscardReviews(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest]())
}
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

This test doesn't stub BuildAutoplanCommands, so it may pass even if autoplan produces no plan commands (and therefore doesn't validate the intended behavior when an actual autoplan plan runs). To make the coverage meaningful, set BuildAutoplanCommands to return at least one command.ProjectContext (and stub any required plan/workingDir interactions) and then assert DiscardReviews is invoked.

Copilot uses AI. Check for mistakes.
Signed-off-by: Vadim Aleksandrov <valeksandrov@me.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

CommandName: command.Plan,
},
}, nil)

Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

This test relies on unstubbed mock defaults for workingDir.GetPullDir and projectCommandRunner.Plan, which can cause it to run with an empty pull dir and a zero-value plan result. Consider stubbing those calls (as other tests in this file do) so the test exercises the realistic autoplan path and is less brittle to future changes.

Suggested change
When(projectCommandRunner.Plan(Any[command.ProjectContext]())).ThenReturn(
command.ProjectCommandOutput{
PlanSuccess: &models.PlanSuccess{},
},
)
When(workingDir.GetPullDir(Any[models.Repo](), Any[models.PullRequest]())).
ThenReturn(tmp, nil)

Copilot uses AI. Check for mistakes.
Signed-off-by: Vadim Aleksandrov <valeksandrov@me.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discard pull request approval status on autoplan

2 participants