Skip to content

Commit db9601d

Browse files
authored
Merge pull request #45 from zapier/CLOUD-257-fix-resolving-file-paths-with-common-suffix
Fix Resolving File Paths with a Common Suffix
2 parents 6e1c4d6 + a8882b7 commit db9601d

2 files changed

Lines changed: 76 additions & 13 deletions

File tree

pkg/tfc_trigger/project_config.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,38 @@ type ProjectConfig struct {
2424
Workspaces []*TFCWorkspace `yaml:"workspaces"`
2525
}
2626

27+
// Finds the workspace with the deepest matching directory suffix.
28+
// For example, given workspaces with config "terraform/dev/" and a dir of "dev/",
29+
// and a filepath directory of "filesystem/terraform/dev/" - "dev" and "terraform/dev/" are both a suffix
30+
// we would return the ws for "terraform/dev/"as it is the deepest match.
31+
// Special case: if a workspace has config "/" and the input dir is ".", that workspace is returned.
32+
// If no workspace matches, returns nil.
2733
func (cfg *ProjectConfig) workspaceForDir(dir string) *TFCWorkspace {
34+
var longestMatch *TFCWorkspace
35+
var longestMatchDepth int
36+
2837
for _, ws := range cfg.Workspaces {
2938
wsDir := ws.Dir
3039
if !strings.HasSuffix(wsDir, "/") {
3140
wsDir += "/"
3241
}
3342

34-
if strings.HasSuffix(dir, wsDir) {
35-
return ws
36-
} else if wsDir != "/" && strings.HasSuffix(dir+"/", wsDir) {
37-
return ws
38-
} else if dir == "." && wsDir == "/" {
39-
return ws
43+
if wsDir == "/" {
44+
if dir == "." {
45+
return ws
46+
}
47+
continue
4048
}
4149

50+
if (strings.HasSuffix(dir+"/", wsDir) || strings.HasSuffix(dir, wsDir)) {
51+
wsDirDepth := len(strings.Split(wsDir, "/"))
52+
if wsDirDepth > longestMatchDepth {
53+
longestMatch = ws
54+
longestMatchDepth = wsDirDepth
55+
}
56+
}
4257
}
43-
return nil
58+
return longestMatch
4459
}
4560

4661
func (cfg *ProjectConfig) workspacesForTriggerDir(dir string) []*TFCWorkspace {

pkg/tfc_trigger/project_config_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,51 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) {
365365
},
366366
},
367367
{
368-
name: "subdir-and-dir-same-name",
369-
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
368+
name: "dir-and-subdir-same-name--dir-change",
369+
cfgYaml: tfbuddyYamlDirAndSubdirSameName,
370370
args: args{
371371
modifiedFiles: []string{
372372
"workspaces/main.tf",
373373
},
374374
},
375375
want: []*TFCWorkspace{
376-
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1],
376+
testLoadConfig(t, tfbuddyYamlDirAndSubdirSameName).Workspaces[0], // "workspaces" workspace
377+
},
378+
},
379+
{
380+
name: "dir-and-subdir-same-name--subdir-change",
381+
cfgYaml: tfbuddyYamlDirAndSubdirSameName,
382+
args: args{
383+
modifiedFiles: []string{
384+
"aws/workspaces/main.tf",
385+
},
386+
},
387+
want: []*TFCWorkspace{
388+
testLoadConfig(t, tfbuddyYamlDirAndSubdirSameName).Workspaces[1], // "aws/workspaces" workspace
389+
},
390+
},
391+
{
392+
name: "subdir-and-dir-same-name--dir-change",
393+
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
394+
args: args{
395+
modifiedFiles: []string{
396+
"test2/test3/main.tf",
397+
},
398+
},
399+
want: []*TFCWorkspace{
400+
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1], // "test2/test3" workspace
401+
},
402+
},
403+
{
404+
name: "subdir-and-dir-same-name--subdir-change",
405+
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
406+
args: args{
407+
modifiedFiles: []string{
408+
"test1/test2/test3/main.tf",
409+
},
410+
},
411+
want: []*TFCWorkspace{
412+
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[0], // "test1/test2/test3" workspace
377413
},
378414
},
379415
{
@@ -718,15 +754,27 @@ workspaces:
718754
719755
`
720756

721-
const tfbuddyYamlSubdirAndDirSameName = `
757+
const tfbuddyYamlDirAndSubdirSameName = `
722758
---
723759
workspaces:
760+
- name: workspaces
761+
organization: foo-corp
762+
dir: workspaces
724763
- name: aws-workspaces
725764
organization: foo-corp
726765
dir: aws/workspaces
727-
- name: workspaces
766+
767+
`
768+
769+
const tfbuddyYamlSubdirAndDirSameName = `
770+
---
771+
workspaces:
772+
- name: subdir
728773
organization: foo-corp
729-
dir: workspaces
774+
dir: test1/test2/test3
775+
- name: dir
776+
organization: foo-corp
777+
dir: test2/test3
730778
731779
`
732780

0 commit comments

Comments
 (0)