From 186db5463ec3d65397f35f4df0d7a6103a751603 Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Thu, 28 Nov 2024 16:20:53 +0000 Subject: [PATCH 1/6] fix: Fix Suffix resolution with paths containing a common suffix Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/tfc_trigger/project_config.go b/pkg/tfc_trigger/project_config.go index eeac779..15c5219 100644 --- a/pkg/tfc_trigger/project_config.go +++ b/pkg/tfc_trigger/project_config.go @@ -25,22 +25,28 @@ type ProjectConfig struct { } 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 == "/" { + if dir == "." && wsDir == "/" { return ws } + if 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 { From b10b9b85432f0e32e806d8ce0bc7a26d25058d7d Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Thu, 28 Nov 2024 17:59:01 +0000 Subject: [PATCH 2/6] fix: bugs with root / Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/tfc_trigger/project_config.go b/pkg/tfc_trigger/project_config.go index 15c5219..6876dc1 100644 --- a/pkg/tfc_trigger/project_config.go +++ b/pkg/tfc_trigger/project_config.go @@ -34,12 +34,19 @@ func (cfg *ProjectConfig) workspaceForDir(dir string) *TFCWorkspace { wsDir += "/" } - if dir == "." && wsDir == "/" { - return ws + if wsDir == "/" { + if dir == "." { + return ws + } + continue } - if strings.HasSuffix(dir+"/", wsDir) { + if (strings.HasSuffix(dir+"/", wsDir) || strings.HasSuffix(dir, wsDir)) { wsDirDepth := len(strings.Split(wsDir, "/")) + fmt.Println(dir) + fmt.Println(wsDir) + fmt.Println(strings.Split(wsDir, "/")) + fmt.Println(wsDirDepth) if wsDirDepth > longestMatchDepth { longestMatch = ws longestMatchDepth = wsDirDepth From c53bb3ef64265ae1e8bee3febb8be3310ab94734 Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Thu, 28 Nov 2024 18:12:48 +0000 Subject: [PATCH 3/6] fix: Update unit tests Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config_test.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/tfc_trigger/project_config_test.go b/pkg/tfc_trigger/project_config_test.go index 2a6be6c..b6fda53 100644 --- a/pkg/tfc_trigger/project_config_test.go +++ b/pkg/tfc_trigger/project_config_test.go @@ -365,7 +365,7 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) { }, }, { - name: "subdir-and-dir-same-name", + name: "subdir-and-dir-same-name--dir-change", cfgYaml: tfbuddyYamlSubdirAndDirSameName, args: args{ modifiedFiles: []string{ @@ -373,7 +373,19 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) { }, }, want: []*TFCWorkspace{ - testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], + testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[0], // "workspaces" workspace + }, + }, + { + name: "subdir-and-dir-same-name--subdir-change", + cfgYaml: tfbuddyYamlSubdirAndDirSameName, + args: args{ + modifiedFiles: []string{ + "aws/workspaces/main.tf", + }, + }, + want: []*TFCWorkspace{ + testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], // "aws/workspaces" workspace }, }, { @@ -721,12 +733,12 @@ workspaces: const tfbuddyYamlSubdirAndDirSameName = ` --- workspaces: - - name: aws-workspaces - organization: foo-corp - dir: aws/workspaces - name: workspaces organization: foo-corp dir: workspaces + - name: aws-workspaces + organization: foo-corp + dir: aws/workspaces ` From 075b62a74de87a3d8bf13281fb395e9009d450bb Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Thu, 28 Nov 2024 18:14:31 +0000 Subject: [PATCH 4/6] fix: remove debuging Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/tfc_trigger/project_config.go b/pkg/tfc_trigger/project_config.go index 6876dc1..054c4c4 100644 --- a/pkg/tfc_trigger/project_config.go +++ b/pkg/tfc_trigger/project_config.go @@ -43,10 +43,6 @@ func (cfg *ProjectConfig) workspaceForDir(dir string) *TFCWorkspace { if (strings.HasSuffix(dir+"/", wsDir) || strings.HasSuffix(dir, wsDir)) { wsDirDepth := len(strings.Split(wsDir, "/")) - fmt.Println(dir) - fmt.Println(wsDir) - fmt.Println(strings.Split(wsDir, "/")) - fmt.Println(wsDirDepth) if wsDirDepth > longestMatchDepth { longestMatch = ws longestMatchDepth = wsDirDepth From 1fe91d21c8c07a557d3e64c5598397b718ac60c9 Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Thu, 28 Nov 2024 19:16:37 +0000 Subject: [PATCH 5/6] fix: Add some more tests Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config_test.go | 46 +++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/pkg/tfc_trigger/project_config_test.go b/pkg/tfc_trigger/project_config_test.go index b6fda53..419e45d 100644 --- a/pkg/tfc_trigger/project_config_test.go +++ b/pkg/tfc_trigger/project_config_test.go @@ -364,16 +364,40 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) { testLoadConfig(t, tfbuddyYamlSharedTriggerDirMultipleWorkspaces).Workspaces[1], }, }, + { + name: "dir-and-subdir-same-name--dir-change", + cfgYaml: tfbuddyYamlDirAndSubdirSameName, + args: args{ + modifiedFiles: []string{ + "workspaces/main.tf", + }, + }, + want: []*TFCWorkspace{ + 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{ - "workspaces/main.tf", + "test2/test3/main.tf", }, }, want: []*TFCWorkspace{ - testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[0], // "workspaces" workspace + testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], // "test2/test3" workspace }, }, { @@ -381,11 +405,11 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) { cfgYaml: tfbuddyYamlSubdirAndDirSameName, args: args{ modifiedFiles: []string{ - "aws/workspaces/main.tf", + "test1/test2/test3/main.tf", }, }, want: []*TFCWorkspace{ - testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], // "aws/workspaces" workspace + testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[0], // "test1/test2/test3" workspace }, }, { @@ -730,7 +754,7 @@ workspaces: ` -const tfbuddyYamlSubdirAndDirSameName = ` +const tfbuddyYamlDirAndSubdirSameName = ` --- workspaces: - name: workspaces @@ -742,6 +766,18 @@ workspaces: ` +const tfbuddyYamlSubdirAndDirSameName = ` +--- +workspaces: + - name: subdir + organization: foo-corp + dir: test1/test2/test3 + - name: dir + organization: foo-corp + dir: test2/test3 + +` + const tfbuddyYamlDifferentDirSameSubdir = ` --- workspaces: From a8882b74c048ee82bca031bf3a429cc9697a1e72 Mon Sep 17 00:00:00 2001 From: Sam Plant Date: Fri, 29 Nov 2024 12:31:49 +0000 Subject: [PATCH 6/6] fix: Added function docs Signed-off-by: Sam Plant --- pkg/tfc_trigger/project_config.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/tfc_trigger/project_config.go b/pkg/tfc_trigger/project_config.go index 054c4c4..fcc5613 100644 --- a/pkg/tfc_trigger/project_config.go +++ b/pkg/tfc_trigger/project_config.go @@ -24,6 +24,12 @@ 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