Skip to content

Commit

Permalink
fix(autoplan): modules for per-repo configs (runatlantis#3272)
Browse files Browse the repository at this point in the history
* Fix autoplan modules for per-repo configs

* Use utils package for slices.Contains
  • Loading branch information
jukie authored and ijames-gc committed Feb 13, 2024
1 parent 1705c8c commit d6bc1e7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/events/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func findModuleDependants(files fs.FS, autoplanModuleDependants string) (ModuleP
filter, _ := patternmatcher.New(strings.Split(autoplanModuleDependants, ","))
var projects []string
err := fs.WalkDir(files, ".", func(rel string, info fs.DirEntry, err error) error {
if match, _ := filter.Matches(rel); match {
if match, _ := filter.MatchesOrParentMatches(rel); match {
if projectDir := getProjectDirFromFs(files, rel); projectDir != "" {
projects = append(projects, projectDir)
}
Expand Down
15 changes: 8 additions & 7 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
}
ctx.Log.Info("successfully parsed remote %s file", repoCfgFile)
if len(repoCfg.Projects) > 0 {
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, repoCfg, "")
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, repoCfg, "", nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -352,8 +352,14 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
ctx.Log.Info("successfully parsed %s file", repoCfgFile)
}

moduleInfo, err := FindModuleProjects(repoDir, p.AutoDetectModuleFiles)
if err != nil {
ctx.Log.Warn("error(s) loading project module dependencies: %s", err)
}
ctx.Log.Debug("moduleInfo for %s (matching %q) = %v", repoDir, p.AutoDetectModuleFiles, moduleInfo)

if len(repoCfg.Projects) > 0 {
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, repoCfg, repoDir)
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, repoCfg, repoDir, moduleInfo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -387,11 +393,6 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex
ctx.Log.Info("found no %s file", repoCfgFile)
}
// build a module index for projects that are explicitly included
moduleInfo, err := FindModuleProjects(repoDir, p.AutoDetectModuleFiles)
if err != nil {
ctx.Log.Warn("error(s) loading project module dependencies: %s", err)
}
ctx.Log.Debug("moduleInfo for %s (matching %q) = %v", repoDir, p.AutoDetectModuleFiles, moduleInfo)
modifiedProjects := p.ProjectFinder.DetermineProjects(ctx.Log, modifiedFiles, ctx.Pull.BaseRepo.FullName, repoDir, p.AutoplanFileList, moduleInfo)
ctx.Log.Info("automatically determined that there were %d projects modified in this pull request: %s", len(modifiedProjects), modifiedProjects)
for _, mp := range modifiedProjects {
Expand Down
22 changes: 20 additions & 2 deletions server/events/project_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/utils"

"github.com/moby/patternmatcher"
"github.com/pkg/errors"
Expand All @@ -44,7 +45,7 @@ type ProjectFinder interface {
// DetermineProjectsViaConfig returns the list of projects that were modified
// based on modifiedFiles and the repo's config.
// absRepoDir is the path to the cloned repo on disk.
DetermineProjectsViaConfig(log logging.SimpleLogging, modifiedFiles []string, config valid.RepoCfg, absRepoDir string) ([]valid.Project, error)
DetermineProjectsViaConfig(log logging.SimpleLogging, modifiedFiles []string, config valid.RepoCfg, absRepoDir string, moduleInfo ModuleProjects) ([]valid.Project, error)

DetermineWorkspaceFromHCL(log logging.SimpleLogging, absRepoDir string) (string, error)
}
Expand Down Expand Up @@ -174,10 +175,27 @@ func (p *DefaultProjectFinder) DetermineProjects(log logging.SimpleLogging, modi
}

// See ProjectFinder.DetermineProjectsViaConfig.
func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log logging.SimpleLogging, modifiedFiles []string, config valid.RepoCfg, absRepoDir string) ([]valid.Project, error) {
func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log logging.SimpleLogging, modifiedFiles []string, config valid.RepoCfg, absRepoDir string, moduleInfo ModuleProjects) ([]valid.Project, error) {

// Check moduleInfo for downstream project dependencies
var dependentProjects []string
for _, file := range modifiedFiles {
if moduleInfo != nil {
downstreamProjects := moduleInfo.DependentProjects(path.Dir(file))
log.Debug("found downstream projects for %q: %v", file, downstreamProjects)
dependentProjects = append(dependentProjects, downstreamProjects...)
}
}

var projects []valid.Project
for _, project := range config.Projects {
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)

if utils.SlicesContains(dependentProjects, project.Dir) {
projects = append(projects, project)
continue
}

var whenModifiedRelToRepoRoot []string
for _, wm := range project.Autoplan.WhenModified {
wm = strings.TrimSpace(wm)
Expand Down
2 changes: 1 addition & 1 deletion server/events/project_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
pf := events.DefaultProjectFinder{}
projects, err := pf.DetermineProjectsViaConfig(logging.NewNoopLogger(t), c.modified, c.config, tmpDir)
projects, err := pf.DetermineProjectsViaConfig(logging.NewNoopLogger(t), c.modified, c.config, tmpDir, nil)
Ok(t, err)
Equals(t, len(c.expProjPaths), len(projects))
for i, proj := range projects {
Expand Down

0 comments on commit d6bc1e7

Please sign in to comment.