Skip to content

Commit

Permalink
Fix config normalise (#987)
Browse files Browse the repository at this point in the history
* Fix bitrise config normlize

* fix workflow normalize

* Normalize missing config fields

* Improve StepListItem multiple keys error message

* Fix how models get reassigned after normalize

* Pull latest envman version
  • Loading branch information
godrei authored Jul 30, 2024
1 parent 0026e43 commit db63613
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 183 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.21

require (
github.com/bitrise-io/colorstring v0.0.0-20180614154802-a8cd70115192
github.com/bitrise-io/envman v0.0.0-20240624065735-ca81d0a80500
github.com/bitrise-io/envman v0.0.0-20240730123632-8066eeb61599
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.19
github.com/bitrise-io/go-utils v1.0.13
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22
github.com/bitrise-io/goinp v0.0.0-20240103152431-054ed78518ef
github.com/bitrise-io/stepman v0.0.0-20240628140527-5e941cdb67a1
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
github.com/gofrs/uuid v4.3.1+incompatible
github.com/hashicorp/go-version v1.4.0
Expand All @@ -33,6 +32,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/bitrise-io/colorstring v0.0.0-20180614154802-a8cd70115192 h1:vSHYT6kCL/iOT9BVuUPm0tVcbW57r5zldLWg0aB7qbQ=
github.com/bitrise-io/colorstring v0.0.0-20180614154802-a8cd70115192/go.mod h1:CIHVcxZUvsG99XUJV6JlR7okNsMMGY81jMvPC20W+O0=
github.com/bitrise-io/envman v0.0.0-20240624065735-ca81d0a80500 h1:U6ZSNtNnjeFBW2rS9ay7e4Hp1Ln9U/wfykrVfDNR8QY=
github.com/bitrise-io/envman v0.0.0-20240624065735-ca81d0a80500/go.mod h1:7yJQdVdq8BxJYq2xjG0yViQf/aspJLJ/xqk/OnP6lGE=
github.com/bitrise-io/envman v0.0.0-20240730123632-8066eeb61599 h1:UaudsJR8LbzL7wjz5D2RVtxN7RYnTL0ZoX+lA9yuqDI=
github.com/bitrise-io/envman v0.0.0-20240730123632-8066eeb61599/go.mod h1:7yJQdVdq8BxJYq2xjG0yViQf/aspJLJ/xqk/OnP6lGE=
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.19 h1:zP9oZRZA9JGPELjBcWSzo/nd8g7apc7EBd7KZHHFdvs=
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.19/go.mod h1:/ueNOKnsjcUrlt8Ck75WRNspL7E6nAAylvl9oGJtYio=
github.com/bitrise-io/go-utils v1.0.13 h1:1QENhTS/JlKH9F7+/nB+TtbTcor6jGrE6cQ4CJWfp5U=
Expand Down
101 changes: 77 additions & 24 deletions models/models_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,61 @@ func (config *BitriseDataModel) getPipelineIDs() []string {
// ----------------------------
// --- Normalize

func (bundle *StepBundleListItemModel) Normalize() error {
for _, env := range bundle.Environments {
func (bundle *StepBundleModel) Normalize() error {
for idx, stepListItem := range bundle.Steps {
stepID, step, err := stepListItem.GetStepIDAndStep()
if err != nil {
return err
}

if err := step.Normalize(); err != nil {
return err
}
stepListItem[stepID] = step
bundle.Steps[idx] = stepListItem
}

for i, env := range bundle.Environments {
if err := env.Normalize(); err != nil {
return err
}
bundle.Environments[i] = env
}
return nil
}

func (bundle *StepBundleModel) Normalize() error {
for _, env := range bundle.Environments {
func (bundle *StepBundleListItemModel) Normalize() error {
for i, env := range bundle.Environments {
if err := env.Normalize(); err != nil {
return err
}
bundle.Environments[i] = env
}
return nil
}

func (with *WithModel) Normalize() error {
for idx, stepListItem := range with.Steps {
stepID, step, err := stepListItem.GetStepIDAndStep()
if err != nil {
return err
}

if err := step.Normalize(); err != nil {
return err
}
stepListItem[stepID] = step
with.Steps[idx] = stepListItem
}
return nil
}

func (container *Container) Normalize() error {
for _, env := range container.Envs {
for i, env := range container.Envs {
if err := env.Normalize(); err != nil {
return err
}
container.Envs[i] = env
}
return nil
}
Expand All @@ -143,8 +175,8 @@ func (workflow *WorkflowModel) Normalize() error {
}
}

for _, stepListItem := range workflow.Steps {
_, t, err := stepListItem.GetKeyAndType()
for idx, stepListItem := range workflow.Steps {
key, t, err := stepListItem.GetKeyAndType()
if err != nil {
return err
}
Expand All @@ -157,6 +189,9 @@ func (workflow *WorkflowModel) Normalize() error {
if err := step.Normalize(); err != nil {
return err
}

stepListItem[key] = *step
workflow.Steps[idx] = stepListItem
} else if t == StepListItemTypeBundle {
bundle, err := stepListItem.GetBundle()
if err != nil {
Expand All @@ -166,17 +201,39 @@ func (workflow *WorkflowModel) Normalize() error {
if err := bundle.Normalize(); err != nil {
return err
}

stepListItem[StepListItemStepBundleKeyPrefix+key] = *bundle
workflow.Steps[idx] = stepListItem
} else if t == StepListItemTypeWith {
with, err := stepListItem.GetWith()
if err != nil {
return err
}

if err := with.Normalize(); err != nil {
return err
}

stepListItem[key] = *with
workflow.Steps[idx] = stepListItem
}
}

normalizedMeta, err := stepmanModels.JSONMarshallable(workflow.Meta)
if err != nil {
return fmt.Errorf("failed to normalize meta: %w", err)
}
workflow.Meta = normalizedMeta

return nil
}

func (app *AppModel) Normalize() error {
for _, env := range app.Environments {
for idx, env := range app.Environments {
if err := env.Normalize(); err != nil {
return err
}
app.Environments[idx] = env
}
return nil
}
Expand All @@ -192,28 +249,32 @@ func (config *BitriseDataModel) Normalize() error {
}
config.TriggerMap = normalizedTriggerMap

for _, container := range config.Containers {
for containerID, container := range config.Containers {
if err := container.Normalize(); err != nil {
return fmt.Errorf("failed to normalize container: %w", err)
}
config.Containers[containerID] = container
}

for _, container := range config.Services {
if err := container.Normalize(); err != nil {
for serviceID, service := range config.Services {
if err := service.Normalize(); err != nil {
return fmt.Errorf("failed to normalize service: %w", err)
}
config.Services[serviceID] = service
}

for _, stepBundle := range config.StepBundles {
for stepBundleID, stepBundle := range config.StepBundles {
if err := stepBundle.Normalize(); err != nil {
return fmt.Errorf("failed to normalize step_bundle: %w", err)
}
config.StepBundles[stepBundleID] = stepBundle
}

for _, workflow := range config.Workflows {
for workflowID, workflow := range config.Workflows {
if err := workflow.Normalize(); err != nil {
return fmt.Errorf("failed to normalize workflow: %w", err)
}
config.Workflows[workflowID] = workflow
}

normalizedMeta, err := stepmanModels.JSONMarshallable(config.Meta)
Expand Down Expand Up @@ -269,7 +330,7 @@ func (container *Container) Validate() error {
return nil
}

func (with WithModel) Validate(workflowID string, containers, services map[string]Container) ([]string, error) {
func (with *WithModel) Validate(workflowID string, containers, services map[string]Container) ([]string, error) {
var warnings []string

if with.ContainerID != "" {
Expand Down Expand Up @@ -655,7 +716,7 @@ func validateWorkflows(config *BitriseDataModel) ([]string, error) {
}

// TODO: Why is this assignment needed?
stepListItem[stepID] = step
stepListItem[stepID] = *step
} else if t == StepListItemTypeWith {
with, err := stepListItem.GetWith()
if err != nil {
Expand Down Expand Up @@ -1252,7 +1313,7 @@ func (stepListItem *StepListItemModel) GetKeyAndType() (string, StepListItemType
}

if len(*stepListItem) > 1 {
return "", StepListItemTypeUnknown, errors.New("StepListItem contains more than 1 key-value pair")
return "", StepListItemTypeUnknown, fmt.Errorf("StepListItem contains more than 1 key-value pair: %#v", *stepListItem)
}

for key := range *stepListItem {
Expand Down Expand Up @@ -1314,14 +1375,6 @@ func (stepListItem *StepListItemModel) GetStep() (*stepmanModels.StepModel, erro
break
}

// StepListItemModel is a map[string]interface{}, when it comes from a JSON/YAML unmarshal
// the StepModel has a pointer type.
sPtr, ok := value.(*stepmanModels.StepModel)
if ok {
stepPtr = sPtr
break
}

break
}

Expand Down
Loading

0 comments on commit db63613

Please sign in to comment.