Skip to content

Commit

Permalink
feat: global repo params (runatlantis#3379)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudomorph authored and ijames-gc committed Feb 13, 2024
1 parent 83bfcae commit 99b1fe0
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 113 deletions.
10 changes: 10 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const (
AllowRepoConfigFlag = "allow-repo-config"
AtlantisURLFlag = "atlantis-url"
AutomergeFlag = "automerge"
ParallelPlanFlag = "parallel-plan"
ParallelApplyFlag = "parallel-apply"
AutoplanModules = "autoplan-modules"
AutoplanModulesFromProjects = "autoplan-modules-from-projects"
AutoplanFileListFlag = "autoplan-file-list"
Expand Down Expand Up @@ -468,6 +470,14 @@ var boolFlags = map[string]boolFlag{
"VCS support is limited to: GitHub.",
defaultValue: false,
},
ParallelPlanFlag: {
description: "Run plan operations in parallel.",
defaultValue: false,
},
ParallelApplyFlag: {
description: "Run apply operations in parallel.",
defaultValue: false,
},
QuietPolicyChecks: {
description: "Exclude policy check comments from pull requests unless there's an actual error from conftest. This also excludes warnings.",
defaultValue: false,
Expand Down
2 changes: 2 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var testFlags = map[string]interface{}{
AllowDraftPRs: true,
PortFlag: 8181,
ParallelPoolSize: 100,
ParallelPlanFlag: true,
ParallelApplyFlag: true,
RepoAllowlistFlag: "github.com/runatlantis/atlantis",
RequireApprovalFlag: true,
RequireMergeableFlag: true,
Expand Down
3 changes: 1 addition & 2 deletions runatlantis.io/docs/automerging.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ been successfully applied.

## How To Enable
Automerging can be enabled either by:
1. Passing the `--automerge` flag to `atlantis server`. This will cause all
pull requests to be automerged and any repo config will be ignored.
1. Passing the `--automerge` flag to `atlantis server`. This sets the parameter globally; however, explicit declaration in the repo config will be respected and take priority.
1. Setting `automerge: true` in the repo's `atlantis.yaml` file:
```yaml
version: 3
Expand Down
16 changes: 16 additions & 0 deletions runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,22 @@ This is useful when you have many projects and want to keep the pull request cle
```
Max size of the wait group that runs parallel plans and applies (if enabled). Defaults to `15`

### `--parallel-plan`
```bash
atlantis server --parallel-plan
# or
ATLANTIS_PARALLEL_PLAN=true
```
Whether to run plan operations in parallel. Defaults to `false`. Explicit declaration in [repo config](repo-level-atlantis-yaml.html#run-plans-and-applies-in-parallel) takes precidence.

### `--parallel-apply`
```bash
atlantis server --parallel-apply
# or
ATLANTIS_PARALLEL_APPLY=true
```
Whether to run apply operations in parallel. Defaults to `false`. Explicit declaration in [repo config](repo-level-atlantis-yaml.html#run-plans-and-applies-in-parallel) takes precidence.

### `--port`
```bash
atlantis server --port=4141
Expand Down
3 changes: 3 additions & 0 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,9 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
commentParser,
false,
false,
false,
false,
false,
"",
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
Expand Down
32 changes: 3 additions & 29 deletions server/core/config/raw/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/valid"
)

// DefaultAutomerge is the default setting for automerge.
const DefaultAutomerge = false

// DefaultParallelApply is the default setting for parallel apply
const DefaultParallelApply = false

// DefaultParallelPlan is the default setting for parallel plan
const DefaultParallelPlan = false

// DefaultParallelPolicyCheck is the default setting for parallel plan
const DefaultParallelPolicyCheck = false

// DefaultDeleteSourceBranchOnMerge being false is the default setting whether or not to remove a source branch on merge
const DefaultDeleteSourceBranchOnMerge = false

// DefaultEmojiReaction is the default emoji reaction for repos
const DefaultEmojiReaction = ""

Expand Down Expand Up @@ -72,20 +57,9 @@ func (r RepoCfg) ToValid() valid.RepoCfg {
validProjects = append(validProjects, p.ToValid())
}

automerge := DefaultAutomerge
if r.Automerge != nil {
automerge = *r.Automerge
}

parallelApply := DefaultParallelApply
if r.ParallelApply != nil {
parallelApply = *r.ParallelApply
}

parallelPlan := DefaultParallelPlan
if r.ParallelPlan != nil {
parallelPlan = *r.ParallelPlan
}
automerge := r.Automerge
parallelApply := r.ParallelApply
parallelPlan := r.ParallelPlan

emojiReaction := DefaultEmojiReaction
if r.EmojiReaction != nil {
Expand Down
20 changes: 10 additions & 10 deletions server/core/config/raw/repo_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ func TestConfig_ToValid(t *testing.T) {
},
exp: valid.RepoCfg{
Version: 2,
Automerge: false,
ParallelApply: false,
Automerge: nil,
ParallelApply: nil,
AbortOnExcecutionOrderFail: false,
Workflows: map[string]valid.Workflow{},
},
Expand All @@ -281,8 +281,8 @@ func TestConfig_ToValid(t *testing.T) {
},
exp: valid.RepoCfg{
Version: 2,
Automerge: true,
ParallelApply: true,
Automerge: Bool(true),
ParallelApply: Bool(true),
AbortOnExcecutionOrderFail: true,
Workflows: map[string]valid.Workflow{},
},
Expand All @@ -297,8 +297,8 @@ func TestConfig_ToValid(t *testing.T) {
},
exp: valid.RepoCfg{
Version: 2,
Automerge: false,
ParallelApply: false,
Automerge: Bool(false),
ParallelApply: Bool(false),
AbortOnExcecutionOrderFail: false,
Workflows: map[string]valid.Workflow{},
},
Expand All @@ -319,8 +319,8 @@ func TestConfig_ToValid(t *testing.T) {
},
exp: valid.RepoCfg{
Version: 2,
Automerge: false,
ParallelApply: false,
Automerge: nil,
ParallelApply: nil,
Workflows: map[string]valid.Workflow{
"myworkflow": {
Name: "myworkflow",
Expand Down Expand Up @@ -386,8 +386,8 @@ func TestConfig_ToValid(t *testing.T) {
},
exp: valid.RepoCfg{
Version: 2,
Automerge: true,
ParallelApply: true,
Automerge: Bool(true),
ParallelApply: Bool(true),
Workflows: map[string]valid.Workflow{
"myworkflow": {
Name: "myworkflow",
Expand Down
8 changes: 4 additions & 4 deletions server/core/config/valid/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type RepoCfg struct {
Projects []Project
Workflows map[string]Workflow
PolicySets PolicySets
Automerge bool
ParallelApply bool
ParallelPlan bool
ParallelPolicyCheck bool
Automerge *bool
ParallelApply *bool
ParallelPlan *bool
ParallelPolicyCheck *bool
DeleteSourceBranchOnMerge *bool
RepoLocking *bool
EmojiReaction string
Expand Down
14 changes: 10 additions & 4 deletions server/events/automerger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ func (c *AutoMerger) automerge(ctx *command.Context, pullStatus models.PullStatu

// automergeEnabled returns true if automerging is enabled in this context.
func (c *AutoMerger) automergeEnabled(projectCmds []command.ProjectContext) bool {
// If the global automerge is set, we always automerge.
return c.GlobalAutomerge ||
// Otherwise we check if this repo is configured for automerging.
(len(projectCmds) > 0 && projectCmds[0].AutomergeEnabled)
// only automerge if all projects have automerge set; or if global automerge is set and there are no projects.
automerge := c.GlobalAutomerge
if len(projectCmds) > 0 {
for _, prjCmd := range projectCmds {
if !prjCmd.AutomergeEnabled {
automerge = false
}
}
}
return automerge
}

// deleteSourceBranchOnMergeEnabled returns true if we should delete the source branch on merge in this context.
Expand Down
73 changes: 48 additions & 25 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ const (
// DefaultWorkspace is the default Terraform workspace we run commands in.
// This is also Terraform's default workspace.
DefaultWorkspace = "default"
// DefaultAutomergeEnabled is the default for the automerge setting.
DefaultAutomergeEnabled = false
// DefaultParallelApplyEnabled is the default for the parallel apply setting.
DefaultParallelApplyEnabled = false
// DefaultParallelPlanEnabled is the default for the parallel plan setting.
DefaultParallelPlanEnabled = false
// DefaultDeleteSourceBranchOnMerge being false is the default setting whether or not to remove a source branch on merge
DefaultDeleteSourceBranchOnMerge = false
// DefaultAbortOnExcecutionOrderFail being false is the default setting for abort on execution group failiures
Expand All @@ -53,6 +47,9 @@ func NewInstrumentedProjectCommandBuilder(
commentBuilder CommentBuilder,
skipCloneNoChanges bool,
EnableRegExpCmd bool,
EnableAutoMerge bool,
EnableParallelPlan bool,
EnableParallelApply bool,
AutoDetectModuleFiles string,
AutoplanFileList string,
RestrictFileList bool,
Expand Down Expand Up @@ -80,6 +77,9 @@ func NewInstrumentedProjectCommandBuilder(
commentBuilder,
skipCloneNoChanges,
EnableRegExpCmd,
EnableAutoMerge,
EnableParallelPlan,
EnableParallelApply,
AutoDetectModuleFiles,
AutoplanFileList,
RestrictFileList,
Expand All @@ -105,6 +105,9 @@ func NewProjectCommandBuilder(
commentBuilder CommentBuilder,
skipCloneNoChanges bool,
EnableRegExpCmd bool,
EnableAutoMerge bool,
EnableParallelPlan bool,
EnableParallelApply bool,
AutoDetectModuleFiles string,
AutoplanFileList string,
RestrictFileList bool,
Expand All @@ -123,6 +126,9 @@ func NewProjectCommandBuilder(
PendingPlanFinder: pendingPlanFinder,
SkipCloneNoChanges: skipCloneNoChanges,
EnableRegExpCmd: EnableRegExpCmd,
EnableAutoMerge: EnableAutoMerge,
EnableParallelPlan: EnableParallelPlan,
EnableParallelApply: EnableParallelApply,
AutoDetectModuleFiles: AutoDetectModuleFiles,
AutoplanFileList: AutoplanFileList,
RestrictFileList: RestrictFileList,
Expand Down Expand Up @@ -205,6 +211,9 @@ type DefaultProjectCommandBuilder struct {
ProjectCommandContextBuilder ProjectCommandContextBuilder
SkipCloneNoChanges bool
EnableRegExpCmd bool
EnableAutoMerge bool
EnableParallelPlan bool
EnableParallelApply bool
AutoDetectModuleFiles string
AutoplanFileList string
EnableDiffMarkdownFormat bool
Expand Down Expand Up @@ -364,6 +373,23 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
}
ctx.Log.Debug("moduleInfo for %s (matching %q) = %v", repoDir, p.AutoDetectModuleFiles, moduleInfo)

automerge := p.EnableAutoMerge
parallelApply := p.EnableParallelApply
parallelPlan := p.EnableParallelPlan
abortOnExcecutionOrderFail := DefaultAbortOnExcecutionOrderFail
if hasRepoCfg {
if repoCfg.Automerge != nil {
automerge = *repoCfg.Automerge
}
if repoCfg.ParallelApply != nil {
parallelApply = *repoCfg.ParallelApply
}
if repoCfg.ParallelPlan != nil {
parallelPlan = *repoCfg.ParallelPlan
}
abortOnExcecutionOrderFail = repoCfg.AbortOnExcecutionOrderFail
}

if len(repoCfg.Projects) > 0 {
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, repoCfg, repoDir, moduleInfo)
if err != nil {
Expand All @@ -383,9 +409,9 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
mergedCfg,
commentFlags,
repoDir,
repoCfg.Automerge,
repoCfg.ParallelApply,
repoCfg.ParallelPlan,
automerge,
parallelApply,
parallelPlan,
verbose,
repoCfg.AbortOnExcecutionOrderFail,
p.TerraformExecutor,
Expand All @@ -408,16 +434,7 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
if err != nil {
return nil, errors.Wrapf(err, "looking for Terraform Cloud workspace from configuration %s", repoDir)
}
automerge := DefaultAutomergeEnabled
parallelApply := DefaultParallelApplyEnabled
parallelPlan := DefaultParallelPlanEnabled
abortOnExcecutionOrderFail := DefaultAbortOnExcecutionOrderFail
if hasRepoCfg {
automerge = repoCfg.Automerge
parallelApply = repoCfg.ParallelApply
parallelPlan = repoCfg.ParallelPlan
abortOnExcecutionOrderFail = repoCfg.AbortOnExcecutionOrderFail
}

pCfg := p.GlobalCfg.DefaultProjCfg(ctx.Log, ctx.Pull.BaseRepo.ID(), mp.Path, pWorkspace)

projCtxs = append(projCtxs,
Expand Down Expand Up @@ -738,14 +755,20 @@ func (p *DefaultProjectCommandBuilder) buildProjectCommandCtx(ctx *command.Conte
}
var projCtxs []command.ProjectContext
var projCfg valid.MergedProjectCfg
automerge := DefaultAutomergeEnabled
parallelApply := DefaultParallelApplyEnabled
parallelPlan := DefaultParallelPlanEnabled
automerge := p.EnableAutoMerge
parallelApply := p.EnableParallelApply
parallelPlan := p.EnableParallelPlan
abortOnExcecutionOrderFail := DefaultAbortOnExcecutionOrderFail
if repoCfgPtr != nil {
automerge = repoCfgPtr.Automerge
parallelApply = repoCfgPtr.ParallelApply
parallelPlan = repoCfgPtr.ParallelPlan
if repoCfgPtr.Automerge != nil {
automerge = *repoCfgPtr.Automerge
}
if repoCfgPtr.ParallelApply != nil {
parallelApply = *repoCfgPtr.ParallelApply
}
if repoCfgPtr.ParallelPlan != nil {
parallelPlan = *repoCfgPtr.ParallelPlan
}
abortOnExcecutionOrderFail = *&repoCfgPtr.AbortOnExcecutionOrderFail
}

Expand Down
12 changes: 12 additions & 0 deletions server/events/project_command_builder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@ projects:
&CommentParser{ExecutableName: "atlantis"},
false,
false,
false,
false,
false,
"",
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
Expand Down Expand Up @@ -876,6 +879,9 @@ projects:
&CommentParser{ExecutableName: "atlantis"},
false,
true,
false,
false,
false,
"",
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
Expand Down Expand Up @@ -1121,6 +1127,9 @@ workflows:
&CommentParser{ExecutableName: "atlantis"},
false,
false,
false,
false,
false,
"",
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
Expand Down Expand Up @@ -1273,6 +1282,9 @@ projects:
&CommentParser{ExecutableName: "atlantis"},
false,
false,
false,
false,
false,
"",
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
Expand Down
Loading

0 comments on commit 99b1fe0

Please sign in to comment.