1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

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 <jordi.van.liempt@sap.com>
Co-authored-by: Anil Keshav <anil.keshav@sap.com>
This commit is contained in:
Jordi van Liempt 2022-08-29 13:44:32 +02:00 committed by GitHub
parent b75d6cf9ed
commit 402c6085c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 15 deletions

View File

@ -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 {

View File

@ -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) {

View File

@ -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,