Skip to content

Commit 8157a8d

Browse files
authored
fix: Workspace Error when include-git-untracked-files is true (#5288)
Signed-off-by: X-Guardian <[email protected]>
1 parent be06063 commit 8157a8d

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

server/events/project_command_builder.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -455,23 +455,17 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
455455
return nil, err
456456
}
457457

458-
if p.IncludeGitUntrackedFiles {
459-
ctx.Log.Debug(("'include-git-untracked-files' option is set, getting untracked files"))
460-
untrackedFiles, err := p.WorkingDir.GetGitUntrackedFiles(ctx.Log, ctx.HeadRepo, ctx.Pull, DefaultWorkspace)
458+
ctx.Log.Debug("%d files were modified in this pull request. Modified files: %v", len(modifiedFiles), modifiedFiles)
459+
460+
// If we're not including git untracked files, we can skip the clone if there are no modified files.
461+
if !p.IncludeGitUntrackedFiles {
462+
shouldSkipClone, err := p.shouldSkipClone(ctx, modifiedFiles)
461463
if err != nil {
462464
return nil, err
463465
}
464-
modifiedFiles = append(modifiedFiles, untrackedFiles...)
465-
}
466-
467-
ctx.Log.Debug("%d files were modified in this pull request. Modified files: %v", len(modifiedFiles), modifiedFiles)
468-
469-
shouldSkipClone, err := p.shouldSkipClone(ctx, modifiedFiles)
470-
if err != nil {
471-
return nil, err
472-
}
473-
if shouldSkipClone {
474-
return []command.ProjectContext{}, nil
466+
if shouldSkipClone {
467+
return []command.ProjectContext{}, nil
468+
}
475469
}
476470

477471
// Need to lock the workspace we're about to clone to.
@@ -490,6 +484,15 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
490484
return nil, err
491485
}
492486

487+
if p.IncludeGitUntrackedFiles {
488+
ctx.Log.Debug(("'include-git-untracked-files' option is set, getting untracked files"))
489+
untrackedFiles, err := p.WorkingDir.GetGitUntrackedFiles(ctx.Log, ctx.HeadRepo, ctx.Pull, DefaultWorkspace)
490+
if err != nil {
491+
return nil, err
492+
}
493+
modifiedFiles = append(modifiedFiles, untrackedFiles...)
494+
}
495+
493496
// Parse config file if it exists.
494497
repoCfgFile := p.GlobalCfg.RepoConfigFile(ctx.Pull.BaseRepo.ID())
495498
hasRepoCfg, err := p.ParserValidator.HasRepoCfg(repoDir, repoCfgFile)

server/events/project_command_builder_test.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var defaultUserConfig = struct {
4545
AutoplanFileList: "**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
4646
RestrictFileList: false,
4747
SilenceNoProjects: false,
48-
IncludeGitUntrackedFiles: true,
48+
IncludeGitUntrackedFiles: false,
4949
AutoDiscoverMode: "auto",
5050
}
5151

@@ -1695,27 +1695,40 @@ projects:
16951695
// Test that we don't clone the repo if there were no changes based on the atlantis.yaml file.
16961696
func TestDefaultProjectCommandBuilder_SkipCloneNoChanges(t *testing.T) {
16971697
cases := []struct {
1698-
AtlantisYAML string
1699-
ExpectedCtxs int
1700-
ExpectedClones InvocationCountMatcher
1701-
ModifiedFiles []string
1698+
AtlantisYAML string
1699+
ExpectedCtxs int
1700+
ExpectedClones InvocationCountMatcher
1701+
ModifiedFiles []string
1702+
IncludeGitUntrackedFiles bool
17021703
}{
17031704
{
17041705
AtlantisYAML: `
17051706
version: 3
17061707
projects:
17071708
- dir: dir1`,
1708-
ExpectedCtxs: 0,
1709-
ExpectedClones: Never(),
1710-
ModifiedFiles: []string{"dir2/main.tf"},
1709+
ExpectedCtxs: 0,
1710+
ExpectedClones: Never(),
1711+
ModifiedFiles: []string{"dir2/main.tf"},
1712+
IncludeGitUntrackedFiles: false,
1713+
},
1714+
{
1715+
AtlantisYAML: `
1716+
version: 3
1717+
projects:
1718+
- dir: dir1`,
1719+
ExpectedCtxs: 0,
1720+
ExpectedClones: Once(),
1721+
ModifiedFiles: []string{"dir2/main.tf"},
1722+
IncludeGitUntrackedFiles: true,
17111723
},
17121724
{
17131725
AtlantisYAML: `
17141726
version: 3
17151727
parallel_plan: true`,
1716-
ExpectedCtxs: 0,
1717-
ExpectedClones: Once(),
1718-
ModifiedFiles: []string{"README.md"},
1728+
ExpectedCtxs: 0,
1729+
ExpectedClones: Once(),
1730+
ModifiedFiles: []string{"README.md"},
1731+
IncludeGitUntrackedFiles: false,
17191732
},
17201733
{
17211734
AtlantisYAML: `
@@ -1724,9 +1737,10 @@ autodiscover:
17241737
mode: enabled
17251738
projects:
17261739
- dir: dir1`,
1727-
ExpectedCtxs: 0,
1728-
ExpectedClones: Once(),
1729-
ModifiedFiles: []string{"dir2/main.tf"},
1740+
ExpectedCtxs: 0,
1741+
ExpectedClones: Once(),
1742+
ModifiedFiles: []string{"dir2/main.tf"},
1743+
IncludeGitUntrackedFiles: false,
17301744
},
17311745
}
17321746

@@ -1770,7 +1784,7 @@ projects:
17701784
userConfig.AutoplanFileList,
17711785
userConfig.RestrictFileList,
17721786
userConfig.SilenceNoProjects,
1773-
userConfig.IncludeGitUntrackedFiles,
1787+
c.IncludeGitUntrackedFiles,
17741788
userConfig.AutoDiscoverMode,
17751789
scope,
17761790
terraformClient,

0 commit comments

Comments
 (0)