Allow modified files to be excluded

This commit is contained in:
Leon Sodhi
2019-11-18 19:09:43 +00:00
committed by Luke Kysow
parent c5e8e2995c
commit ce72bd4019
2 changed files with 65 additions and 4 deletions

View File

@@ -82,12 +82,25 @@ func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLog
var projects []valid.Project
for _, project := range config.Projects {
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)
// Prepend project dir to when modified patterns because the patterns
// are relative to the project dirs but our list of modified files is
// relative to the repo root.
var whenModifiedRelToRepoRoot []string
for _, wm := range project.Autoplan.WhenModified {
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, filepath.Join(project.Dir, wm))
wm = strings.TrimSpace(wm)
// An exclusion uses a '!' at the beginning. If it's there, we need
// to remove it, then add in the project path, then add it back.
exclusion := false
if wm != "" && wm[0] == '!' {
wm = wm[1:]
exclusion = true
}
// Prepend project dir to when modified patterns because the patterns
// are relative to the project dirs but our list of modified files is
// relative to the repo root.
wmRelPath := filepath.Join(project.Dir, wm)
if exclusion {
wmRelPath = "!" + wmRelPath
}
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, wmRelPath)
}
pm, err := fileutils.NewPatternMatcher(whenModifiedRelToRepoRoot)
if err != nil {

View File

@@ -396,6 +396,54 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
modified: []string{"project2/terraform.tfvars"},
expProjPaths: []string{"project2"},
},
{
description: "file excluded",
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project1",
Autoplan: valid.Autoplan{
Enabled: true,
WhenModified: []string{"*.tf", "!exclude-me.tf"},
},
},
},
},
modified: []string{"project1/exclude-me.tf"},
expProjPaths: nil,
},
{
description: "some files excluded and others included",
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project1",
Autoplan: valid.Autoplan{
Enabled: true,
WhenModified: []string{"*.tf", "!exclude-me.tf"},
},
},
},
},
modified: []string{"project1/exclude-me.tf", "project1/include-me.tf"},
expProjPaths: []string{"project1"},
},
{
description: "multiple dirs excluded",
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project1",
Autoplan: valid.Autoplan{
Enabled: true,
WhenModified: []string{"**/*.tf", "!subdir1/*", "!subdir2/*"},
},
},
},
},
modified: []string{"project1/subdir1/main.tf", "project1/subdir2/main.tf"},
expProjPaths: nil,
},
}
for _, c := range cases {