From 14390e7ddc36d4c9d6259ad7e3a4016429cdabb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 17 Jan 2025 10:45:11 +0100 Subject: [PATCH] Move step bundle and with group's step list validation --- bitrise/util_test.go | 16 ++++++------ models/models_methods.go | 53 +++++++++------------------------------- 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/bitrise/util_test.go b/bitrise/util_test.go index 37d77cc5..a7e9692f 100644 --- a/bitrise/util_test.go +++ b/bitrise/util_test.go @@ -322,7 +322,7 @@ step_bundles: test: steps: - bundle::build: {}`, - wantErr: "step bundle is not allowed in a step list", + wantErr: "step bundle (test) has config issue: step bundle is not allowed in a step bundle's step list", }, { name: "Invalid bitrise.yml: with group in a step bundle's steps list", @@ -333,7 +333,7 @@ step_bundles: test: steps: - with: {}`, - wantErr: "'with' group is not allowed in a step list", + wantErr: "step bundle (test) has config issue: 'with' group is not allowed in a step bundle's step list", }, { name: "Invalid bitrise.yml: step bundle in a 'with' group's steps list", @@ -351,7 +351,7 @@ workflows: - postgres steps: - bundle::test: {}`, - wantErr: "step bundle is not allowed in a step list", + wantErr: "step bundle is not allowed in a 'with' group's step list", }, { name: "Invalid bitrise.yml: with group in a 'with' group's steps list", @@ -369,7 +369,7 @@ workflows: - postgres steps: - with: {}`, - wantErr: "'with' group is not allowed in a step list", + wantErr: "'with' group is not allowed in a 'with' group's step list", }, } for _, tt := range tests { @@ -407,7 +407,7 @@ func TestConfigModelFromJSONFileContent_StepListValidation(t *testing.T) { } } }`, - wantErr: "step bundle is not allowed in a step list", + wantErr: "step bundle (test) has config issue: step bundle is not allowed in a step bundle's step list", }, { name: "Invalid bitrise.yml: with group in a step bundle's steps list", @@ -424,7 +424,7 @@ func TestConfigModelFromJSONFileContent_StepListValidation(t *testing.T) { } } }`, - wantErr: "'with' group is not allowed in a step list", + wantErr: "step bundle (test) has config issue: 'with' group is not allowed in a step bundle's step list", }, { name: "Invalid bitrise.yml: step bundle in a 'with' group's steps list", @@ -455,7 +455,7 @@ func TestConfigModelFromJSONFileContent_StepListValidation(t *testing.T) { } } }`, - wantErr: "step bundle is not allowed in a step list", + wantErr: "step bundle is not allowed in a 'with' group's step list", }, { name: "Invalid bitrise.yml: with group in a 'with' group's steps list", @@ -486,7 +486,7 @@ func TestConfigModelFromJSONFileContent_StepListValidation(t *testing.T) { } } }`, - wantErr: "'with' group is not allowed in a step list", + wantErr: "'with' group is not allowed in a 'with' group's step list", }, } for _, tt := range tests { diff --git a/models/models_methods.go b/models/models_methods.go index 925668c8..893660df 100644 --- a/models/models_methods.go +++ b/models/models_methods.go @@ -312,6 +312,12 @@ func (bundle *StepBundleModel) Validate() ([]string, error) { return warnings, err } + if stepID == StepListItemWithKey { + return warnings, errors.New("'with' group is not allowed in a step bundle's step list") + } else if strings.HasPrefix(stepID, StepListItemStepBundleKeyPrefix) { + return warnings, errors.New("step bundle is not allowed in a step bundle's step list") + } + warns, err := validateStep(stepID, step) warnings = append(warnings, warns...) if err != nil { @@ -357,6 +363,12 @@ func (with *WithModel) Validate(workflowID string, containers, services map[stri return warnings, err } + if stepID == StepListItemWithKey { + return warnings, errors.New("'with' group is not allowed in a 'with' group's step list") + } else if strings.HasPrefix(stepID, StepListItemStepBundleKeyPrefix) { + return warnings, errors.New("step bundle is not allowed in a 'with' group's step list") + } + warns, err := validateStep(stepID, step) warnings = append(warnings, warns...) if err != nil { @@ -1306,47 +1318,6 @@ func (stepListItem *StepListItemModel) UnmarshalYAML(unmarshal func(interface{}) return nil } -func (stepListStepItem *StepListStepItemModel) UnmarshalJSON(b []byte) error { - var raw map[string]stepmanModels.StepModel - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - - return stepListStepItem.fromRawStepListStepItem(raw) -} - -func (stepListStepItem *StepListStepItemModel) UnmarshalYAML(unmarshal func(interface{}) error) error { - var raw map[string]stepmanModels.StepModel - if err := unmarshal(&raw); err != nil { - return err - } - - return stepListStepItem.fromRawStepListStepItem(raw) -} - -func (stepListStepItem *StepListStepItemModel) fromRawStepListStepItem(raw map[string]stepmanModels.StepModel) error { - var key string - for k := range raw { - key = k - break - } - - // Only Steps are allowed - if key == StepListItemWithKey { - return errors.New("'with' group is not allowed in a step list") - } - if strings.HasPrefix(key, StepListItemStepBundleKeyPrefix) { - return errors.New("step bundle is not allowed in a step list") - } - - *stepListStepItem = map[string]stepmanModels.StepModel{} - for k, v := range raw { - (*stepListStepItem)[k] = v - } - - return nil -} - func (stepListStepItem *StepListStepItemModel) GetStepIDAndStep() (string, stepmanModels.StepModel, error) { if stepListStepItem == nil { return "", stepmanModels.StepModel{}, nil