Skip to content

Commit

Permalink
Make project names unique
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilKalloli committed Oct 20, 2024
1 parent 8350fec commit 6f141d1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
27 changes: 27 additions & 0 deletions docs/ce/reference/digger.yml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,30 @@ You can run plan / apply in a specified project by using the -p option in Github
```bash
digger apply -p my-second-app
```
## Project Name Uniqueness

When defining projects in your `digger.yml` file, it's crucial to ensure that each project has a unique name. Digger relies on project names to organize various resources, including state files. Therefore, maintaining unique project names is an important prerequisite for proper functionality.

Digger automatically checks for duplicate project names when loading the configuration. If any duplicate project names are found, an error will be raised, and the configuration will not be loaded.

For example, the following configuration would result in an error:
```yml
projects:
- name: my-project
dir: ./app1
- name: my-project # This will cause an error due to duplicate name
dir: ./app2
```


To fix this, ensure each project has a unique name:

```yml
projects:
- name: my-project-app1
dir: ./app1
- name: my-project-app2
dir: ./app2
```
This uniqueness check is implemented in the Digger configuration loading process.

9 changes: 1 addition & 8 deletions libs/digger_config/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,7 @@ func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml) (*DiggerConfig, gra
projects := copyProjects(diggerYaml.Projects)
diggerConfig.Projects = projects

// update project's workflow if needed
for _, project := range diggerConfig.Projects {
if project.Workflow == "" {
project.Workflow = defaultWorkflowName
}
}

// check for project name duplicates
// Check for project name duplicates
projectNames := make(map[string]bool)
for _, project := range diggerConfig.Projects {
if projectNames[project.Name] {
Expand Down
13 changes: 13 additions & 0 deletions libs/digger_config/digger_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,3 +1231,16 @@ func TestGetModifiedProjectsReturnsCorrectSourceMapping(t *testing.T) {
assert.Equal(t, expectedImpactingLocations["prod"].ImpactingLocations, projectSourceMapping["prod"].ImpactingLocations)

}

func TestDiggerConfigDuplicateProjectNames(t *testing.T) {
diggerCfg := `
projects:
- name: prod
dir: path/to/module/test1
- name: prod
dir: path/to/module/test2
`
_, _, _, err := LoadDiggerConfigFromString(diggerCfg, "./")
assert.Error(t, err, "expected error for duplicate project names")
assert.Contains(t, err.Error(), "project name 'prod' is duplicated", "error message should mention the duplicate project name")
}
31 changes: 31 additions & 0 deletions libs/digger_config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package digger_config

import (
"errors"
"fmt"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -274,3 +275,33 @@ func (s *StepYaml) extract(stepMap map[string]interface{}, action string) {
}
}
}

func ValidateDiggerConfigYaml(configYaml *DiggerConfigYaml, fileName string) error {
if (configYaml.Projects == nil || len(configYaml.Projects) == 0) && configYaml.GenerateProjectsConfig == nil {
return fmt.Errorf("no projects config found in '%s'", fileName)
}
if configYaml.DependencyConfiguration != nil {
if configYaml.DependencyConfiguration.Mode != DependencyConfigurationHard && configYaml.DependencyConfiguration.Mode != DependencyConfigurationSoft {
return fmt.Errorf("dependency config mode can only be '%s' or '%s'", DependencyConfigurationHard, DependencyConfigurationSoft)
}
}

if configYaml.GenerateProjectsConfig != nil {
if configYaml.GenerateProjectsConfig.Include != "" &&
configYaml.GenerateProjectsConfig.Exclude != "" &&
len(configYaml.GenerateProjectsConfig.Blocks) != 0 {
return fmt.Errorf("if include/exclude patterns are used for project generation, blocks of include/exclude can't be used")
}
}

// Check for unique project names
projectNames := make(map[string]bool)
for _, project := range configYaml.Projects {
if projectNames[project.Name] {
return fmt.Errorf("duplicate project name '%s' found in '%s'", project.Name, fileName)
}
projectNames[project.Name] = true
}

return nil
}

0 comments on commit 6f141d1

Please sign in to comment.