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.
This commit is contained in:
Simone Gotti 2019-08-29 16:14:07 +02:00
parent afcd05244f
commit 53dad95cd0
4 changed files with 45 additions and 4 deletions

View File

@ -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),
}
}

View File

@ -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{

View File

@ -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"

View File

@ -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)),