diff --git a/bitrise/util.go b/bitrise/util.go index 3277dc7d..d60e5c2b 100644 --- a/bitrise/util.go +++ b/bitrise/util.go @@ -8,6 +8,8 @@ import ( "path/filepath" "strings" + "gopkg.in/yaml.v2" + "github.com/bitrise-io/bitrise/configs" "github.com/bitrise-io/bitrise/models" envmanModels "github.com/bitrise-io/envman/models" @@ -16,7 +18,13 @@ import ( "github.com/bitrise-io/go-utils/pathutil" "github.com/bitrise-io/go-utils/pointers" stepmanModels "github.com/bitrise-io/stepman/models" - "gopkg.in/yaml.v2" +) + +type ValidationType int + +const ( + ValidationTypeFull ValidationType = iota + ValidationTypeMinimal ) func InventoryModelFromYAMLBytes(inventoryBytes []byte) (inventory envmanModels.EnvsSerializeModel, err error) { @@ -198,13 +206,13 @@ func SetBuildFailedEnv(failed bool) error { return nil } -func normalizeValidateFillMissingDefaults(bitriseData *models.BitriseDataModel, fullValidation bool) ([]string, error) { +func normalizeValidateFillMissingDefaults(bitriseData *models.BitriseDataModel, validation ValidationType) ([]string, error) { if err := bitriseData.Normalize(); err != nil { return []string{}, err } var validationFunc func() ([]string, error) - if fullValidation { + if validation == ValidationTypeFull { validationFunc = bitriseData.Validate } else { validationFunc = bitriseData.MinimalValidation @@ -222,7 +230,7 @@ func normalizeValidateFillMissingDefaults(bitriseData *models.BitriseDataModel, return warnings, nil } -func ConfigModelFromFileContent(configBytes []byte, isJSON bool, fullValidation bool) (bitriseData models.BitriseDataModel, warnings []string, err error) { +func ConfigModelFromFileContent(configBytes []byte, isJSON bool, validation ValidationType) (bitriseData models.BitriseDataModel, warnings []string, err error) { if isJSON { bitriseData, err = jsonBytesToConfig(configBytes) } else { @@ -233,7 +241,7 @@ func ConfigModelFromFileContent(configBytes []byte, isJSON bool, fullValidation return } - warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, true) + warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, validation) return } @@ -243,17 +251,17 @@ func ConfigModelFromYAMLBytes(configBytes []byte) (bitriseData models.BitriseDat return } - warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, true) + warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, ValidationTypeFull) return } -func ConfigModelFromYAMLBytesWithValidation(configBytes []byte, fullValidation bool) (bitriseData models.BitriseDataModel, warnings []string, err error) { +func ConfigModelFromYAMLBytesWithValidation(configBytes []byte, validation ValidationType) (bitriseData models.BitriseDataModel, warnings []string, err error) { bitriseData, err = yamlBytesToConfig(configBytes) if err != nil { return } - warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, fullValidation) + warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, validation) return } @@ -263,7 +271,7 @@ func ConfigModelFromJSONBytes(configBytes []byte) (bitriseData models.BitriseDat return } - warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, true) + warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, ValidationTypeFull) return } @@ -285,7 +293,7 @@ func yamlBytesToConfig(bytes []byte) (models.BitriseDataModel, error) { return config, nil } -func ReadBitriseConfig(pth string, fullValidation bool) (models.BitriseDataModel, []string, error) { +func ReadBitriseConfig(pth string, validation ValidationType) (models.BitriseDataModel, []string, error) { if isExists, err := pathutil.IsPathExists(pth); err != nil { return models.BitriseDataModel{}, []string{}, err } else if !isExists { @@ -301,7 +309,7 @@ func ReadBitriseConfig(pth string, fullValidation bool) (models.BitriseDataModel return models.BitriseDataModel{}, []string{}, errors.New("empty config") } - return ConfigModelFromFileContent(bytes, strings.HasSuffix(pth, ".json"), fullValidation) + return ConfigModelFromFileContent(bytes, strings.HasSuffix(pth, ".json"), validation) } func ReadSpecStep(pth string) (stepmanModels.StepModel, error) { diff --git a/cli/run.go b/cli/run.go index c1cfc447..ecb561da 100644 --- a/cli/run.go +++ b/cli/run.go @@ -434,7 +434,7 @@ func processArgs(c *cli.Context) (*RunConfig, error) { return nil, fmt.Errorf("failed to create inventory: %s", err) } - bitriseConfig, warnings, err := CreateBitriseConfigFromCLIParams(runParams.BitriseConfigBase64Data, runParams.BitriseConfigPath, false) + bitriseConfig, warnings, err := CreateBitriseConfigFromCLIParams(runParams.BitriseConfigBase64Data, runParams.BitriseConfigPath, bitrise.ValidationTypeMinimal) for _, warning := range warnings { log.Warnf("warning: %s", warning) } diff --git a/cli/run_util.go b/cli/run_util.go index 94ced88a..7bdb39cb 100644 --- a/cli/run_util.go +++ b/cli/run_util.go @@ -883,13 +883,13 @@ func isDirEmpty(path string) (bool, error) { return len(entries) == 0, nil } -func GetBitriseConfigFromBase64Data(configBase64Str string, fullValidation bool) (models.BitriseDataModel, []string, error) { +func GetBitriseConfigFromBase64Data(configBase64Str string, validation bitrise.ValidationType) (models.BitriseDataModel, []string, error) { configBase64Bytes, err := base64.StdEncoding.DecodeString(configBase64Str) if err != nil { return models.BitriseDataModel{}, []string{}, fmt.Errorf("Failed to decode base 64 string, error: %s", err) } - config, warnings, err := bitrise.ConfigModelFromYAMLBytesWithValidation(configBase64Bytes, fullValidation) + config, warnings, err := bitrise.ConfigModelFromYAMLBytesWithValidation(configBase64Bytes, validation) if err != nil { return models.BitriseDataModel{}, warnings, fmt.Errorf("Failed to parse bitrise config, error: %s", err) } @@ -911,12 +911,12 @@ func GetBitriseConfigFilePath(bitriseConfigPath string) (string, error) { return bitriseConfigPath, nil } -func CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath string, fullValidation bool) (models.BitriseDataModel, []string, error) { +func CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath string, validation bitrise.ValidationType) (models.BitriseDataModel, []string, error) { var bitriseConfig *models.BitriseDataModel warnings := []string{} if bitriseConfigBase64Data != "" { - config, warns, err := GetBitriseConfigFromBase64Data(bitriseConfigBase64Data, fullValidation) + config, warns, err := GetBitriseConfigFromBase64Data(bitriseConfigBase64Data, validation) warnings = warns if err != nil { return models.BitriseDataModel{}, warnings, fmt.Errorf("failed to get Bitrise config (bitrise.yml) from base 64 data: %w", err) @@ -947,14 +947,14 @@ func CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath } isJSON := filepath.Ext(bitriseConfigPath) == "json" - config, warns, err := bitrise.ConfigModelFromFileContent([]byte(mergedConfigContent), isJSON, fullValidation) + config, warns, err := bitrise.ConfigModelFromFileContent([]byte(mergedConfigContent), isJSON, validation) warnings = warns if err != nil { return models.BitriseDataModel{}, warnings, fmt.Errorf("config (%s) is not valid: %w", bitriseConfigPath, err) } bitriseConfig = &config } else { - config, warns, err := bitrise.ReadBitriseConfig(bitriseConfigPath, fullValidation) + config, warns, err := bitrise.ReadBitriseConfig(bitriseConfigPath, validation) warnings = warns if err != nil { return models.BitriseDataModel{}, warnings, fmt.Errorf("config (%s) is not valid: %w", bitriseConfigPath, err) diff --git a/cli/run_util_pipeline_test.go b/cli/run_util_pipeline_test.go index 7d78f141..1a61681d 100644 --- a/cli/run_util_pipeline_test.go +++ b/cli/run_util_pipeline_test.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "testing" + "github.com/bitrise-io/bitrise/bitrise" "github.com/stretchr/testify/require" ) @@ -236,7 +237,7 @@ func TestValidation(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b64Data := base64.StdEncoding.EncodeToString([]byte(tt.config)) - _, _, err := CreateBitriseConfigFromCLIParams(b64Data, "", true) + _, _, err := CreateBitriseConfigFromCLIParams(b64Data, "", bitrise.ValidationTypeFull) if tt.wantErr != "" { require.EqualError(t, err, tt.wantErr) diff --git a/cli/run_util_test.go b/cli/run_util_test.go index c491bcf7..1faf7638 100644 --- a/cli/run_util_test.go +++ b/cli/run_util_test.go @@ -332,7 +332,7 @@ workflows: configBytes := []byte(configStr) configBase64Str := base64.StdEncoding.EncodeToString(configBytes) - config, warnings, err := GetBitriseConfigFromBase64Data(configBase64Str, true) + config, warnings, err := GetBitriseConfigFromBase64Data(configBase64Str, bitrise.ValidationTypeFull) require.NoError(t, err) require.Equal(t, 0, len(warnings)) diff --git a/cli/run_util_validataion_test.go b/cli/run_util_validataion_test.go index 286709df..3a246eae 100644 --- a/cli/run_util_validataion_test.go +++ b/cli/run_util_validataion_test.go @@ -4,15 +4,16 @@ import ( "encoding/base64" "testing" + "github.com/bitrise-io/bitrise/bitrise" "github.com/stretchr/testify/require" ) func TestMinimalValidation(t *testing.T) { tests := []struct { - name string - config string - fullValidation bool - wantErr string + name string + config string + validation bitrise.ValidationType + wantErr string }{ { name: "Valid config", @@ -28,7 +29,7 @@ workflows: a: {} b: {} `, - fullValidation: true, + validation: bitrise.ValidationTypeFull, }, { name: "Only valid with minimal validation", @@ -43,6 +44,7 @@ pipelines: workflows: a: {} b: {}`, + validation: bitrise.ValidationTypeMinimal, }, { name: "Invalid config even with minimal validation", @@ -58,14 +60,15 @@ workflows: before_run: [c] b: {} `, - wantErr: "failed to get Bitrise config (bitrise.yml) from base 64 data: Failed to parse bitrise config, error: Workflow does not exist with name c", + validation: bitrise.ValidationTypeMinimal, + wantErr: "failed to get Bitrise config (bitrise.yml) from base 64 data: Failed to parse bitrise config, error: Workflow does not exist with name c", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b64Data := base64.StdEncoding.EncodeToString([]byte(tt.config)) - _, warnings, err := CreateBitriseConfigFromCLIParams(b64Data, "", tt.fullValidation) + _, warnings, err := CreateBitriseConfigFromCLIParams(b64Data, "", tt.validation) if tt.wantErr != "" { require.EqualError(t, err, tt.wantErr) diff --git a/cli/trigger.go b/cli/trigger.go index c1821534..99148ff9 100644 --- a/cli/trigger.go +++ b/cli/trigger.go @@ -5,6 +5,7 @@ import ( "os" "strings" + "github.com/bitrise-io/bitrise/bitrise" "github.com/bitrise-io/bitrise/configs" "github.com/bitrise-io/bitrise/log" "github.com/bitrise-io/bitrise/models" @@ -131,7 +132,7 @@ func trigger(c *cli.Context) error { } // Config validation - bitriseConfig, warnings, err := CreateBitriseConfigFromCLIParams(triggerParams.BitriseConfigBase64Data, triggerParams.BitriseConfigPath, false) + bitriseConfig, warnings, err := CreateBitriseConfigFromCLIParams(triggerParams.BitriseConfigBase64Data, triggerParams.BitriseConfigPath, bitrise.ValidationTypeMinimal) for _, warning := range warnings { log.Warnf("warning: %s", warning) } diff --git a/cli/trigger_check.go b/cli/trigger_check.go index 6935f2b1..fbd07080 100644 --- a/cli/trigger_check.go +++ b/cli/trigger_check.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/bitrise-io/bitrise/bitrise" "github.com/bitrise-io/bitrise/log" "github.com/bitrise-io/bitrise/models" "github.com/bitrise-io/bitrise/output" @@ -125,7 +126,7 @@ func triggerCheck(c *cli.Context) error { } // Config validation - bitriseConfig, warns, err := CreateBitriseConfigFromCLIParams(triggerParams.BitriseConfigBase64Data, triggerParams.BitriseConfigPath, true) + bitriseConfig, warns, err := CreateBitriseConfigFromCLIParams(triggerParams.BitriseConfigBase64Data, triggerParams.BitriseConfigPath, bitrise.ValidationTypeFull) warnings = append(warnings, warns...) if err != nil { registerFatal(fmt.Sprintf("Failed to create config, err: %s", err), warnings, triggerParams.Format) diff --git a/cli/validate.go b/cli/validate.go index 3ce375e5..f32e8cea 100644 --- a/cli/validate.go +++ b/cli/validate.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "github.com/bitrise-io/bitrise/bitrise" "github.com/bitrise-io/bitrise/output" "github.com/bitrise-io/go-utils/colorstring" "github.com/urfave/cli" @@ -154,7 +155,7 @@ func validateBitriseYML(bitriseConfigPath string, bitriseConfigBase64Data string if pth != "" || (pth == "" && bitriseConfigBase64Data != "") { // Config validation - _, warns, err := CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath, true) + _, warns, err := CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath, bitrise.ValidationTypeFull) configValidation := ValidationItemModel{ IsValid: true, Warnings: warns, diff --git a/cli/workflow_list.go b/cli/workflow_list.go index 7e435f55..b57b2552 100644 --- a/cli/workflow_list.go +++ b/cli/workflow_list.go @@ -7,6 +7,7 @@ import ( "sort" "strings" + "github.com/bitrise-io/bitrise/bitrise" "github.com/bitrise-io/bitrise/log" "github.com/bitrise-io/bitrise/output" "github.com/bitrise-io/go-utils/colorstring" @@ -208,7 +209,7 @@ func workflowList(c *cli.Context) error { } // Config validation - bitriseConfig, warns, err := CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath, true) + bitriseConfig, warns, err := CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath, bitrise.ValidationTypeFull) warnings = append(warnings, warns...) if err != nil { logger.Print(NewErrorOutput(fmt.Sprintf("Failed to create bitrise config: %s", err)))