Skip to content

Commit

Permalink
add flag to allow the user disable the autodiscover
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Medeiros committed Oct 13, 2023
1 parent 27010d3 commit 1b9ca74
Show file tree
Hide file tree
Showing 16 changed files with 542 additions and 167 deletions.
1 change: 1 addition & 0 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
false,
false,
false,
valid.Autodiscover{Enabled: true},
false,
false,
"",
Expand Down
63 changes: 54 additions & 9 deletions server/core/config/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ workflows:
StateRm: valid.DefaultStateRmStage,
},
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},

Expand All @@ -190,9 +191,10 @@ workflows:
version: 3
projects:`,
exp: valid.RepoCfg{
Version: 3,
Projects: nil,
Workflows: map[string]valid.Workflow{},
Version: 3,
Projects: nil,
Workflows: map[string]valid.Workflow{},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -224,7 +226,8 @@ projects:
ApplyRequirements: nil,
},
},
Workflows: map[string]valid.Workflow{},
Workflows: map[string]valid.Workflow{},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand All @@ -246,7 +249,8 @@ projects:
},
},
},
Workflows: make(map[string]valid.Workflow),
Workflows: make(map[string]valid.Workflow),
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand All @@ -270,7 +274,32 @@ projects:
},
},
},
Workflows: make(map[string]valid.Workflow),
Workflows: make(map[string]valid.Workflow),
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
description: "autodiscover should be enabled by default",
input: `
version: 3
`,
exp: valid.RepoCfg{
Version: 3,
Autodiscover: &valid.Autodiscover{Enabled: true},
Workflows: make(map[string]valid.Workflow),
},
},
{
description: "disable autodiscover",
input: `
version: 3
autodiscover:
enabled: false
`,
exp: valid.RepoCfg{
Version: 3,
Autodiscover: &valid.Autodiscover{Enabled: false},
Workflows: make(map[string]valid.Workflow),
},
},
{
Expand All @@ -292,7 +321,8 @@ projects:
},
},
},
Workflows: make(map[string]valid.Workflow),
Workflows: make(map[string]valid.Workflow),
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand All @@ -315,7 +345,8 @@ workflows: ~
},
},
},
Workflows: make(map[string]valid.Workflow),
Workflows: make(map[string]valid.Workflow),
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -346,6 +377,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"default": defaultWorkflow("default"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -378,6 +410,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -412,6 +445,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -446,6 +480,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -480,6 +515,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -514,6 +550,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -548,6 +585,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -582,6 +620,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -616,6 +655,7 @@ workflows:
Workflows: map[string]valid.Workflow{
"myworkflow": defaultWorkflow("myworkflow"),
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -731,7 +771,8 @@ projects:
},
},
},
Workflows: map[string]valid.Workflow{},
Workflows: map[string]valid.Workflow{},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -822,6 +863,7 @@ workflows:
},
},
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -925,6 +967,7 @@ workflows:
},
},
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -1008,6 +1051,7 @@ workflows:
},
},
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
{
Expand Down Expand Up @@ -1106,6 +1150,7 @@ workflows:
},
},
},
Autodiscover: &valid.Autodiscover{Enabled: true},
},
},
}
Expand Down
32 changes: 32 additions & 0 deletions server/core/config/raw/autodiscover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package raw

import (
"github.com/runatlantis/atlantis/server/core/config/valid"
)

type Autodiscover struct {
Enabled *bool `yaml:"enabled,omitempty"`
}

func (a Autodiscover) ToValid() valid.Autodiscover {
var v valid.Autodiscover

if a.Enabled == nil {
v.Enabled = true
} else {
v.Enabled = *a.Enabled
}

return v
}

func (a Autodiscover) Validate() error {
return nil
}

// DefaultAutoDiscover returns the default autodiscover config.
func DefaultAutoDiscover() valid.Autodiscover {
return valid.Autodiscover{
Enabled: valid.DefaultAutoDiscoverEnabled,
}
}
115 changes: 115 additions & 0 deletions server/core/config/raw/autodiscover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package raw_test

import (
"testing"

"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestAutoDiscover_UnmarshalYAML(t *testing.T) {
cases := []struct {
description string
input string
exp raw.Autodiscover
}{
{
description: "omit unset fields",
input: "",
exp: raw.Autodiscover{
Enabled: nil,
},
},
{
description: "enabled true",
input: `
enabled: true
`,
exp: raw.Autodiscover{
Enabled: Bool(true),
},
},
{
description: "enabled false",
input: `
enabled: false
`,
exp: raw.Autodiscover{
Enabled: Bool(false),
},
},
}

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var a raw.Autodiscover
err := yaml.UnmarshalStrict([]byte(c.input), &a)
Ok(t, err)
Equals(t, c.exp, a)
})
}
}

func TestAutodiscover_Validate(t *testing.T) {
cases := []struct {
description string
input raw.Autodiscover
}{
{
description: "nothing set",
input: raw.Autodiscover{},
},
{
description: "enabled false",
input: raw.Autodiscover{
Enabled: Bool(false),
},
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
Ok(t, c.input.Validate())
})
}
}

func TestAutodiscover_ToValid(t *testing.T) {
cases := []struct {
description string
input raw.Autodiscover
exp valid.Autodiscover
}{
{
description: "nothing set",
input: raw.Autodiscover{},
exp: valid.Autodiscover{
Enabled: true,
},
},
{
description: "enabled false",
input: raw.Autodiscover{
Enabled: Bool(false),
},
exp: valid.Autodiscover{
Enabled: false,
},
},
{
description: "enabled true",
input: raw.Autodiscover{
Enabled: Bool(true),
},
exp: valid.Autodiscover{
Enabled: true,
},
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
Equals(t, c.exp, c.input.ToValid())
})
}
}
11 changes: 11 additions & 0 deletions server/core/config/raw/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RepoCfg struct {
Workflows map[string]Workflow `yaml:"workflows,omitempty"`
PolicySets PolicySets `yaml:"policies,omitempty"`
Automerge *bool `yaml:"automerge,omitempty"`
Autodiscover *Autodiscover `yaml:"autodiscover,omitempty"`
ParallelApply *bool `yaml:"parallel_apply,omitempty"`
ParallelPlan *bool `yaml:"parallel_plan,omitempty"`
DeleteSourceBranchOnMerge *bool `yaml:"delete_source_branch_on_merge,omitempty"`
Expand Down Expand Up @@ -57,6 +58,15 @@ func (r RepoCfg) ToValid() valid.RepoCfg {
validProjects = append(validProjects, p.ToValid())
}

var autodiscover *valid.Autodiscover
if r.Autodiscover == nil {
defaultAutoDiscover := DefaultAutoDiscover()
autodiscover = &defaultAutoDiscover
} else {
validAutoDiscover := r.Autodiscover.ToValid()
autodiscover = &validAutoDiscover
}

automerge := r.Automerge
parallelApply := r.ParallelApply
parallelPlan := r.ParallelPlan
Expand All @@ -76,6 +86,7 @@ func (r RepoCfg) ToValid() valid.RepoCfg {
Projects: validProjects,
Workflows: validWorkflows,
Automerge: automerge,
Autodiscover: autodiscover,
ParallelApply: parallelApply,
ParallelPlan: parallelPlan,
ParallelPolicyCheck: parallelPlan,
Expand Down
Loading

0 comments on commit 1b9ca74

Please sign in to comment.