Skip to content

Commit

Permalink
Enable the feature with a server flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bewie committed Feb 27, 2021
1 parent 25741c4 commit 74f0ca1
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
DisableMarkdownFoldingFlag = "disable-markdown-folding"
DisableRepoLockingFlag = "disable-repo-locking"
EnablePolicyChecksFlag = "enable-policy-checks"
EnableRegExpCmdFlag = "enable-regexp-cmd"
GHHostnameFlag = "gh-hostname"
GHTokenFlag = "gh-token"
GHUserFlag = "gh-user"
Expand Down Expand Up @@ -300,6 +301,10 @@ var boolFlags = map[string]boolFlag{
description: "Enable atlantis to run user defined policy checks. This is explicitly disabled for TFE/TFC backends since plan files are inaccessible.",
defaultValue: false,
},
EnableRegExpCmdFlag: {
description: "Enable Atlantis to use regular expressions on plan/apply commands when \"-p\" flag is passed with it.",
defaultValue: false,
},
AllowDraftPRs: {
description: "Enable autoplan for Github Draft Pull Requests",
defaultValue: false,
Expand Down
1 change: 1 addition & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var testFlags = map[string]interface{}{
WriteGitCredsFlag: true,
DisableAutoplanFlag: true,
EnablePolicyChecksFlag: false,
EnableRegExpCmdFlag: false,
}

func TestExecute_Defaults(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewProjectCommandBuilder(
pendingPlanFinder *DefaultPendingPlanFinder,
commentBuilder CommentBuilder,
skipCloneNoChanges bool,
EnableRegExpCmd bool,
) *DefaultProjectCommandBuilder {
projectCommandBuilder := &DefaultProjectCommandBuilder{
ParserValidator: parserValidator,
Expand All @@ -48,6 +49,7 @@ func NewProjectCommandBuilder(
GlobalCfg: globalCfg,
PendingPlanFinder: pendingPlanFinder,
SkipCloneNoChanges: skipCloneNoChanges,
EnableRegExpCmd: EnableRegExpCmd,
ProjectCommandContextBuilder: NewProjectCommandContextBulder(
policyChecksSupported,
commentBuilder,
Expand Down Expand Up @@ -101,6 +103,7 @@ type DefaultProjectCommandBuilder struct {
PendingPlanFinder *DefaultPendingPlanFinder
ProjectCommandContextBuilder ProjectCommandContextBuilder
SkipCloneNoChanges bool
EnableRegExpCmd bool
}

// See ProjectCommandBuilder.BuildAutoplanCommands.
Expand Down Expand Up @@ -327,7 +330,13 @@ func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName s
// If they've specified a project by name we look it up. Otherwise we
// use the dir and workspace.
if projectName != "" {
projectsCfg = repoCfg.FindProjectsByName(projectName)
if p.EnableRegExpCmd {
projectsCfg = repoCfg.FindProjectsByName(projectName)
} else {
if p := repoCfg.FindProjectByName(projectName); p != nil {
projectsCfg = append(projectsCfg, *p)
}
}
if len(projectsCfg) == 0 {
err = fmt.Errorf("no project with name %q is defined in %s", projectName, yaml.AtlantisYAMLFilename)
return
Expand Down
187 changes: 187 additions & 0 deletions server/events/project_command_builder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ projects:
&DefaultPendingPlanFinder{},
&CommentParser{},
false,
false,
)

// We run a test for each type of command.
Expand Down Expand Up @@ -640,6 +641,191 @@ projects:
}
}

func TestBuildProjectCmdCtx_WithRegExpCmdEnabled(t *testing.T) {
emptyPolicySets := valid.PolicySets{
Version: nil,
PolicySets: []valid.PolicySet{},
}
baseRepo := models.Repo{
FullName: "owner/repo",
VCSHost: models.VCSHost{
Hostname: "github.com",
},
}
pull := models.PullRequest{
BaseRepo: baseRepo,
}
cases := map[string]struct {
globalCfg string
repoCfg string
expErr string
expCtx models.ProjectCommandContext
expPlanSteps []string
expApplySteps []string
}{

// Test that if we've set global defaults, that they are used but the
// allowed project config values also come through.
"global defaults with repo cfg": {
globalCfg: `
repos:
- id: /.*/
workflow: default
workflows:
default:
plan:
steps:
- init
- plan
apply:
steps:
- apply`,
repoCfg: `
version: 3
automerge: true
projects:
- name: myproject_1
dir: project1
workspace: myworkspace
autoplan:
enabled: true
when_modified: [../modules/**/*.tf]
terraform_version: v10.0
- name: myproject_2
dir: project2
workspace: myworkspace
autoplan:
enabled: true
when_modified: [../modules/**/*.tf]
terraform_version: v10.0
- name: myproject_3
dir: project3
workspace: myworkspace
autoplan:
enabled: true
when_modified: [../modules/**/*.tf]
terraform_version: v10.0
`,
expCtx: models.ProjectCommandContext{
ApplyCmd: "atlantis apply -p myproject_1",
BaseRepo: baseRepo,
EscapedCommentArgs: []string{`\f\l\a\g`},
AutomergeEnabled: true,
AutoplanEnabled: true,
HeadRepo: models.Repo{},
Log: nil,
PullMergeable: true,
Pull: pull,
ProjectName: "myproject_1",
ApplyRequirements: []string{},
RepoConfigVersion: 3,
RePlanCmd: "atlantis plan -p myproject_1 -- flag",
RepoRelDir: "project1",
TerraformVersion: mustVersion("10.0"),
User: models.User{},
Verbose: true,
Workspace: "myworkspace",
PolicySets: emptyPolicySets,
},
expPlanSteps: []string{"init", "plan"},
expApplySteps: []string{"apply"},
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
tmp, cleanup := DirStructure(t, map[string]interface{}{
"project1": map[string]interface{}{
"main.tf": nil,
},
"modules": map[string]interface{}{
"module": map[string]interface{}{
"main.tf": nil,
},
},
})
defer cleanup()

workingDir := NewMockWorkingDir()
When(workingDir.Clone(matchers.AnyPtrToLoggingSimpleLogger(), matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest(), AnyString())).ThenReturn(tmp, false, nil)
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{"modules/module/main.tf"}, nil)

// Write and parse the global config file.
globalCfgPath := filepath.Join(tmp, "global.yaml")
Ok(t, ioutil.WriteFile(globalCfgPath, []byte(c.globalCfg), 0600))
parser := &yaml.ParserValidator{}
globalCfg, err := parser.ParseGlobalCfg(globalCfgPath, valid.NewGlobalCfg(false, false, false))
Ok(t, err)

if c.repoCfg != "" {
Ok(t, ioutil.WriteFile(filepath.Join(tmp, "atlantis.yaml"), []byte(c.repoCfg), 0600))
}

builder := NewProjectCommandBuilder(
false,
parser,
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{},
false,
true,
)

// We run a test for each type of command, again specific projects
for _, cmd := range []models.CommandName{models.PlanCommand, models.ApplyCommand} {
t.Run(cmd.String(), func(t *testing.T) {
ctxs, err := builder.buildProjectCommandCtx(&CommandContext{
Pull: models.PullRequest{
BaseRepo: baseRepo,
},
PullMergeable: true,
}, cmd, "myproject_[1-2]", []string{"flag"}, tmp, "project1", "myworkspace", true)

if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
}
ctx := ctxs[0]

Ok(t, err)

Equals(t, 2, len(ctxs))
// Construct expected steps.
var stepNames []string
switch cmd {
case models.PlanCommand:
stepNames = c.expPlanSteps
case models.ApplyCommand:
stepNames = c.expApplySteps
}
var expSteps []valid.Step
for _, stepName := range stepNames {
expSteps = append(expSteps, valid.Step{
StepName: stepName,
})
}

c.expCtx.CommandName = cmd
// Init fields we couldn't in our cases map.
c.expCtx.Steps = expSteps
ctx.PolicySets = emptyPolicySets
Equals(t, c.expCtx, ctx)
// Equals() doesn't compare TF version properly so have to
// use .String().
if c.expCtx.TerraformVersion != nil {
Equals(t, c.expCtx.TerraformVersion.String(), ctx.TerraformVersion.String())
}
})
}
})
}
}

func TestBuildProjectCmdCtx_WithPolicCheckEnabled(t *testing.T) {
emptyPolicySets := valid.PolicySets{
Version: nil,
Expand Down Expand Up @@ -790,6 +976,7 @@ workflows:
&DefaultPendingPlanFinder{},
&CommentParser{},
false,
false,
)

cmd := models.PolicyCheckCommand
Expand Down
9 changes: 9 additions & 0 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

ctxs, err := builder.BuildAutoplanCommands(&events.CommandContext{
Expand Down Expand Up @@ -370,6 +371,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
true,
)

var actCtxs []models.ProjectCommandContext
Expand Down Expand Up @@ -506,6 +508,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

ctxs, err := builder.BuildPlanCommands(
Expand Down Expand Up @@ -580,6 +583,7 @@ func TestDefaultProjectCommandBuilder_BuildMultiApply(t *testing.T) {
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

ctxs, err := builder.BuildApplyCommands(
Expand Down Expand Up @@ -649,6 +653,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

ctx := &events.CommandContext{
Expand Down Expand Up @@ -713,6 +718,7 @@ func TestDefaultProjectCommandBuilder_EscapeArgs(t *testing.T) {
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

var actCtxs []models.ProjectCommandContext
Expand Down Expand Up @@ -879,6 +885,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

actCtxs, err := builder.BuildPlanCommands(
Expand Down Expand Up @@ -929,6 +936,7 @@ projects:
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
true,
false,
)

var actCtxs []models.ProjectCommandContext
Expand Down Expand Up @@ -969,6 +977,7 @@ func TestDefaultProjectCommandBuilder_WithPolicyCheckEnabled_BuildAutoplanComman
&events.DefaultPendingPlanFinder{},
&events.CommentParser{},
false,
false,
)

ctxs, err := builder.BuildAutoplanCommands(&events.CommandContext{
Expand Down
1 change: 1 addition & 0 deletions server/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ func setupE2E(t *testing.T, repoDir string, policyChecksEnabled bool) (server.Ev
&events.DefaultPendingPlanFinder{},
commentParser,
false,
false,
)

showStepRunner, err := runtime.NewShowStepRunner(terraformClient, defaultTFVersion)
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
pendingPlanFinder,
commentParser,
userConfig.SkipCloneNoChanges,
userConfig.EnableRegExpCmd,
)

showStepRunner, err := runtime.NewShowStepRunner(terraformClient, defaultTfVersion)
Expand Down
1 change: 1 addition & 0 deletions server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type UserConfig struct {
DisableMarkdownFolding bool `mapstructure:"disable-markdown-folding"`
DisableRepoLocking bool `mapstructure:"disable-repo-locking"`
EnablePolicyChecksFlag bool `mapstructure:"enable-policy-checks"`
EnableRegExpCmd bool `mapstructure:"enable-regexp-cmd"`
GithubHostname string `mapstructure:"gh-hostname"`
GithubToken string `mapstructure:"gh-token"`
GithubUser string `mapstructure:"gh-user"`
Expand Down

0 comments on commit 74f0ca1

Please sign in to comment.