Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
marcportabellaclotet-mt committed Oct 13, 2023
1 parent ecfabf8 commit a895c5d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 58 deletions.
53 changes: 33 additions & 20 deletions pkg/atlantis/atlantis.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,62 @@ type ProjectFolder struct {

// GenerateAtlantisYAML generates the atlantis.yaml file
func GenerateAtlantisYAML() error {

// Check if the PR filter is enabled
enablePRFilter := config.GlobalConfig.Parameters["pr-filter"] == "true"

// Get the changed files from the PR if prFilter is enabled
prChangedFiles := []string{}
var err error
if config.GlobalConfig.Parameters["pr-filter"] == "true" {
var prChangedFiles []string
if enablePRFilter {
var err error
prChangedFiles, err = github.GetChangedFiles()
if err != nil {
return err
}
}

// Scan folders to detect projects
projectFoldersList, err := scanProjectFolders(
config.GlobalConfig.Parameters["terraform-base-dir"],
config.GlobalConfig.Parameters["workflow"],
config.GlobalConfig.Parameters["pattern-detector"],
prChangedFiles,
config.GlobalConfig.Parameters["pr-filter"] == "true")
)
if err != nil {
return err
}

// Apply PR filter if enabled
if enablePRFilter {
projectFoldersList, err = applyPRFilter(projectFoldersList, prChangedFiles)
}

// Detect project workspaces
projectFoldersListWithWorkspaces, err := detectProjectWorkspaces(
projectFoldersList,
config.GlobalConfig.Parameters["workflow"],
config.GlobalConfig.Parameters["pattern-detector"],
prChangedFiles,
config.GlobalConfig.Parameters["pr-filter"] == "true")
prChangedFiles, enablePRFilter)
if err != nil {
return err
}

// Generate atlantis projects
atlantisProjects, err := generateAtlantisProjects(
config.GlobalConfig.Parameters["workflow"],
projectFoldersListWithWorkspaces)
if err != nil {
return err
}

// Filter atlantis projects with included and excluded regex rules
filteredAtlantisProjects, err := filterAtlantisProjects(
filteredAtlantisProjects, err := applyProjectFilter(
config.GlobalConfig.Parameters["excluded-projects"],
config.GlobalConfig.Parameters["included-projects"],
atlantisProjects)
if err != nil {
return err
}

// Generate atlantis config to later render the atlantis.yaml file
atlantisConfig, err := generateAtlantisConfig(
config.GlobalConfig.Parameters["automerge"],
Expand All @@ -97,6 +109,7 @@ func GenerateAtlantisYAML() error {
if err != nil {
return err
}

// Generate atlantis.yaml file
err = generateOutputYAML(&atlantisConfig,
config.GlobalConfig.Parameters["output-file"],
Expand All @@ -107,14 +120,13 @@ func GenerateAtlantisYAML() error {
return nil
}

func scanProjectFolders(basePath, workflow, patternDetector string, changedFiles []string, enablePRFilter bool) (projectFolders []ProjectFolder, err error) {
func scanProjectFolders(basePath, workflow, patternDetector string) (projectFolders []ProjectFolder, err error) {
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil {
return err
}

relPath, _ := filepath.Rel(basePath, filepath.Dir(path))
if shouldIncludeProject(info, path, workflow, patternDetector, relPath, changedFiles, enablePRFilter) {
if workflowFilter(info, path, workflow, patternDetector) {
relPath, _ := filepath.Rel(basePath, filepath.Dir(path))
projectFolders = append(projectFolders, ProjectFolder{Path: relPath})
}
return nil
Expand All @@ -123,16 +135,17 @@ func scanProjectFolders(basePath, workflow, patternDetector string, changedFiles
return projectFolders, err
}

func shouldIncludeProject(info os.FileInfo, path, workflow, patternDetector string, relPath string, changedFiles []string, enablePRFilter bool) bool {
workflowFilterResult := workflowFilter(info, path, workflow, patternDetector)
func applyPRFilter(projectFolders []ProjectFolder, changedFiles []string) (filteredProjectFolders []ProjectFolder, err error) {
// Iterate over atlantis projects and filter them
for _, project := range projectFolders {
prFilterResult := prFilter(project.Path, changedFiles)

// If PR filter is enabled, check if the project should be included based on changed files.
if enablePRFilter {
prFilterResult := prFilter(relPath, changedFiles)
return workflowFilterResult && prFilterResult
if prFilterResult {
filteredProjectFolders = append(filteredProjectFolders, project)
}
}

return workflowFilterResult
return filteredProjectFolders, nil
}

func detectProjectWorkspaces(foldersList []ProjectFolder, workflow string, patternDetector string, changedFiles []string, enablePRfilter bool) (updatedFoldersList []ProjectFolder, err error) {
Expand Down Expand Up @@ -163,7 +176,7 @@ func generateAtlantisProjects(workflow string, projectFolderList []ProjectFolder
return projects, nil
}

func filterAtlantisProjects(excludedProjects, includedProjects string, atlantisProjects []Project) (filteredAtlantisProjects []Project, err error) {
func applyProjectFilter(excludedProjects, includedProjects string, atlantisProjects []Project) (filteredAtlantisProjects []Project, err error) {

// Iterate over atlantis projects and filter them
for _, project := range atlantisProjects {
Expand Down
74 changes: 36 additions & 38 deletions pkg/atlantis/atlantis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"gopkg.in/yaml.v3"
)

func TestFilterAtlantisProjects(t *testing.T) {
func TestApplyProjectFilter(t *testing.T) {
atlantisProjects := []Project{
{Name: "project1"},
{Name: "project2"},
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestFilterAtlantisProjects(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
filteredProjects, err := filterAtlantisProjects(tc.excludedProjects, tc.includedProjects, atlantisProjects)
filteredProjects, err := applyProjectFilter(tc.excludedProjects, tc.includedProjects, atlantisProjects)

if tc.expectedError && err == nil {
t.Error("Expected an error, but got nil")
Expand Down Expand Up @@ -309,39 +309,51 @@ func TestPrFilter(t *testing.T) {
assert.False(t, result, "Expected false, but got true")
}

func TestApplyPRFilter(t *testing.T) {
// Define some sample data
projectFolders := []ProjectFolder{
{Path: "project1"},
{Path: "project2"},
{Path: "project3"},
}

changedFiles := []string{"project1/main.tf", "project2/main.tf"}

// Test case 1: Filtered projects
filteredProjectFolders, err := applyPRFilter(projectFolders, changedFiles)
if err != nil {
t.Errorf("Expected no error, but got an error: %v", err)
}

expectedFilteredProjects := []ProjectFolder{
{Path: "project1"},
{Path: "project2"},
}

if len(filteredProjectFolders) != len(expectedFilteredProjects) {
t.Errorf("Expected %d filtered projects, but got %d", len(expectedFilteredProjects), len(filteredProjectFolders))
}

for i, project := range filteredProjectFolders {
if project.Path != expectedFilteredProjects[i].Path {
t.Errorf("Expected project path %s, but got %s", expectedFilteredProjects[i].Path, project.Path)
}
}

}

func TestScanProjectFolders(t *testing.T) {
tests := []struct {
basePath string
workflow string
patternDetector string
changedFiles []string
enablePRFilter bool
expectedProjectFolder []ProjectFolder
expectedError bool
}{
{
basePath: "mockproject",
workflow: "multi-workspace",
patternDetector: "workspace_vars",
changedFiles: []string{
"multiworkspace/workspace_vars/test1.tfvars",
"multiworkspace2/workspace_vars/test1.tfvars",
"multiworkspace2/workspace_vars/test2.tfvars",
},
enablePRFilter: true,
expectedProjectFolder: []ProjectFolder{
{Path: "multiworkspace"},
{Path: "multiworkspace2"},
},
},
{
basePath: "mockproject",
workflow: "multi-workspace",
patternDetector: "workspace_vars",
changedFiles: []string{
"multiworkspace/workspace_vars/test1.tfvars",
},
enablePRFilter: false,
expectedProjectFolder: []ProjectFolder{
{Path: "multiworkspace"},
{Path: "multiworkspace2"},
Expand All @@ -351,18 +363,6 @@ func TestScanProjectFolders(t *testing.T) {
basePath: "mockproject",
workflow: "single-workspace",
patternDetector: "main.tf",
changedFiles: []string{"singleworkspace/main.tf", "file2"},
enablePRFilter: true,
expectedProjectFolder: []ProjectFolder{
{Path: "singleworkspace"},
},
},
{
basePath: "mockproject",
workflow: "single-workspace",
patternDetector: "main.tf",
changedFiles: []string{"singleworkspace/main.tf", "file2"},
enablePRFilter: false,
expectedProjectFolder: []ProjectFolder{
{Path: "singleworkspace"},
{Path: "singleworkspace2"},
Expand All @@ -372,8 +372,6 @@ func TestScanProjectFolders(t *testing.T) {
basePath: "invalidpath", // Invalid path to check file walk error
workflow: "single-workspace",
patternDetector: "main.tf",
changedFiles: []string{"singleworkspace/main.tf", "file2"},
enablePRFilter: true,
expectedProjectFolder: []ProjectFolder{
{Path: "singleworkspace"},
},
Expand All @@ -383,7 +381,7 @@ func TestScanProjectFolders(t *testing.T) {

for _, test := range tests {
t.Run(test.workflow, func(t *testing.T) {
projectFolders, err := scanProjectFolders(test.basePath, test.workflow, test.patternDetector, test.changedFiles, test.enablePRFilter)
projectFolders, err := scanProjectFolders(test.basePath, test.workflow, test.patternDetector)
if test.expectedError {
assert.Error(t, err)
} else {
Expand Down

0 comments on commit a895c5d

Please sign in to comment.