From 53dad95cd071dd23d931431fd2797e81b93dd2a7 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 29 Aug 2019 16:14:07 +0200 Subject: [PATCH] cmd: fix variable create/update In c1ff28ef9f we exported various types. Unfortunately the types used by cmd variable create/update are the wrong types and marshalling fails. Fix it using the right type. In future this internal types should be exported. --- cmd/agola/cmd/projectvariablecreate.go | 39 ++++++++++++++++++- cmd/agola/cmd/projectvariableupdate.go | 2 +- .../services/executor/driver/docker_test.go | 2 +- internal/services/gateway/api/variable.go | 6 +++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/agola/cmd/projectvariablecreate.go b/cmd/agola/cmd/projectvariablecreate.go index c3f7925..666c050 100644 --- a/cmd/agola/cmd/projectvariablecreate.go +++ b/cmd/agola/cmd/projectvariablecreate.go @@ -19,6 +19,8 @@ import ( "io/ioutil" "os" + config "agola.io/agola/internal/config" + cstypes "agola.io/agola/services/configstore/types" gwapitypes "agola.io/agola/services/gateway/api/types" gwclient "agola.io/agola/services/gateway/client" @@ -91,7 +93,7 @@ type VariableValue struct { SecretName string `json:"secret_name,omitempty"` SecretVar string `json:"secret_var,omitempty"` - When *gwapitypes.When `json:"when,omitempty"` + When *config.When `json:"when,omitempty"` } func variableCreate(cmd *cobra.Command, ownertype string, args []string) error { @@ -121,7 +123,7 @@ func variableCreate(cmd *cobra.Command, ownertype string, args []string) error { rvalues = append(rvalues, gwapitypes.VariableValueRequest{ SecretName: value.SecretName, SecretVar: value.SecretVar, - When: value.When, + When: fromCsWhen(value.When.ToCSWhen()), }) } req := &gwapitypes.CreateVariableRequest{ @@ -148,3 +150,36 @@ func variableCreate(cmd *cobra.Command, ownertype string, args []string) error { return nil } + +func fromCsWhenCondition(apiwc cstypes.WhenCondition) gwapitypes.WhenCondition { + return gwapitypes.WhenCondition{ + Type: gwapitypes.WhenConditionType(apiwc.Type), + Match: apiwc.Match, + } +} + +func fromCsWhenConditions(apiwcs *cstypes.WhenConditions) *gwapitypes.WhenConditions { + if apiwcs == nil { + return nil + } + wcs := &gwapitypes.WhenConditions{ + Include: make([]gwapitypes.WhenCondition, len(apiwcs.Include)), + Exclude: make([]gwapitypes.WhenCondition, len(apiwcs.Exclude)), + } + for i, include := range apiwcs.Include { + wcs.Include[i] = fromCsWhenCondition(include) + } + for i, exclude := range apiwcs.Exclude { + wcs.Exclude[i] = fromCsWhenCondition(exclude) + } + + return wcs +} + +func fromCsWhen(apiwhen *cstypes.When) *gwapitypes.When { + return &gwapitypes.When{ + Branch: fromCsWhenConditions(apiwhen.Branch), + Tag: fromCsWhenConditions(apiwhen.Tag), + Ref: fromCsWhenConditions(apiwhen.Ref), + } +} diff --git a/cmd/agola/cmd/projectvariableupdate.go b/cmd/agola/cmd/projectvariableupdate.go index 1b54969..fb2b6ca 100644 --- a/cmd/agola/cmd/projectvariableupdate.go +++ b/cmd/agola/cmd/projectvariableupdate.go @@ -94,7 +94,7 @@ func variableUpdate(cmd *cobra.Command, ownertype string, args []string) error { rvalues = append(rvalues, gwapitypes.VariableValueRequest{ SecretName: value.SecretName, SecretVar: value.SecretVar, - When: value.When, + When: fromCsWhen(value.When.ToCSWhen()), }) } req := &gwapitypes.UpdateVariableRequest{ diff --git a/internal/services/executor/driver/docker_test.go b/internal/services/executor/driver/docker_test.go index 81dfa79..a228ad7 100644 --- a/internal/services/executor/driver/docker_test.go +++ b/internal/services/executor/driver/docker_test.go @@ -23,9 +23,9 @@ import ( "time" slog "agola.io/agola/internal/log" + "agola.io/agola/internal/testutil" "github.com/docker/docker/api/types" uuid "github.com/satori/go.uuid" - "agola.io/agola/internal/testutil" "github.com/google/go-cmp/cmp" "go.uber.org/zap" diff --git a/internal/services/gateway/api/variable.go b/internal/services/gateway/api/variable.go index cb82490..1fb09e6 100644 --- a/internal/services/gateway/api/variable.go +++ b/internal/services/gateway/api/variable.go @@ -235,6 +235,9 @@ func fromApiWhenCondition(apiwc gwapitypes.WhenCondition) cstypes.WhenCondition } func fromApiWhenConditions(apiwcs *gwapitypes.WhenConditions) *cstypes.WhenConditions { + if apiwcs == nil { + return nil + } wcs := &cstypes.WhenConditions{ Include: make([]cstypes.WhenCondition, len(apiwcs.Include)), Exclude: make([]cstypes.WhenCondition, len(apiwcs.Exclude)), @@ -265,6 +268,9 @@ func fromCsWhenCondition(apiwc cstypes.WhenCondition) gwapitypes.WhenCondition { } func fromCsWhenConditions(apiwcs *cstypes.WhenConditions) *gwapitypes.WhenConditions { + if apiwcs == nil { + return nil + } wcs := &gwapitypes.WhenConditions{ Include: make([]gwapitypes.WhenCondition, len(apiwcs.Include)), Exclude: make([]gwapitypes.WhenCondition, len(apiwcs.Exclude)),