Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support changes in modules/ directories #211

Merged
merged 1 commit into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions server/events/project_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ProjectFinder interface {
// DefaultProjectFinder implements ProjectFinder.
type DefaultProjectFinder struct{}

var excludeList = []string{"terraform.tfstate", "terraform.tfstate.backup", "_modules", "modules"}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need _modules because it's not a common directory for modules. Googling for it shows only Hootsuite uses.

var excludeList = []string{"terraform.tfstate", "terraform.tfstate.backup"}

// FindModified returns the list of projects that were modified based on
// the modifiedFiles. The list will be de-duplicated.
Expand All @@ -31,7 +31,7 @@ func (p *DefaultProjectFinder) FindModified(log *logging.SimpleLogger, modifiedF
if len(modifiedTerraformFiles) == 0 {
return projects
}
log.Info("filtered modified files to %d non-module .tf files: %v",
log.Info("filtered modified files to %d .tf files: %v",
len(modifiedTerraformFiles), modifiedTerraformFiles)

var paths []string
Expand Down Expand Up @@ -67,15 +67,22 @@ func (p *DefaultProjectFinder) isInExcludeList(fileName string) bool {
}

// getProjectPath returns the path to the project relative to the repo root
// if the project is at the root returns "."
// if the project is at the root returns ".".
func (p *DefaultProjectFinder) getProjectPath(modifiedFilePath string) string {
dir := path.Dir(modifiedFilePath)
if path.Base(dir) == "env" {
// If the modified file was inside an env/ directory, we treat this
// specially and run plan one level up.
return path.Dir(dir)
}
return dir
// Need to add a trailing slash before splitting on modules/ because if
// the input was modules/file.tf then path.Dir will be "modules" and so our
// split on "modules/" will fail.
dirWithTrailingSlash := dir + "/"
// It's safe to do this split even if there is no modules/ in the path
// because SplitN will just return the original string.
modulesSplit := strings.SplitN(dirWithTrailingSlash, "modules/", 2)
return path.Clean(modulesSplit[0])
}

func (p *DefaultProjectFinder) unique(strs []string) []string {
Expand Down
16 changes: 13 additions & 3 deletions server/events/project_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var noopLogger = logging.NewNoopLogger()
var modifiedRepo = "owner/repo"
var m = events.DefaultProjectFinder{}

func TestGetModified_NoFiles(t *testing.T) {
func TestGetModified(t *testing.T) {
cases := []struct {
description string
files []string
Expand All @@ -29,10 +29,20 @@ func TestGetModified_NoFiles(t *testing.T) {
nil,
},
{
"Should ignore .tf files in module directories",
[]string{"_modules/file.tf", "modules/file.tf", "parent/_modules/file.tf", "parent/modules/file.tf", "main.tf"},
"Should plan in the parent directory from modules",
[]string{"modules/file.tf"},
[]string{"."},
},
{
"Should plan in the parent directory from modules when module is in a subdir",
[]string{"modules/subdir/file.tf"},
[]string{"."},
},
{
"Should plan in the parent directory from modules when project is in its own dir",
[]string{"projectdir/modules/file.tf"},
[]string{"projectdir"},
},
{
"Should ignore tfstate files and return an empty list",
[]string{"terraform.tfstate", "terraform.tfstate.backup", "parent/terraform.tfstate", "parent/terraform.tfstate.backup"},
Expand Down