Files
atlantis/server/events/unlock_command_runner.go
Simon Heather b459645d03 fix: Running 'atlantis unlock' on a PR Causes The Whole Working Directory to be Deleted (#3751)
* Running 'atlantis unlock' on a PR Causes The Whole Working Directory to be Deleted

* Update events controller tests

* Fix events controller tests

---------

Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
2023-09-11 11:00:12 -04:00

55 lines
1.5 KiB
Go

package events
import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/vcs"
)
func NewUnlockCommandRunner(
deleteLockCommand DeleteLockCommand,
vcsClient vcs.Client,
SilenceNoProjects bool,
) *UnlockCommandRunner {
return &UnlockCommandRunner{
deleteLockCommand: deleteLockCommand,
vcsClient: vcsClient,
SilenceNoProjects: SilenceNoProjects,
}
}
type UnlockCommandRunner struct {
vcsClient vcs.Client
deleteLockCommand DeleteLockCommand
// SilenceNoProjects is whether Atlantis should respond to PRs if no projects
// are found
SilenceNoProjects bool
}
func (u *UnlockCommandRunner) Run(
ctx *command.Context,
cmd *CommentCommand,
) {
baseRepo := ctx.Pull.BaseRepo
pullNum := ctx.Pull.Num
ctx.Log.Info("Unlocking all locks")
vcsMessage := "All Atlantis locks for this PR have been unlocked and plans discarded"
numLocks, err := u.deleteLockCommand.DeleteLocksByPull(baseRepo.FullName, pullNum)
if err != nil {
vcsMessage = "Failed to delete PR locks"
ctx.Log.Err("failed to delete locks by pull %s", err.Error())
}
// if there are no locks to delete, no errors, and SilenceNoProjects is enabled, don't comment
if err == nil && numLocks == 0 {
ctx.Log.Info("No locks to delete")
if u.SilenceNoProjects {
return
}
}
if commentErr := u.vcsClient.CreateComment(baseRepo, pullNum, vcsMessage, command.Unlock.String()); commentErr != nil {
ctx.Log.Err("unable to comment: %s", commentErr)
}
}