Files
atlantis/server/events/unlock_command_runner.go
Dylan Page 25103d72f4 feat: add new flag --silence-no-projects (#1469)
* feat: add new flag --silence-no-projects

* refactor: add tests, clean up command_runner logic

* fix: attempt at fixing PolicyCheck e2e test

* docs: add silence-no-projects flag to server-configuration.md

* docs: fix grammar

* fix: requested changes: commit status resets, misc fixes

* fix: status check comments to actually reflect their commands

* fix: logic bug in autoplan commit reset

* fix: tests make check-lint errors
2021-04-19 14:52:56 -07:00

51 lines
1.4 KiB
Go

package events
import (
"github.com/runatlantis/atlantis/server/events/models"
"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 *CommandContext,
cmd *CommentCommand,
) {
baseRepo := ctx.Pull.BaseRepo
pullNum := ctx.Pull.Num
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 && u.SilenceNoProjects {
return
}
if commentErr := u.vcsClient.CreateComment(baseRepo, pullNum, vcsMessage, models.UnlockCommand.String()); commentErr != nil {
ctx.Log.Err("unable to comment: %s", commentErr)
}
}