Skip to content

Commit

Permalink
Clean-up checkIfStepActive (#4814)
Browse files Browse the repository at this point in the history
* Clean-up checkIfStepActive

* Mark --useV1 deprecated

* Clean up tests

* Update test

* Add warning message

* Update warning msg
  • Loading branch information
vstarostin authored Feb 8, 2024
1 parent c2343b1 commit 4e3fa38
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1,094 deletions.
41 changes: 14 additions & 27 deletions cmd/checkIfStepActive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"io"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/bmatcuk/doublestar"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type checkStepActiveCommandOptions struct {
Expand Down Expand Up @@ -63,6 +63,9 @@ func checkIfStepActive(utils piperutils.FileUtils) error {
if checkStepActiveOptions.stageName == "" {
return errors.New("stage name must not be empty")
}
if checkStepActiveOptions.v1Active {
log.Entry().Warning("Please do not use --useV1 flag since it is deprecated and will be removed in future releases")
}
var pConfig config.Config

// load project config and defaults
Expand All @@ -78,31 +81,15 @@ func checkIfStepActive(utils piperutils.FileUtils) error {
}
defer stageConfigFile.Close()

var runSteps map[string]map[string]bool
var runStages map[string]bool

// load and evaluate step conditions
if checkStepActiveOptions.v1Active {
runConfig := config.RunConfig{StageConfigFile: stageConfigFile}
runConfigV1 := &config.RunConfigV1{RunConfig: runConfig}
err = runConfigV1.InitRunConfigV1(projectConfig, utils, GeneralConfig.EnvRootPath)
if err != nil {
return err
}
runSteps = runConfigV1.RunSteps
runStages = runConfigV1.RunStages
} else {
log.Entry().Warning("This step is using deprecated format of stage conditions which will be removed in Jan 2024. " +
"To avoid pipeline breakage, please call checkIfStepActive command with --useV1 flag.",
)
runConfig := &config.RunConfig{StageConfigFile: stageConfigFile}
err = runConfig.InitRunConfig(projectConfig, nil, nil, nil, nil, doublestar.Glob, checkStepActiveOptions.openFile)
if err != nil {
return err
}
runSteps = runConfig.RunSteps
runStages = runConfig.RunStages
runConfig := config.RunConfig{StageConfigFile: stageConfigFile}
runConfigV1 := &config.RunConfigV1{RunConfig: runConfig}
err = runConfigV1.InitRunConfigV1(projectConfig, utils, GeneralConfig.EnvRootPath)
if err != nil {
return err
}
runSteps := runConfigV1.RunSteps
runStages := runConfigV1.RunStages

log.Entry().Debugf("RunSteps: %v", runSteps)
log.Entry().Debugf("RunStages: %v", runStages)
Expand Down Expand Up @@ -149,7 +136,7 @@ func addCheckStepActiveFlags(cmd *cobra.Command) {
"Default config of piper pipeline stages")
cmd.Flags().StringVar(&checkStepActiveOptions.stepName, "step", "", "Name of the step being checked")
cmd.Flags().StringVar(&checkStepActiveOptions.stageName, "stage", "", "Name of the stage in which contains the step being checked")
cmd.Flags().BoolVar(&checkStepActiveOptions.v1Active, "useV1", false, "Use new CRD-style stage configuration")
cmd.Flags().BoolVar(&checkStepActiveOptions.v1Active, "useV1", false, "Use new CRD-style stage configuration (deprecated)")
cmd.Flags().StringVar(&checkStepActiveOptions.stageOutputFile, "stageOutputFile", "", "Defines a file path. If set, the stage output will be written to the defined file")
cmd.Flags().StringVar(&checkStepActiveOptions.stepOutputFile, "stepOutputFile", "", "Defines a file path. If set, the step output will be written to the defined file")
_ = cmd.MarkFlagRequired("step")
Expand Down
13 changes: 8 additions & 5 deletions cmd/checkIfStepActive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ stages:
steps:`
case "stage-config.yml":
fileContent = `
stages:
testStage:
stepConditions:
testStep:
config: testConfig`
spec:
stages:
- name: testStage
displayName: testStage
steps:
- name: testStep
conditions:
- configKey: testConfig`
case ".pipeline/config.yml":
fileContent = `
steps:
Expand Down
8 changes: 3 additions & 5 deletions cmd/getDefaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"io"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type defaultsCommandOptions struct {
Expand Down Expand Up @@ -81,9 +82,6 @@ func getDefaults() ([]map[string]string, error) {
var yamlContent string

if !defaultsOptions.useV1 {
log.Entry().Warning("This step is using deprecated format of stage conditions which will be removed in Jan 2024. " +
"To avoid pipeline breakage, please call getDefaults command with --useV1 flag.",
)
var c config.Config
c.ReadConfig(fc)

Expand Down
231 changes: 2 additions & 229 deletions pkg/config/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package config
import (
"encoding/json"
"fmt"
"io"
"path"
"strings"

"github.com/pkg/errors"

"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/SAP/jenkins-library/pkg/piperutils"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -254,232 +253,6 @@ func checkConfigKeyV1(config map[string]interface{}, configKey []string) (bool,
return checkConfigKeyV1(castedValue, configKey[1:])
}

// EvaluateConditions validates stage conditions and updates runSteps in runConfig
func (r *RunConfig) evaluateConditions(config *Config, filters map[string]StepFilters, parameters map[string][]StepParameters,
secrets map[string][]StepSecrets, stepAliases map[string][]Alias, glob func(pattern string) (matches []string, err error)) error {
for stageName, stepConditions := range r.StageConfig.Stages {
runStep := map[string]bool{}
for stepName, stepCondition := range stepConditions.Conditions {
stepActive := false
stepConfig, err := r.getStepConfig(config, stageName, stepName, filters, parameters, secrets, stepAliases)
if err != nil {
return err
}

if active, ok := stepConfig.Config[stepName].(bool); ok {
// respect explicit activation/de-activation if available
stepActive = active
} else {
for conditionName, condition := range stepCondition {
var err error
switch conditionName {
case configCondition:
if stepActive, err = checkConfig(condition, stepConfig, stepName); err != nil {
return errors.Wrapf(err, "error: check config condition failed")
}
case configKeysCondition:
if stepActive, err = checkConfigKeys(condition, stepConfig, stepName); err != nil {
return errors.Wrapf(err, "error: check configKeys condition failed")
}
case filePatternFromConfigCondition:
if stepActive, err = checkForFilesWithPatternFromConfig(condition, stepConfig, stepName, glob); err != nil {
return errors.Wrapf(err, "error: check filePatternFromConfig condition failed")
}
case filePatternCondition:
if stepActive, err = checkForFilesWithPattern(condition, stepConfig, stepName, glob); err != nil {
return errors.Wrapf(err, "error: check filePattern condition failed")
}
case npmScriptsCondition:
if stepActive, err = checkForNpmScriptsInPackages(condition, stepConfig, stepName, glob, r.OpenFile); err != nil {
return errors.Wrapf(err, "error: check npmScripts condition failed")
}
default:
return errors.Errorf("unknown condition %s", conditionName)
}
if stepActive {
break
}
}
}
runStep[stepName] = stepActive
r.RunSteps[stageName] = runStep
}
}
return nil
}

func checkConfig(condition interface{}, config StepConfig, stepName string) (bool, error) {
switch condition := condition.(type) {
case string:
if configValue := stepConfigLookup(config.Config, stepName, condition); configValue != nil {
return true, nil
}
case map[string]interface{}:
for conditionConfigKey, conditionConfigValue := range condition {
configValue := stepConfigLookup(config.Config, stepName, conditionConfigKey)
if configValue == nil {
return false, nil
}
configValueStr, ok := configValue.(string)
if !ok {
return false, errors.Errorf("error: config value of %v to compare with is not a string", configValue)
}
condConfigValueArr, ok := conditionConfigValue.([]interface{})
if !ok {
return false, errors.Errorf("error: type assertion to []interface{} failed: %T", conditionConfigValue)
}
for _, item := range condConfigValueArr {
itemStr, ok := item.(string)
if !ok {
return false, errors.Errorf("error: type assertion to string failed: %T", conditionConfigValue)
}
if configValueStr == itemStr {
return true, nil
}
}
}
default:
return false, errors.Errorf("error: condidiion type invalid: %T, possible types: string, map[string]interface{}", condition)
}

return false, nil
}

func checkConfigKey(configKey string, config StepConfig, stepName string) (bool, error) {
if configValue := stepConfigLookup(config.Config, stepName, configKey); configValue != nil {
return true, nil
}
return false, nil
}

func checkConfigKeys(condition interface{}, config StepConfig, stepName string) (bool, error) {
arrCondition, ok := condition.([]interface{})
if !ok {
return false, errors.Errorf("error: type assertion to []interface{} failed: %T", condition)
}
for _, configKey := range arrCondition {
if configValue := stepConfigLookup(config.Config, stepName, configKey.(string)); configValue != nil {
return true, nil
}
}
return false, nil
}

func checkForFilesWithPatternFromConfig(condition interface{}, config StepConfig, stepName string,
glob func(pattern string) (matches []string, err error)) (bool, error) {
filePatternConfig, ok := condition.(string)
if !ok {
return false, errors.Errorf("error: type assertion to string failed: %T", condition)
}
filePatternFromConfig := stepConfigLookup(config.Config, stepName, filePatternConfig)
if filePatternFromConfig == nil {
return false, nil
}
filePattern, ok := filePatternFromConfig.(string)
if !ok {
return false, errors.Errorf("error: type assertion to string failed: %T", filePatternFromConfig)
}
matches, err := glob(filePattern)
if err != nil {
return false, errors.Wrap(err, "error: failed to check if file-exists")
}
if len(matches) > 0 {
return true, nil
}
return false, nil
}

func checkForFilesWithPattern(condition interface{}, config StepConfig, stepName string,
glob func(pattern string) (matches []string, err error)) (bool, error) {
switch condition := condition.(type) {
case string:
filePattern := condition
matches, err := glob(filePattern)
if err != nil {
return false, errors.Wrap(err, "error: failed to check if file-exists")
}
if len(matches) > 0 {
return true, nil
}
case []interface{}:
filePatterns := condition
for _, filePattern := range filePatterns {
filePatternStr, ok := filePattern.(string)
if !ok {
return false, errors.Errorf("error: type assertion to string failed: %T", filePatternStr)
}
matches, err := glob(filePatternStr)
if err != nil {
return false, errors.Wrap(err, "error: failed to check if file-exists")
}
if len(matches) > 0 {
return true, nil
}
}
default:
return false, errors.Errorf("error: condidiion type invalid: %T, possible types: string, []interface{}", condition)
}
return false, nil
}

func checkForNpmScriptsInPackages(condition interface{}, config StepConfig, stepName string,
glob func(pattern string) (matches []string, err error), openFile func(s string, t map[string]string) (io.ReadCloser, error)) (bool, error) {
packages, err := glob("**/package.json")
if err != nil {
return false, errors.Wrap(err, "error: failed to check if file-exists")
}
for _, pack := range packages {
packDirs := strings.Split(path.Dir(pack), "/")
isNodeModules := false
for _, dir := range packDirs {
if dir == "node_modules" {
isNodeModules = true
break
}
}
if isNodeModules {
continue
}

jsonFile, err := openFile(pack, nil)
if err != nil {
return false, errors.Errorf("error: failed to open file %s: %v", pack, err)
}
defer jsonFile.Close()
packageJSON := map[string]interface{}{}
if err := json.NewDecoder(jsonFile).Decode(&packageJSON); err != nil {
return false, errors.Errorf("error: failed to unmarshal json file %s: %v", pack, err)
}
npmScripts, ok := packageJSON["scripts"]
if !ok {
continue
}
scriptsMap, ok := npmScripts.(map[string]interface{})
if !ok {
return false, errors.Errorf("error: type assertion to map[string]interface{} failed: %T", npmScripts)
}
switch condition := condition.(type) {
case string:
if _, ok := scriptsMap[condition]; ok {
return true, nil
}
case []interface{}:
for _, conditionNpmScript := range condition {
conditionNpmScriptStr, ok := conditionNpmScript.(string)
if !ok {
return false, errors.Errorf("error: type assertion to string failed: %T", conditionNpmScript)
}
if _, ok := scriptsMap[conditionNpmScriptStr]; ok {
return true, nil
}
}
default:
return false, errors.Errorf("error: condidiion type invalid: %T, possible types: string, []interface{}", condition)
}
}
return false, nil
}

func checkForNpmScriptsInPackagesV1(npmScript string, config StepConfig, utils piperutils.FileUtils) (bool, error) {
packages, err := utils.Glob("**/package.json")
if err != nil {
Expand Down
Loading

0 comments on commit 4e3fa38

Please sign in to comment.