Skip to content

Commit 1750a67

Browse files
authored
Merge pull request #865 from runatlantis/when-modified
Allow when_modified files to be excluded
2 parents 6bf0624 + ce72bd4 commit 1750a67

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

server/events/project_finder.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,25 @@ func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLog
8282
var projects []valid.Project
8383
for _, project := range config.Projects {
8484
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)
85-
// Prepend project dir to when modified patterns because the patterns
86-
// are relative to the project dirs but our list of modified files is
87-
// relative to the repo root.
8885
var whenModifiedRelToRepoRoot []string
8986
for _, wm := range project.Autoplan.WhenModified {
90-
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, filepath.Join(project.Dir, wm))
87+
wm = strings.TrimSpace(wm)
88+
// An exclusion uses a '!' at the beginning. If it's there, we need
89+
// to remove it, then add in the project path, then add it back.
90+
exclusion := false
91+
if wm != "" && wm[0] == '!' {
92+
wm = wm[1:]
93+
exclusion = true
94+
}
95+
96+
// Prepend project dir to when modified patterns because the patterns
97+
// are relative to the project dirs but our list of modified files is
98+
// relative to the repo root.
99+
wmRelPath := filepath.Join(project.Dir, wm)
100+
if exclusion {
101+
wmRelPath = "!" + wmRelPath
102+
}
103+
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, wmRelPath)
91104
}
92105
pm, err := fileutils.NewPatternMatcher(whenModifiedRelToRepoRoot)
93106
if err != nil {

server/events/project_finder_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,54 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
396396
modified: []string{"project2/terraform.tfvars"},
397397
expProjPaths: []string{"project2"},
398398
},
399+
{
400+
description: "file excluded",
401+
config: valid.RepoCfg{
402+
Projects: []valid.Project{
403+
{
404+
Dir: "project1",
405+
Autoplan: valid.Autoplan{
406+
Enabled: true,
407+
WhenModified: []string{"*.tf", "!exclude-me.tf"},
408+
},
409+
},
410+
},
411+
},
412+
modified: []string{"project1/exclude-me.tf"},
413+
expProjPaths: nil,
414+
},
415+
{
416+
description: "some files excluded and others included",
417+
config: valid.RepoCfg{
418+
Projects: []valid.Project{
419+
{
420+
Dir: "project1",
421+
Autoplan: valid.Autoplan{
422+
Enabled: true,
423+
WhenModified: []string{"*.tf", "!exclude-me.tf"},
424+
},
425+
},
426+
},
427+
},
428+
modified: []string{"project1/exclude-me.tf", "project1/include-me.tf"},
429+
expProjPaths: []string{"project1"},
430+
},
431+
{
432+
description: "multiple dirs excluded",
433+
config: valid.RepoCfg{
434+
Projects: []valid.Project{
435+
{
436+
Dir: "project1",
437+
Autoplan: valid.Autoplan{
438+
Enabled: true,
439+
WhenModified: []string{"**/*.tf", "!subdir1/*", "!subdir2/*"},
440+
},
441+
},
442+
},
443+
},
444+
modified: []string{"project1/subdir1/main.tf", "project1/subdir2/main.tf"},
445+
expProjPaths: nil,
446+
},
399447
}
400448

401449
for _, c := range cases {

0 commit comments

Comments
 (0)