Repository: diggerhq/digger
Pull request #336 (2023-06-08 10:13:11)

only match generated projects with directory


Patch:
diff --git a/cmd/digger/main.go b/cmd/digger/main.go
index cb635723..77e86e44 100644
--- a/cmd/digger/main.go
+++ b/cmd/digger/main.go
@@ -101,8 +101,11 @@ func gitHubCI(lock core_locking.Lock) {
 		CiService: githubPrService,
 		PrNumber:  prNumber,
 	}
-
-	allAppliesSuccessful, atLeastOneApply, err := digger.RunCommandsPerProject(commandsToRunPerProject, parsedGhContext.Repository, githubActor, eventName, prNumber, githubPrService, lock, reporter, planStorage, "")
+	currentDir, err := os.Getwd()
+	if err != nil {
+		reportErrorAndExit(githubActor, fmt.Sprintf("Failed to get current dir. %s", err), 4)
+	}
+	allAppliesSuccessful, atLeastOneApply, err := digger.RunCommandsPerProject(commandsToRunPerProject, parsedGhContext.Repository, githubActor, eventName, prNumber, githubPrService, lock, reporter, planStorage, currentDir)
 	if err != nil {
 		reportErrorAndExit(githubActor, fmt.Sprintf("Failed to run commands. %s", err), 8)
 	}
diff --git a/pkg/configuration/digger_config.go b/pkg/configuration/digger_config.go
index 3177cd25..f6a4d103 100644
--- a/pkg/configuration/digger_config.go
+++ b/pkg/configuration/digger_config.go
@@ -5,11 +5,11 @@ import (
 	"digger/pkg/utils"
 	"errors"
 	"fmt"
-	"github.com/bmatcuk/doublestar/v4"
 	"gopkg.in/yaml.v3"
 	"os"
 	"path"
 	"path/filepath"
+	"regexp"
 )
 
 type WorkflowConfiguration struct {
@@ -85,22 +85,45 @@ type DirWalker interface {
 type FileSystemDirWalker struct {
 }
 
-func (walker *FileSystemDirWalker) GetDirs(workingDir string) ([]string, error) {
+func GetFilesWithExtension(workingDir string, ext string) ([]string, error) {
 	var files []string
+	listOfFiles, err := os.ReadDir(workingDir)
+	if err != nil {
+		return nil, errors.New(fmt.Sprintf("error reading directory %s: %v", workingDir, err))
+	}
+	for _, f := range listOfFiles {
+		if !f.IsDir() {
+			r, err := regexp.MatchString(ext, f.Name())
+			if err == nil && r {
+				files = append(files, f.Name())
+			}
+		}
+	}
+
+	return files, nil
+}
+
+func (walker *FileSystemDirWalker) GetDirs(workingDir string) ([]string, error) {
+	var dirs []string
 	err := filepath.Walk(workingDir,
 		func(path string, info os.FileInfo, err error) error {
+
 			if err != nil {
 				return err
 			}
 			if info.IsDir() {
-				files = append(files, path)
+				terraformFiles, _ := GetFilesWithExtension(path, ".tf")
+				if len(terraformFiles) > 0 {
+					dirs = append(dirs, path)
+					return filepath.SkipDir
+				}
 			}
 			return nil
 		})
 	if err != nil {
 		return nil, err
 	}
-	return files, nil
+	return dirs, nil
 }
 
 var ErrDiggerConfigConflict = errors.New("more than one digger config file detected, please keep either 'digger.yml' or 'digger.yaml'")
@@ -252,6 +275,11 @@ func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml, workingDir string,
 	// if workflow block is not specified in yaml we create a default one, and add it to every project
 	if diggerYaml.Workflows != nil {
 		diggerConfig.Workflows = diggerYaml.Workflows
+		// provide default workflow if not specified
+		if _, ok := diggerConfig.Workflows[defaultWorkflowName]; !ok {
+			workflow := *defaultWorkflow()
+			diggerConfig.Workflows[defaultWorkflowName] = workflow
+		}
 	} else {
 		workflow := *defaultWorkflow()
 		diggerConfig.Workflows = make(map[string]Workflow)
@@ -290,18 +318,8 @@ func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml, workingDir string,
 		for _, dir := range dirs {
 			includePattern := diggerYaml.GenerateProjectsConfig.Include
 			excludePattern := diggerYaml.GenerateProjectsConfig.Exclude
-			includeMatch, err := doublestar.PathMatch(includePattern, dir)
-			if err != nil {
-				return nil, err
-			}
-
-			excludeMatch, err := doublestar.PathMatch(excludePattern, dir)
-			if err != nil {
-				return nil, err
-			}
-			if includeMatch && !excludeMatch {
-				// generate a new project using default workflow
-				project := Project{Name: filepath.Base(dir), Dir: filepath.Join(workingDir, dir), Workflow: defaultWorkflowName}
+			if utils.MatchIncludeExcludePatternsToFile(dir, []string{includePattern}, []string{excludePattern}) {
+				project := Project{Name: filepath.Base(dir), Dir: dir, Workflow: defaultWorkflowName, Workspace: "default"}
 				diggerConfig.Projects = append(diggerConfig.Projects, project)
 			}
 		}
diff --git a/pkg/core/execution/execution.go b/pkg/core/execution/execution.go
index e554c7b5..d32087a6 100644
--- a/pkg/core/execution/execution.go
+++ b/pkg/core/execution/execution.go
@@ -32,7 +32,7 @@ type DiggerExecutor struct {
 }
 
 func (d DiggerExecutor) planFileName() string {
-	return d.ProjectNamespace + "#" + d.ProjectName + ".tfplan"
+	return strings.ReplaceAll(d.ProjectNamespace, "/", ":") + "#" + d.ProjectName + ".tfplan"
 }
 
 func (d DiggerExecutor) localPlanFilePath() string {


Test patch:
diff --git a/pkg/configuration/digger_config_test.go b/pkg/configuration/digger_config_test.go
index 9dd42af6..d7d9f641 100644
--- a/pkg/configuration/digger_config_test.go
+++ b/pkg/configuration/digger_config_test.go
@@ -238,8 +238,8 @@ generate_projects:
 	assert.NotNil(t, dg, "expected digger config to be not nil")
 	assert.Equal(t, "test1", dg.Projects[0].Name)
 	assert.Equal(t, "test2", dg.Projects[1].Name)
-	assert.Equal(t, tempDir+"/dev/test1", dg.Projects[0].Dir)
-	assert.Equal(t, tempDir+"/dev/test2", dg.Projects[1].Dir)
+	assert.Equal(t, "dev/test1", dg.Projects[0].Dir)
+	assert.Equal(t, "dev/test2", dg.Projects[1].Dir)
 	assert.Equal(t, 2, len(dg.Projects))
 }
 
@@ -268,8 +268,8 @@ generate_projects:
 	assert.NotNil(t, dg, "expected digger config to be not nil")
 	assert.Equal(t, "test1", dg.Projects[0].Name)
 	assert.Equal(t, "test2", dg.Projects[1].Name)
-	assert.Equal(t, tempDir+"/dev/test1", dg.Projects[0].Dir)
-	assert.Equal(t, tempDir+"/dev/test2", dg.Projects[1].Dir)
+	assert.Equal(t, "dev/test1", dg.Projects[0].Dir)
+	assert.Equal(t, "dev/test2", dg.Projects[1].Dir)
 	assert.Equal(t, 2, len(dg.Projects))
 }