Skip to content

Commit

Permalink
Use enum for validation type
Browse files Browse the repository at this point in the history
  • Loading branch information
tothszabi committed Jan 8, 2025
1 parent b5e5a49 commit 165a667
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 31 deletions.
30 changes: 19 additions & 11 deletions bitrise/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -233,7 +241,7 @@ func ConfigModelFromFileContent(configBytes []byte, isJSON bool, fullValidation
return
}

warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, true)
warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, validation)
return
}

Expand All @@ -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
}

Expand All @@ -263,7 +271,7 @@ func ConfigModelFromJSONBytes(configBytes []byte) (bitriseData models.BitriseDat
return
}

warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, true)
warnings, err = normalizeValidateFillMissingDefaults(&bitriseData, ValidationTypeFull)
return
}

Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions cli/run_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cli/run_util_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"testing"

"github.com/bitrise-io/bitrise/bitrise"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cli/run_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
17 changes: 10 additions & 7 deletions cli/run_util_validataion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,7 +29,7 @@ workflows:
a: {}
b: {}
`,
fullValidation: true,
validation: bitrise.ValidationTypeFull,
},
{
name: "Only valid with minimal validation",
Expand All @@ -43,6 +44,7 @@ pipelines:
workflows:
a: {}
b: {}`,
validation: bitrise.ValidationTypeMinimal,
},
{
name: "Invalid config even with minimal validation",
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cli/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion cli/trigger_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cli/workflow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit 165a667

Please sign in to comment.