Repository: diggerhq/digger
Pull request #375 (2023-06-15 14:27:00)

add more DIgger Config validators


Patch:
diff --git a/pkg/configuration/converters.go b/pkg/configuration/converters.go
index e0828b7f..98d2a8bc 100644
--- a/pkg/configuration/converters.go
+++ b/pkg/configuration/converters.go
@@ -75,20 +75,26 @@ func copyWorkflowConfiguration(config *WorkflowConfigurationYaml) *WorkflowConfi
 	return &result
 }
 
+// converts dict of WorkflowYaml's to dict of Workflow's
 func copyWorkflows(workflows map[string]*WorkflowYaml) map[string]Workflow {
 	result := make(map[string]Workflow, len(workflows))
 	for i, w := range workflows {
-		envVars := copyTerraformEnvConfig(w.EnvVars)
-		plan := copyStage(w.Plan)
-		apply := copyStage(w.Apply)
-		configuration := copyWorkflowConfiguration(w.Configuration)
-		item := Workflow{
-			envVars,
-			plan,
-			apply,
-			configuration,
+		if w == nil {
+			item := *defaultWorkflow()
+			result[i] = item
+		} else {
+			envVars := copyTerraformEnvConfig(w.EnvVars)
+			plan := copyStage(w.Plan)
+			apply := copyStage(w.Apply)
+			configuration := copyWorkflowConfiguration(w.Configuration)
+			item := Workflow{
+				envVars,
+				plan,
+				apply,
+				configuration,
+			}
+			result[i] = item
 		}
-		result[i] = item
 	}
 	return result
 }
@@ -159,5 +165,17 @@ func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml, workingDir string,
 			}
 		}
 	}
+
+	// if one of the workflows is missing Plan or Apply we copy default values
+	for _, w := range diggerConfig.Workflows {
+		defaultWorkflow := *defaultWorkflow()
+		if w.Plan == nil {
+			w.Plan = defaultWorkflow.Plan
+		}
+		if w.Apply == nil {
+			w.Apply = defaultWorkflow.Apply
+		}
+	}
+
 	return &diggerConfig, nil
 }


Test patch:
diff --git a/pkg/configuration/digger_config_test.go b/pkg/configuration/digger_config_test.go
index 1a0b6610..8193b108 100644
--- a/pkg/configuration/digger_config_test.go
+++ b/pkg/configuration/digger_config_test.go
@@ -364,6 +364,96 @@ func TestMissingProjectsReturnsError(t *testing.T) {
 	assert.ErrorContains(t, err, "no projects configuration found")
 }
 
+func TestDiggerConfigCustomWorkflow(t *testing.T) {
+	tempDir, teardown := setUp()
+	defer teardown()
+
+	diggerCfg := `
+projects:
+- name: my-first-app
+  dir: app-one
+  workflow: my_custom_workflow
+workflows:
+  my_custom_workflow:
+    steps:
+      - run: echo "run"
+      - init: terraform init
+      - plan: terraform plan
+`
+	deleteFile := createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
+
+	dg, err := LoadDiggerConfig(tempDir, &FileSystemDirWalker{})
+	assert.NoError(t, err, "expected error to be nil")
+	assert.NotNil(t, dg, "expected digger config to be not nil")
+	assert.Equal(t, "my_custom_workflow", dg.Projects[0].Workflow)
+	_, ok := dg.Workflows["my_custom_workflow"]
+	assert.True(t, ok)
+}
+
+func TestDiggerConfigCustomWorkflowMissingParams(t *testing.T) {
+	tempDir, teardown := setUp()
+	defer teardown()
+
+	// missing workflow config
+	diggerCfg := `
+projects:
+- name: my-first-app
+  dir: app-one
+  workflow: my_custom_workflow
+`
+	deleteFile := createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
+
+	_, err := LoadDiggerConfig(tempDir, &FileSystemDirWalker{})
+	assert.Error(t, err, "failed to find workflow config 'my_custom_workflow' for project 'my-first-app'")
+
+	// steps block is missing for workflows
+	diggerCfg = `
+projects:
+- name: my-first-app
+  dir: app-one
+  workflow: my_custom_workflow
+workflows:
+  my_custom_workflow:
+`
+	deleteFile = createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
+
+	diggerConfig, err := LoadDiggerConfig(tempDir, &FileSystemDirWalker{})
+	assert.Equal(t, "my_custom_workflow", diggerConfig.Projects[0].Workflow)
+	workflow, ok := diggerConfig.Workflows["my_custom_workflow"]
+	assert.True(t, ok)
+	assert.NotNil(t, workflow)
+	assert.NotNil(t, workflow.Plan)
+	assert.NotNil(t, workflow.Apply)
+
+}
+
+func TestDiggerConfigMissingProjectsWorkflow(t *testing.T) {
+	tempDir, teardown := setUp()
+	defer teardown()
+
+	diggerCfg := `
+projects:
+- name: my-first-app
+  dir: app-one
+  workflow: my_custom_workflow
+workflows:
+  my_custom_workflow_no_one_use:
+    steps:
+      - run: echo "run"
+      - init: terraform init
+      - plan: terraform plan
+`
+	deleteFile := createFile(path.Join(tempDir, "digger.yaml"), diggerCfg)
+	defer deleteFile()
+
+	_, err := LoadDiggerConfig(tempDir, &FileSystemDirWalker{})
+	assert.Equal(t, "failed to find workflow config 'my_custom_workflow' for project 'my-first-app'", err.Error())
+
+}
+
 func createTempDir() string {
 	dir, err := os.MkdirTemp("", "tmp")
 	if err != nil {