Skip to content

Commit

Permalink
Merge pull request #45 from zapier/CLOUD-257-fix-resolving-file-paths…
Browse files Browse the repository at this point in the history
…-with-common-suffix

Fix Resolving File Paths with a Common Suffix
  • Loading branch information
sampsre authored Nov 29, 2024
2 parents 6e1c4d6 + a8882b7 commit db9601d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
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

0 comments on commit db9601d

Please sign in to comment.