From 402c6085c95414b5190cb3ab8ef6fd770789377e Mon Sep 17 00:00:00 2001 From: Jordi van Liempt <35920075+jliempt@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:44:32 +0200 Subject: [PATCH] feat(checkIfStepActive): enhance with pipelineEnvironmentFilled condition (#3975) * add CommonPipelineEnvironmentVariableExists to StepCondition * add CPE variable existance check to checkIfStepActive * refactor getCPEEntry() * condition type -> string * rename CPE filled condition * rename CPE condition Co-authored-by: I557621 Co-authored-by: Anil Keshav --- pkg/config/evaluation.go | 47 +++++++++++++++++++++++++---------- pkg/config/evaluation_test.go | 17 +++++++++++-- pkg/config/run.go | 1 + 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/pkg/config/evaluation.go b/pkg/config/evaluation.go index 31695766f..b8e01cd64 100644 --- a/pkg/config/evaluation.go +++ b/pkg/config/evaluation.go @@ -164,25 +164,31 @@ func (s *StepCondition) evaluateV1(config StepConfig, utils piperutils.FileUtils var metadata StepData for param, value := range s.CommonPipelineEnvironment { - dataType := "interface" - _, ok := value.(string) - if ok { - dataType = "string" - } - metadata.Spec.Inputs.Parameters = []StepParameters{ - {Name: stepName, - Type: dataType, - ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: param}}, - }, - } - resourceParams := metadata.GetResourceParameters(envRootPath, "commonPipelineEnvironment") - if resourceParams[stepName] == value { + cpeEntry := getCPEEntry(param, value, &metadata, stepName, envRootPath) + if cpeEntry[stepName] == value { return true, nil } } return false, nil } + if len(s.PipelineEnvironmentFilled) > 0 { + + var metadata StepData + param := s.PipelineEnvironmentFilled + // check CPE for both a string and non-string value + cpeEntry := getCPEEntry(param, "", &metadata, stepName, envRootPath) + if len(cpeEntry) == 0 { + cpeEntry = getCPEEntry(param, nil, &metadata, stepName, envRootPath) + } + + if _, ok := cpeEntry[stepName]; ok { + return true, nil + } + + return false, nil + } + // needs to be checked last: // if none of the other conditions matches, step will be active unless set to inactive if s.Inactive == true { @@ -192,6 +198,21 @@ func (s *StepCondition) evaluateV1(config StepConfig, utils piperutils.FileUtils } } +func getCPEEntry(param string, value interface{}, metadata *StepData, stepName string, envRootPath string) map[string]interface{} { + dataType := "interface" + _, ok := value.(string) + if ok { + dataType = "string" + } + metadata.Spec.Inputs.Parameters = []StepParameters{ + {Name: stepName, + Type: dataType, + ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: param}}, + }, + } + return metadata.GetResourceParameters(envRootPath, "commonPipelineEnvironment") +} + func checkConfigKeyV1(config map[string]interface{}, configKey []string) (bool, error) { value, ok := config[configKey[0]] if len(configKey) == 1 { diff --git a/pkg/config/evaluation_test.go b/pkg/config/evaluation_test.go index 17d2af553..17c215531 100644 --- a/pkg/config/evaluation_test.go +++ b/pkg/config/evaluation_test.go @@ -384,6 +384,18 @@ func TestEvaluateV1(t *testing.T) { stepCondition: StepCondition{CommonPipelineEnvironment: map[string]interface{}{"myCpeTrueFile": "notMyTrueValue"}}, expected: false, }, + { + name: "CommonPipelineEnvironmentVariableExists - true", + config: StepConfig{Config: map[string]interface{}{}}, + stepCondition: StepCondition{PipelineEnvironmentFilled: "custom/myCpeTrueFile"}, + expected: true, + }, + { + name: "CommonPipelineEnvironmentVariableExists - false", + config: StepConfig{Config: map[string]interface{}{}}, + stepCondition: StepCondition{PipelineEnvironmentFilled: "custom/notMyCpeTrueFile"}, + expected: false, + }, { name: "No condition - true", config: StepConfig{Config: map[string]interface{}{}}, @@ -405,11 +417,12 @@ func TestEvaluateV1(t *testing.T) { dir := t.TempDir() cpeDir := filepath.Join(dir, "commonPipelineEnvironment") - err := os.MkdirAll(cpeDir, 0700) + err := os.MkdirAll(filepath.Join(cpeDir, "custom"), 0700) if err != nil { - t.Fatal("Failed to create sub directory") + t.Fatal("Failed to create sub directories") } ioutil.WriteFile(filepath.Join(cpeDir, "myCpeTrueFile"), []byte("myTrueValue"), 0700) + ioutil.WriteFile(filepath.Join(cpeDir, "custom", "myCpeTrueFile"), []byte("myTrueValue"), 0700) for _, test := range tt { t.Run(test.name, func(t *testing.T) { diff --git a/pkg/config/run.go b/pkg/config/run.go index 3bd8a340f..6eb197684 100644 --- a/pkg/config/run.go +++ b/pkg/config/run.go @@ -75,6 +75,7 @@ type StepCondition struct { Inactive bool `json:"inactive,omitempty"` NpmScript string `json:"npmScript,omitempty"` CommonPipelineEnvironment map[string]interface{} `json:"commonPipelineEnvironment,omitempty"` + PipelineEnvironmentFilled string `json:"pipelineEnvironmentFilled,omitempty"` } func (r *RunConfigV1) InitRunConfigV1(config *Config, filters map[string]StepFilters, parameters map[string][]StepParameters,