From ce72bd4019fc8148af56b101d0fc037d52a05f10 Mon Sep 17 00:00:00 2001 From: Leon Sodhi Date: Mon, 18 Nov 2019 19:09:43 +0000 Subject: [PATCH] Allow modified files to be excluded --- server/events/project_finder.go | 21 +++++++++--- server/events/project_finder_test.go | 48 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/server/events/project_finder.go b/server/events/project_finder.go index f14cb1718..b0db61718 100644 --- a/server/events/project_finder.go +++ b/server/events/project_finder.go @@ -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 { diff --git a/server/events/project_finder_test.go b/server/events/project_finder_test.go index 5a0d0304d..5b10325e4 100644 --- a/server/events/project_finder_test.go +++ b/server/events/project_finder_test.go @@ -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 {