-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
command_requirement_handler.go
105 lines (94 loc) · 4.03 KB
/
command_requirement_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package events
import (
"fmt"
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
)
//go:generate pegomock generate --package mocks -o mocks/mock_command_requirement_handler.go CommandRequirementHandler
type CommandRequirementHandler interface {
ValidateProjectDependencies(ctx command.ProjectContext) (string, error)
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 according to the project's approval rules 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 according to the project's approval rules 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) ValidateProjectDependencies(ctx command.ProjectContext) (failure string, err error) {
for _, dependOnProject := range ctx.DependsOn {
for _, project := range ctx.PullStatus.Projects {
if project.ProjectName == dependOnProject && project.Status != models.AppliedPlanStatus && project.Status != models.PlannedNoChangesPlanStatus {
return fmt.Sprintf("Can't apply your project unless you apply its dependencies: [%s]", project.ProjectName), nil
}
}
}
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 according to the project's approval rules 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
}