Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added stage field in spec and solved issue of checking duplicates of multip… #1875

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions backend/services/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetRunNameFromJob(job models.DiggerJob) (*string, error) {
return &runName, nil
}

func getVariablesSpecFromEnvMap(envVars map[string]string) []spec.VariableSpec {
func getVariablesSpecFromEnvMap(envVars map[string]string, stage string) []spec.VariableSpec {
variablesSpec := make([]spec.VariableSpec, 0)
for k, v := range envVars {
if strings.HasPrefix(v, "$DIGGER_") {
Expand All @@ -70,20 +70,43 @@ func getVariablesSpecFromEnvMap(envVars map[string]string) []spec.VariableSpec {
Value: val,
IsSecret: false,
IsInterpolated: true,
Stage: stage,
})
} else {
variablesSpec = append(variablesSpec, spec.VariableSpec{
Name: k,
Value: v,
IsSecret: false,
IsInterpolated: false,
Stage: stage,
})

}
}
return variablesSpec
}

func findDuplicatesInStage(variablesSpec []spec.VariableSpec, stage string) (error) {
// Extract the names from VariableSpec
justNames := lo.Map(variablesSpec, func(item spec.VariableSpec, i int) string {
return item.Name
})

// Group names by their occurrence
nameCounts := lo.CountValues(justNames)

// Filter names that occur more than once
duplicates := lo.Keys(lo.PickBy(nameCounts, func(name string, count int) bool {
return count > 1
}))

if len(duplicates) > 0 {
return fmt.Errorf("duplicate variable names found in '%s' stage: %v", stage, strings.Join(duplicates, ", "))
}

return nil // No duplicates found
}

func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) {
var jobSpec scheduler.JobJson
err := json.Unmarshal([]byte(job.SerializedJobSpec), &jobSpec)
Expand All @@ -92,23 +115,25 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) {
return nil, fmt.Errorf("could not marshal json string: %v", err)
}

stateVariables := getVariablesSpecFromEnvMap(jobSpec.StateEnvVars, "state")
commandVariables := getVariablesSpecFromEnvMap(jobSpec.CommandEnvVars, "commands")
runVariables := getVariablesSpecFromEnvMap(jobSpec.RunEnvVars, "run")

if err := findDuplicatesInStage(stateVariables, "state"); err != nil {
return nil, err
}
if err := findDuplicatesInStage(commandVariables, "commands"); err != nil {
return nil, err
}
if err := findDuplicatesInStage(runVariables, "run"); err != nil {
return nil, err
}

variablesSpec := make([]spec.VariableSpec, 0)
stateVariables := getVariablesSpecFromEnvMap(jobSpec.StateEnvVars)
commandVariables := getVariablesSpecFromEnvMap(jobSpec.CommandEnvVars)
runVariables := getVariablesSpecFromEnvMap(jobSpec.RunEnvVars)
variablesSpec = append(variablesSpec, stateVariables...)
variablesSpec = append(variablesSpec, commandVariables...)
variablesSpec = append(variablesSpec, runVariables...)

// check for duplicates in list of variablesSpec
justNames := lo.Map(variablesSpec, func(item spec.VariableSpec, i int) string {
return item.Name
})
hasDuplicates := len(justNames) != len(lo.Uniq(justNames))
if hasDuplicates {
return nil, fmt.Errorf("could not load variables due to duplicates")
}

batch := job.Batch

spec := spec.Spec{
Expand Down
1 change: 1 addition & 0 deletions libs/spec/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type VariableSpec struct {
Value string `json:"value"`
IsSecret bool `json:"is_secret"`
IsInterpolated bool `json:"is_interpolated"`
Stage string `json:"stage"`
}

type SpecType string
Expand Down