Files
atlantis/server/events/policy_check_command_runner.go
Sarvar Muminov af2a806870 Implement a new policy check workflow (#1317)
* Adding policy_check support into yaml config

* Added policy check model and runtime structs

* Adding BuildPolicyCheckCommand to ProjectCommandBuilder

* Return incorrectly deleted code

* Remove BuildAutoPolicyPlanCommand from ProjectCommandBuilder

* Split runAutoCommand into two functions

runAutoPlanCommand - does what originally RunAutoplancommand was doing
except now it returns CommandResult and []models.ProjectCommandContext
runAutoPolicyCheckCommand - accepts CommandContext, CommandResult, and
[]models.ProjectCommandContext as arguments and runs PolicyCheckStep runner

* Refactor RunCommentCommand
* Remove BuildPolicyCheckCommand and rename StepCmdExec back to TerraformExec
* Add policy step runner logic and conftest interfaces.
* Add show step runner to policy check stage.

* Adding models.PolicyCheckCommand to buildCtx

This also means buildPlanAllCommands call buildCtx twice once with
models.PlanCommand and once with models.PolicyCheckCommand

* Adding new project_command_builder that supports policy_check

* Refactoring PolicyCheck specific logic into a PolicyCheckProjectCommandBuilder

* Moving events.CommandContext to models.CommandContext this will allow me
to remove buildCtx method and move ProjectCommandContext creation into
models package

* Policy Owners might be different types, for that reason we are
refactoring Owners into its own struct with specific keys defining
different owner types

Co-authored-by: Nish Krishnan <nishk@lyft.com>
Co-authored-by: Nish Krishnan <nishkrishnan@users.noreply.github.com>
2021-02-10 18:13:44 -08:00

77 lines
2.4 KiB
Go

package events
import "github.com/runatlantis/atlantis/server/events/models"
func NewPolicyCheckCommandRunner(
dbUpdater *DBUpdater,
pullUpdater *PullUpdater,
commitStatusUpdater CommitStatusUpdater,
projectCommandRunner ProjectPolicyCheckCommandRunner,
parallelPoolSize int,
) *PolicyCheckCommandRunner {
return &PolicyCheckCommandRunner{
dbUpdater: dbUpdater,
pullUpdater: pullUpdater,
commitStatusUpdater: commitStatusUpdater,
prjCmdRunner: projectCommandRunner,
parallelPoolSize: parallelPoolSize,
}
}
type PolicyCheckCommandRunner struct {
dbUpdater *DBUpdater
pullUpdater *PullUpdater
commitStatusUpdater CommitStatusUpdater
prjCmdRunner ProjectPolicyCheckCommandRunner
parallelPoolSize int
}
func (p *PolicyCheckCommandRunner) Run(ctx *CommandContext, cmds []models.ProjectCommandContext) {
if len(cmds) == 0 {
return
}
// So set policy_check commit status to pending
if err := p.commitStatusUpdater.UpdateCombined(ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, models.PolicyCheckCommand); err != nil {
ctx.Log.Warn("unable to update commit status: %s", err)
}
var result CommandResult
if p.isParallelEnabled(cmds) {
ctx.Log.Info("Running policy_checks in parallel")
result = runProjectCmdsParallel(cmds, p.prjCmdRunner.PolicyCheck, p.parallelPoolSize)
} else {
result = runProjectCmds(cmds, p.prjCmdRunner.PolicyCheck)
}
p.pullUpdater.updatePull(ctx, PolicyCheckCommand{}, result)
pullStatus, err := p.dbUpdater.updateDB(ctx, ctx.Pull, result.ProjectResults)
if err != nil {
ctx.Log.Err("writing results: %s", err)
}
p.updateCommitStatus(ctx, pullStatus)
}
func (p *PolicyCheckCommandRunner) updateCommitStatus(ctx *CommandContext, pullStatus models.PullStatus) {
var numSuccess int
var numErrored int
status := models.SuccessCommitStatus
numSuccess = pullStatus.StatusCount(models.PassedPolicyCheckStatus)
numErrored = pullStatus.StatusCount(models.ErroredPolicyCheckStatus)
if numErrored > 0 {
status = models.FailedCommitStatus
}
if err := p.commitStatusUpdater.UpdateCombinedCount(ctx.Pull.BaseRepo, ctx.Pull, status, models.PolicyCheckCommand, numSuccess, len(pullStatus.Projects)); err != nil {
ctx.Log.Warn("unable to update commit status: %s", err)
}
}
func (p *PolicyCheckCommandRunner) isParallelEnabled(cmds []models.ProjectCommandContext) bool {
return len(cmds) > 0 && cmds[0].ParallelPolicyCheckEnabled
}