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

fix: auto-discover workspaces when in subdirectories #4363

Merged
merged 1 commit into from
Mar 16, 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
5 changes: 3 additions & 2 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,10 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
len(modifiedProjects), modifiedProjects)
for _, mp := range modifiedProjects {
ctx.Log.Debug("determining config for project at dir: %q", mp.Path)
pWorkspace, err := p.ProjectFinder.DetermineWorkspaceFromHCL(ctx.Log, repoDir)
absProjectDir := filepath.Join(repoDir, mp.Path)
pWorkspace, err := p.ProjectFinder.DetermineWorkspaceFromHCL(ctx.Log, absProjectDir)
if err != nil {
return nil, errors.Wrapf(err, "looking for Terraform Cloud workspace from configuration %s", repoDir)
return nil, errors.Wrapf(err, "looking for Terraform Cloud workspace from configuration %s", absProjectDir)
}

pCfg := p.GlobalCfg.DefaultProjCfg(ctx.Log, ctx.Pull.BaseRepo.ID(), mp.Path, pWorkspace)
Expand Down
99 changes: 89 additions & 10 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ var defaultUserConfig = struct {
AutoDiscoverMode: "auto",
}

func ChangedFiles(dirStructure map[string]interface{}, parent string) []string {
var files []string
for k, v := range dirStructure {
switch v := v.(type) {
case map[string]interface{}:
files = append(files, ChangedFiles(v, k)...)
default:
files = append(files, filepath.Join(parent, k))
}
}
return files
}

func TestDefaultProjectCommandBuilder_BuildAutoplanCommands(t *testing.T) {
// expCtxFields define the ctx fields we're going to assert on.
// Since we're focused on autoplanning here, we don't validate all the
Expand All @@ -57,11 +70,16 @@ func TestDefaultProjectCommandBuilder_BuildAutoplanCommands(t *testing.T) {
RepoRelDir string
Workspace string
}
defaultTestDirStructure := map[string]interface{}{
"main.tf": nil,
}

cases := []struct {
Description string
AtlantisYAML string
ServerSideYAML string
exp []expCtxFields
Description string
AtlantisYAML string
ServerSideYAML string
TestDirStructure map[string]interface{}
exp []expCtxFields
}{
{
Description: "simple atlantis.yaml",
Expand All @@ -70,6 +88,7 @@ version: 3
projects:
- dir: .
`,
TestDirStructure: defaultTestDirStructure,
exp: []expCtxFields{
{
ProjectName: "",
Expand All @@ -94,6 +113,7 @@ projects:
name: myname
workspace: myworkspace2
`,
TestDirStructure: defaultTestDirStructure,
exp: []expCtxFields{
{
ProjectName: "",
Expand Down Expand Up @@ -122,6 +142,7 @@ projects:
- dir: .
workspace: myworkspace2
`,
TestDirStructure: defaultTestDirStructure,
exp: []expCtxFields{
{
ProjectName: "",
Expand All @@ -142,7 +163,68 @@ version: 3
projects:
- dir: mydir
`,
exp: nil,
TestDirStructure: defaultTestDirStructure,
exp: nil,
},
{
Description: "workspaces from subdirectories detected",
TestDirStructure: map[string]interface{}{
"work": map[string]interface{}{
"main.tf": `
terraform {
cloud {
organization = "atlantis-test"
workspaces {
name = "test-workspace1"
}
}
}`,
},
"test": map[string]interface{}{
"main.tf": `
terraform {
cloud {
organization = "atlantis-test"
workspaces {
name = "test-workspace12"
}
}
}`,
},
},
exp: []expCtxFields{
{
ProjectName: "",
RepoRelDir: "work",
Workspace: "test-workspace1",
},
{
ProjectName: "",
RepoRelDir: "test",
Workspace: "test-workspace12",
},
},
},
{
Description: "workspaces in parent directory are detected",
TestDirStructure: map[string]interface{}{
"main.tf": `
terraform {
cloud {
organization = "atlantis-test"
workspaces {
name = "test-workspace"
}
}
}`,
},
exp: []expCtxFields{
{
ProjectName: "",
RepoRelDir: ".",
Workspace: "test-workspace",
},
},
},
}

Expand All @@ -156,15 +238,12 @@ projects:
for _, c := range cases {
t.Run(c.Description, func(t *testing.T) {
RegisterMockTestingT(t)
tmpDir := DirStructure(t, map[string]interface{}{
"main.tf": nil,
})

tmpDir := DirStructure(t, c.TestDirStructure)
workingDir := mocks.NewMockWorkingDir()
When(workingDir.Clone(Any[models.Repo](), Any[models.PullRequest](), Any[string]())).ThenReturn(tmpDir, false, nil)
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(
Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest]())).ThenReturn([]string{"main.tf"}, nil)
Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest]())).ThenReturn(ChangedFiles(c.TestDirStructure, ""), nil)
if c.AtlantisYAML != "" {
err := os.WriteFile(filepath.Join(tmpDir, valid.DefaultAtlantisFile), []byte(c.AtlantisYAML), 0600)
Ok(t, err)
Expand Down
Loading