1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/documentation/generator/metadata.go

90 lines
3.1 KiB
Go
Raw Normal View History

package generator
import (
"fmt"
"reflect"
"github.com/SAP/jenkins-library/pkg/config"
)
// readStepMetadata reads and parses the provided step metadata file
func readStepMetadata(metadataFilePath string, docuHelperData DocuHelperData) config.StepData {
stepMetadata := config.StepData{}
metadataFile, err := docuHelperData.OpenFile(metadataFilePath)
checkError(err)
defer metadataFile.Close()
fmt.Printf("Reading metadata file: %v\n", metadataFilePath)
err = stepMetadata.ReadPipelineStepData(metadataFile)
checkError(err)
return stepMetadata
}
// adjustDefaultValues corrects the Default value according to the Type.
func adjustDefaultValues(stepMetadata *config.StepData) {
for key, parameter := range stepMetadata.Spec.Inputs.Parameters {
var typedDefault interface{} = nil
if parameter.Type == "bool" {
typedDefault = false
}
if parameter.Default != nil ||
parameter.Default == typedDefault {
continue
}
fmt.Printf("Changing default value to '%v' for parameter '%s', was '%v'.\n", typedDefault, parameter.Name, parameter.Default)
stepMetadata.Spec.Inputs.Parameters[key].Default = typedDefault
}
}
// adjustMandatoryFlags corrects the Mandatory flag on each parameter if a non-empty default value is provided
func adjustMandatoryFlags(stepMetadata *config.StepData) {
for key, parameter := range stepMetadata.Spec.Inputs.Parameters {
if parameter.Mandatory {
if parameter.Default == nil ||
parameter.Default == "" ||
feat(uiVeri5): golang implmementation for uiVeri5ExecuteTests (#2394) * added uiVeri5ExecuteTests step files * added confPath an regenerated step * added test for uiVeri5ExecuteTests * config modified * added groovy wrapper * ambiguous method fixed * uiveri5 wrapper * removed install command * fixed defaults * added testOptions as confPath arg * test set env * test npm install local * changed env settings * tests regenerated * go generate * fix code climate * overwrite groovy step * remove groovy wrapper go * unstash piper bin * test older node version * test piperExecuteBin * wip * wip * wip * wip * wip * wip * wip * refactored params * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * set testServerUrl as os env for uiveri5 * Update config.yml * fix naming of testServerUrl param * wip * refactored setEnv and fixed tests * wip * step param for NPM_CONFIG_PREFIX * fix runCommand * refactored step param, regenerate, docu, fix tests * fix groovy wrapper test * cleanup * add to CommonStepsTest field whitelist * fixed default pipeline environment vars * fix []string default * fix metadata.go bug * added test for docu metadata gen * fix metadata_test.go in doc gen * Update metadata_generated.go * Update metadata_generated.go * remove npm config prefix param; doc fix * remove tab * changed npm config prefix * removed groovy wrapper test * removed groovy step defaults * modify path variable * modified npm config prefix * fix error wrapper and tests * doc update * add testRepository support * wip * fix testRepository param * wip * add utils * init stash content * wip * wip * wip * add comment for deprecated parameters * fixed commonStepTest * fixed commonStepTest * added error category for testOptions failure * Update vars/uiVeri5ExecuteTests.groovy Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Update vars/uiVeri5ExecuteTests.groovy Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * fix condition Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2021-01-28 12:31:24 +02:00
parameter.Type == "[]string" && interfaceArrayLength(parameter.Default) == 0 {
continue
}
fmt.Printf("Changing mandatory flag to '%v' for parameter '%s', default value available '%v'.\n", false, parameter.Name, parameter.Default)
stepMetadata.Spec.Inputs.Parameters[key].Mandatory = false
}
}
}
// applyCustomDefaultValues applies custom default values from the passed config
func applyCustomDefaultValues(stepMetadata *config.StepData, stepConfiguration config.StepConfig) {
for key, parameter := range stepMetadata.Spec.Inputs.Parameters {
configValue := stepConfiguration.Config[parameter.Name]
if len(parameter.Conditions) != 0 {
fmt.Printf("Skipping custom default values for '%s' as the parameter depends on other parameter values.\n", parameter.Name)
continue
}
if configValue != nil && configValue != "" {
switch parameter.Type {
case "[]string":
if reflect.DeepEqual(parameter.Default, configValue) {
continue
}
fmt.Printf("Applying custom default value '%v' for parameter '%s', was '%v'.\n", configValue, parameter.Name, parameter.Default)
stepMetadata.Spec.Inputs.Parameters[key].Default = configValue
default:
if parameter.Default != configValue {
fmt.Printf("Applying custom default value '%v' for parameter '%s', was '%v'.\n", configValue, parameter.Name, parameter.Default)
stepMetadata.Spec.Inputs.Parameters[key].Default = configValue
}
}
}
}
}
feat(uiVeri5): golang implmementation for uiVeri5ExecuteTests (#2394) * added uiVeri5ExecuteTests step files * added confPath an regenerated step * added test for uiVeri5ExecuteTests * config modified * added groovy wrapper * ambiguous method fixed * uiveri5 wrapper * removed install command * fixed defaults * added testOptions as confPath arg * test set env * test npm install local * changed env settings * tests regenerated * go generate * fix code climate * overwrite groovy step * remove groovy wrapper go * unstash piper bin * test older node version * test piperExecuteBin * wip * wip * wip * wip * wip * wip * wip * refactored params * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * set testServerUrl as os env for uiveri5 * Update config.yml * fix naming of testServerUrl param * wip * refactored setEnv and fixed tests * wip * step param for NPM_CONFIG_PREFIX * fix runCommand * refactored step param, regenerate, docu, fix tests * fix groovy wrapper test * cleanup * add to CommonStepsTest field whitelist * fixed default pipeline environment vars * fix []string default * fix metadata.go bug * added test for docu metadata gen * fix metadata_test.go in doc gen * Update metadata_generated.go * Update metadata_generated.go * remove npm config prefix param; doc fix * remove tab * changed npm config prefix * removed groovy wrapper test * removed groovy step defaults * modify path variable * modified npm config prefix * fix error wrapper and tests * doc update * add testRepository support * wip * fix testRepository param * wip * add utils * init stash content * wip * wip * wip * add comment for deprecated parameters * fixed commonStepTest * fixed commonStepTest * added error category for testOptions failure * Update vars/uiVeri5ExecuteTests.groovy Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Update vars/uiVeri5ExecuteTests.groovy Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * fix condition Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2021-01-28 12:31:24 +02:00
// check length only if interface type is a slice
func interfaceArrayLength(i interface{}) int {
switch i.(type) {
case []string:
return len(i.([]string))
case []interface{}:
return len(i.([]interface{}))
default:
return -1
}
}