mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-07-30 12:38:47 +00:00
* fix(deps): update module github.com/petergtz/pegomock/v3 to v4 in go.mod * remove pegomock generate m option, which is not support after v4 * make regen-mocks * replace pegomock v4 primitive eq/matchers * convert pegomock v4 Eq/Any matchers * remove custom models.Repo matcher * pegomock v4 cannot use result method args ref https://github.com/petergtz/pegomock/issues/123 --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
88 lines
3.4 KiB
Go
88 lines
3.4 KiB
Go
package events
|
|
|
|
import (
|
|
"github.com/runatlantis/atlantis/server/core/config/raw"
|
|
"github.com/runatlantis/atlantis/server/core/config/valid"
|
|
"github.com/runatlantis/atlantis/server/events/command"
|
|
)
|
|
|
|
//go:generate pegomock generate --package mocks -o mocks/mock_command_requirement_handler.go CommandRequirementHandler
|
|
type CommandRequirementHandler interface {
|
|
ValidatePlanProject(repoDir string, ctx command.ProjectContext) (string, error)
|
|
ValidateApplyProject(repoDir string, ctx command.ProjectContext) (string, error)
|
|
ValidateImportProject(repoDir string, ctx command.ProjectContext) (string, error)
|
|
}
|
|
|
|
type DefaultCommandRequirementHandler struct {
|
|
WorkingDir WorkingDir
|
|
}
|
|
|
|
func (a *DefaultCommandRequirementHandler) ValidatePlanProject(repoDir string, ctx command.ProjectContext) (failure string, err error) {
|
|
for _, req := range ctx.PlanRequirements {
|
|
switch req {
|
|
case raw.ApprovedRequirement:
|
|
if !ctx.PullReqStatus.ApprovalStatus.IsApproved {
|
|
return "Pull request must be approved by at least one person other than the author before running plan.", nil
|
|
}
|
|
case raw.MergeableRequirement:
|
|
if !ctx.PullReqStatus.Mergeable {
|
|
return "Pull request must be mergeable before running plan.", nil
|
|
}
|
|
case raw.UnDivergedRequirement:
|
|
if a.WorkingDir.HasDiverged(ctx.Log, repoDir) {
|
|
return "Default branch must be rebased onto pull request before running plan.", nil
|
|
}
|
|
}
|
|
}
|
|
// Passed all plan requirements configured.
|
|
return "", nil
|
|
}
|
|
|
|
func (a *DefaultCommandRequirementHandler) ValidateApplyProject(repoDir string, ctx command.ProjectContext) (failure string, err error) {
|
|
for _, req := range ctx.ApplyRequirements {
|
|
switch req {
|
|
case raw.ApprovedRequirement:
|
|
if !ctx.PullReqStatus.ApprovalStatus.IsApproved {
|
|
return "Pull request must be approved by at least one person other than the author before running apply.", nil
|
|
}
|
|
// this should come before mergeability check since mergeability is a superset of this check.
|
|
case valid.PoliciesPassedCommandReq:
|
|
// We should rely on this function instead of plan status, since plan status after a failed apply will not carry the policy error over.
|
|
if !ctx.PolicyCleared() {
|
|
return "All policies must pass for project before running apply.", nil
|
|
}
|
|
case raw.MergeableRequirement:
|
|
if !ctx.PullReqStatus.Mergeable {
|
|
return "Pull request must be mergeable before running apply.", nil
|
|
}
|
|
case raw.UnDivergedRequirement:
|
|
if a.WorkingDir.HasDiverged(ctx.Log, repoDir) {
|
|
return "Default branch must be rebased onto pull request before running apply.", nil
|
|
}
|
|
}
|
|
}
|
|
// Passed all apply requirements configured.
|
|
return "", nil
|
|
}
|
|
|
|
func (a *DefaultCommandRequirementHandler) ValidateImportProject(repoDir string, ctx command.ProjectContext) (failure string, err error) {
|
|
for _, req := range ctx.ImportRequirements {
|
|
switch req {
|
|
case raw.ApprovedRequirement:
|
|
if !ctx.PullReqStatus.ApprovalStatus.IsApproved {
|
|
return "Pull request must be approved by at least one person other than the author before running import.", nil
|
|
}
|
|
case raw.MergeableRequirement:
|
|
if !ctx.PullReqStatus.Mergeable {
|
|
return "Pull request must be mergeable before running import.", nil
|
|
}
|
|
case raw.UnDivergedRequirement:
|
|
if a.WorkingDir.HasDiverged(ctx.Log, repoDir) {
|
|
return "Default branch must be rebased onto pull request before running import.", nil
|
|
}
|
|
}
|
|
}
|
|
// Passed all import requirements configured.
|
|
return "", nil
|
|
}
|