Skip to content

Commit

Permalink
Also delete locks along with plans when running
Browse files Browse the repository at this point in the history
autoplan or generic plan command
  • Loading branch information
giuli007 committed Jul 14, 2021
1 parent d20687c commit 86a14f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
parallelPoolSize,
silenceNoProjects,
boltdb,
lockingClient,
)

applyCommandRunner := events.NewApplyCommandRunner(
Expand Down
9 changes: 9 additions & 0 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var policyCheckCommandRunner *events.PolicyCheckCommandRunner
var approvePoliciesCommandRunner *events.ApprovePoliciesCommandRunner
var planCommandRunner *events.PlanCommandRunner
var applyLockChecker *lockingmocks.MockApplyLockChecker
var locker *lockingmocks.MockLocker
var applyCommandRunner *events.ApplyCommandRunner
var unlockCommandRunner *events.UnlockCommandRunner
var preWorkflowHooksCommandRunner events.PreWorkflowHooksCommandRunner
Expand All @@ -85,6 +86,7 @@ func setup(t *testing.T) *vcsmocks.MockClient {
drainer = &events.Drainer{}
deleteLockCommand = eventmocks.NewMockDeleteLockCommand()
applyLockChecker = lockingmocks.NewMockApplyLockChecker()
locker = lockingmocks.NewMockLocker()

dbUpdater = &events.DBUpdater{
DB: defaultBoltDB,
Expand Down Expand Up @@ -128,6 +130,7 @@ func setup(t *testing.T) *vcsmocks.MockClient {
parallelPoolSize,
SilenceNoProjects,
defaultBoltDB,
locker,
)

applyCommandRunner = events.NewApplyCommandRunner(
Expand Down Expand Up @@ -465,6 +468,7 @@ func TestRunAutoplanCommand_DeletePlans(t *testing.T) {
fixtures.Pull.BaseRepo = fixtures.GithubRepo
ch.RunAutoplanCommand(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
pendingPlanFinder.VerifyWasCalledOnce().DeletePlans(tmp)
locker.VerifyWasCalledOnce().UnlockByPull(fixtures.Pull.BaseRepo.FullName, fixtures.Pull.Num)
}

func TestRunGenericPlanCommand_DeletePlans(t *testing.T) {
Expand All @@ -480,9 +484,14 @@ func TestRunGenericPlanCommand_DeletePlans(t *testing.T) {

When(projectCommandRunner.Plan(matchers.AnyModelsProjectCommandContext())).ThenReturn(models.ProjectResult{PlanSuccess: &models.PlanSuccess{}})
When(workingDir.GetPullDir(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(tmp, nil)
pull := &github.PullRequest{State: github.String("open")}
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num}
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil)
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil)
fixtures.Pull.BaseRepo = fixtures.GithubRepo
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: models.PlanCommand})
pendingPlanFinder.VerifyWasCalledOnce().DeletePlans(tmp)
locker.VerifyWasCalledOnce().UnlockByPull(fixtures.Pull.BaseRepo.FullName, fixtures.Pull.Num)
}

func TestRunSpecificPlanCommandDoesnt_DeletePlans(t *testing.T) {
Expand Down
16 changes: 14 additions & 2 deletions server/events/plan_command_runner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package events

import (
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs"
)
Expand All @@ -21,6 +22,7 @@ func NewPlanCommandRunner(
parallelPoolSize int,
SilenceNoProjects bool,
pullStatusFetcher PullStatusFetcher,
locker locking.Locker,
) *PlanCommandRunner {
return &PlanCommandRunner{
silenceVCSStatusNoPlans: silenceVCSStatusNoPlans,
Expand All @@ -38,6 +40,7 @@ func NewPlanCommandRunner(
parallelPoolSize: parallelPoolSize,
SilenceNoProjects: SilenceNoProjects,
pullStatusFetcher: pullStatusFetcher,
locker: locker,
}
}

Expand All @@ -63,6 +66,7 @@ type PlanCommandRunner struct {
autoMerger *AutoMerger
parallelPoolSize int
pullStatusFetcher PullStatusFetcher
locker locking.Locker
}

func (p *PlanCommandRunner) runAutoplan(ctx *CommandContext) {
Expand Down Expand Up @@ -106,8 +110,12 @@ func (p *PlanCommandRunner) runAutoplan(ctx *CommandContext) {
}

// discard previous plans that might not be relevant anymore
ctx.Log.Debug("deleting previous plans")
ctx.Log.Debug("deleting previous plans and locks")
p.deletePlans(ctx)
_, err = p.locker.UnlockByPull(baseRepo.FullName, pull.Num)
if err != nil {
ctx.Log.Err("deleting locks: %s", err)
}

// Only run commands in parallel if enabled
var result CommandResult
Expand Down Expand Up @@ -186,8 +194,12 @@ func (p *PlanCommandRunner) run(ctx *CommandContext, cmd *CommentCommand) {
// if the plan is generic, new plans will be generated based on changes
// discard previous plans that might not be relevant anymore
if !cmd.IsForSpecificProject() {
ctx.Log.Debug("deleting previous plans")
ctx.Log.Debug("deleting previous plans and locks")
p.deletePlans(ctx)
_, err = p.locker.UnlockByPull(baseRepo.FullName, pull.Num)
if err != nil {
ctx.Log.Err("deleting locks: %s", err)
}
}

// Only run commands in parallel if enabled
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
userConfig.ParallelPoolSize,
userConfig.SilenceNoProjects,
boltdb,
lockingClient,
)

applyCommandRunner := events.NewApplyCommandRunner(
Expand Down

0 comments on commit 86a14f8

Please sign in to comment.