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

Cloud 257 Fix Resolving File Paths with a Common Suffix #45

Merged
merged 6 commits into from
Nov 29, 2024
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
29 changes: 22 additions & 7 deletions pkg/tfc_trigger/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,38 @@ type ProjectConfig struct {
Workspaces []*TFCWorkspace `yaml:"workspaces"`
}

// Finds the workspace with the deepest matching directory suffix.
// For example, given workspaces with config "terraform/dev/" and a dir of "dev/",
// and a filepath directory of "filesystem/terraform/dev/" - "dev" and "terraform/dev/" are both a suffix
// we would return the ws for "terraform/dev/"as it is the deepest match.
// Special case: if a workspace has config "/" and the input dir is ".", that workspace is returned.
// If no workspace matches, returns nil.
func (cfg *ProjectConfig) workspaceForDir(dir string) *TFCWorkspace {
var longestMatch *TFCWorkspace
var longestMatchDepth int

for _, ws := range cfg.Workspaces {
wsDir := ws.Dir
if !strings.HasSuffix(wsDir, "/") {
wsDir += "/"
}

if strings.HasSuffix(dir, wsDir) {
return ws
} else if wsDir != "/" && strings.HasSuffix(dir+"/", wsDir) {
return ws
} else if dir == "." && wsDir == "/" {
return ws
if wsDir == "/" {
if dir == "." {
return ws
}
continue
}

if (strings.HasSuffix(dir+"/", wsDir) || strings.HasSuffix(dir, wsDir)) {
wsDirDepth := len(strings.Split(wsDir, "/"))
if wsDirDepth > longestMatchDepth {
longestMatch = ws
longestMatchDepth = wsDirDepth
}
}
}
return nil
return longestMatch
}

func (cfg *ProjectConfig) workspacesForTriggerDir(dir string) []*TFCWorkspace {
Expand Down
60 changes: 54 additions & 6 deletions pkg/tfc_trigger/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,51 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) {
},
},
{
name: "subdir-and-dir-same-name",
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
name: "dir-and-subdir-same-name--dir-change",
cfgYaml: tfbuddyYamlDirAndSubdirSameName,
args: args{
modifiedFiles: []string{
"workspaces/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1],
testLoadConfig(t, tfbuddyYamlDirAndSubdirSameName).Workspaces[0], // "workspaces" workspace
},
},
{
name: "dir-and-subdir-same-name--subdir-change",
cfgYaml: tfbuddyYamlDirAndSubdirSameName,
args: args{
modifiedFiles: []string{
"aws/workspaces/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlDirAndSubdirSameName).Workspaces[1], // "aws/workspaces" workspace
},
},
{
name: "subdir-and-dir-same-name--dir-change",
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
args: args{
modifiedFiles: []string{
"test2/test3/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], // "test2/test3" workspace
},
},
{
name: "subdir-and-dir-same-name--subdir-change",
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
args: args{
modifiedFiles: []string{
"test1/test2/test3/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[0], // "test1/test2/test3" workspace
},
},
{
Expand Down Expand Up @@ -718,15 +754,27 @@ workspaces:

`

const tfbuddyYamlSubdirAndDirSameName = `
const tfbuddyYamlDirAndSubdirSameName = `
---
workspaces:
- name: workspaces
organization: foo-corp
dir: workspaces
- name: aws-workspaces
organization: foo-corp
dir: aws/workspaces
- name: workspaces

`

const tfbuddyYamlSubdirAndDirSameName = `
---
workspaces:
- name: subdir
organization: foo-corp
dir: workspaces
dir: test1/test2/test3
- name: dir
organization: foo-corp
dir: test2/test3

`

Expand Down
Loading