Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2507,9 +2507,11 @@ func TestRunSpecificPlanCommandDoesnt_DeletePlans(t *testing.T) {
pendingPlanFinder.VerifyWasCalled(Never()).Find(tmp)
}

// Test that if one plan fails and we are using automerge, that
// we delete the plans.
func TestRunAutoplanCommandWithError_DeletePlans(t *testing.T) {
// Test that if one plan fails and we are using automerge, the successful
// plan is kept (not deleted) so it can still be applied on its own. Automerge
// itself is still blocked because it requires every project to have applied
// successfully.
func TestRunAutoplanCommandWithError_KeepsSuccessfulPlans(t *testing.T) {
vcsClient := setup(t)

tmp := t.TempDir()
Expand Down Expand Up @@ -2556,8 +2558,9 @@ func TestRunAutoplanCommandWithError_DeletePlans(t *testing.T) {
ThenReturn(tmp, nil)
testdata.Pull.BaseRepo = testdata.GithubRepo
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, testdata.Pull, testdata.User)
// gets called twice: the first time before the plan starts, the second time after the plan errors
pendingPlanFinder.VerifyWasCalled(Times(2)).Find(tmp)
// only called once, to discard stale plans before planning starts. The
// successful project's plan/lock must survive the other project's error.
pendingPlanFinder.VerifyWasCalled(Times(1)).Find(tmp)

vcsClient.VerifyWasCalled(Times(0)).DiscardReviews(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest]())
}
Expand Down
30 changes: 15 additions & 15 deletions server/events/plan_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ func (p *PlanCommandRunner) runAutoplan(ctx *command.Context) {
result := runProjectCmdsWithCancellationTracker(ctx, projectCmds, p.cancellationTracker, p.parallelPoolSize, p.isParallelEnabled(projectCmds), p.prjCmdRunner.Plan)

if p.autoMerger.automergeEnabled(projectCmds) && result.HasErrors() {
ctx.Log.Info("deleting plans because there were errors and automerge requires all plans succeed")
if err := p.deletePlansAndPlanLocks(ctx, projectCmds); err != nil {
ctx.Log.Err("deleting pending plans: %s", err)
}
result.PlansDeleted = true
// We keep any successful plans (and their locks) so that projects
// which planned cleanly can still be applied individually. Automerge
// itself already refuses to merge until every project in the pull
// request has been successfully applied, so nothing else needs to
// gate on this failure here.
ctx.Log.Info("not automerging because one or more projects had errors, but keeping successful plans so they can still be applied")
}

p.pullUpdater.updatePull(ctx, AutoplanCommand{}, result)
Expand All @@ -197,9 +198,8 @@ func (p *PlanCommandRunner) runAutoplan(ctx *command.Context) {
p.updateCommitStatus(ctx, pullStatus, command.Plan)
p.updateCommitStatus(ctx, pullStatus, command.Apply)

// Check if there are any planned projects and if there are any errors or if plans are being deleted
if len(policyCheckCmds) > 0 &&
(!result.HasErrors() && !result.PlansDeleted) {
// Check if there are any planned projects and if there are any errors
if len(policyCheckCmds) > 0 && !result.HasErrors() {
// Run policy_check command
ctx.Log.Info("Running policy_checks for all plans")

Expand Down Expand Up @@ -327,11 +327,12 @@ func (p *PlanCommandRunner) run(ctx *command.Context, cmd *CommentCommand) {
ctx.CommandHasErrors = result.HasErrors()

if p.autoMerger.automergeEnabled(projectCmds) && result.HasErrors() {
ctx.Log.Info("deleting plans because there were errors and automerge requires all plans succeed")
if err := p.deletePlansAndPlanLocks(ctx, projectCmds); err != nil {
ctx.Log.Err("deleting pending plans: %s", err)
}
result.PlansDeleted = true
// We keep any successful plans (and their locks) so that projects
// which planned cleanly can still be applied individually. Automerge
// itself already refuses to merge until every project in the pull
// request has been successfully applied, so nothing else needs to
// gate on this failure here.
ctx.Log.Info("not automerging because one or more projects had errors, but keeping successful plans so they can still be applied")
}

p.pullUpdater.updatePull(
Expand All @@ -357,8 +358,7 @@ func (p *PlanCommandRunner) run(ctx *command.Context, cmd *CommentCommand) {

// Runs policy checks step after all plans are successful.
// This step does not approve any policies that require approval.
if len(result.ProjectResults) > 0 &&
(!result.HasErrors() && !result.PlansDeleted) {
if len(result.ProjectResults) > 0 && !result.HasErrors() {
ctx.Log.Info("Running policy check for '%s'", cmd.CommandName())
p.policyCheckCommandRunner.Run(ctx, policyCheckCmds)
} else if len(projectCmds) == 0 && !cmd.IsForSpecificProject() {
Expand Down
Loading