Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ issues:
- funlen
- goconst
- gocyclo
- wsl
34 changes: 27 additions & 7 deletions compiler/template/native/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,22 @@ import (
func convertPlatformVars(slice raw.StringSliceMap, name string) raw.StringSliceMap {
envs := make(map[string]string)

// iterate through the list of key/value pairs provided
for key, value := range slice {
// lowercase the key
key = strings.ToLower(key)

// check if the key has a 'deployment_parameter_*' prefix
if strings.HasPrefix(key, "deployment_parameter_") {
// add the key/value pair with the 'deployment_parameter_` prefix
//
// this is used to ensure we prevent conflicts with `vela_*` prefixed variables
envs[key] = value
}

// check if the key has a 'vela_*' prefix
if strings.HasPrefix(key, "vela_") {
// add the key/value pair without the 'vela_` prefix
envs[strings.TrimPrefix(key, "vela_")] = value
}
}
Expand Down Expand Up @@ -54,14 +67,21 @@ type funcHandler struct {

// returnPlatformVar returns the value of the platform
// variable if it exists within the environment map.
func (h funcHandler) returnPlatformVar(input string) string {
input = strings.ToLower(input)
input = strings.TrimPrefix(input, "vela_")
// check if key exists within map
if _, ok := h.envs[input]; ok {
// return value if exists
return h.envs[input]
func (h funcHandler) returnPlatformVar(key string) string {
// lowercase the key
key = strings.ToLower(key)

// iterate through the list of possible prefixes to look for
for _, prefix := range []string{"deployment_parameter_", "vela_"} {
// trim the prefix from the input key
trimmed := strings.TrimPrefix(key, prefix)
// check if the key exists within map
if _, ok := h.envs[trimmed]; ok {
// return the non-prefixed value if exists
return h.envs[trimmed]
}
}

// return empty string if not exists
return ""
}
63 changes: 57 additions & 6 deletions compiler/template/native/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func Test_convertPlatformVars(t *testing.T) {
templateName string
want raw.StringSliceMap
}{
{
name: "with all deployment parameter prefixed vars",
slice: raw.StringSliceMap{
"DEPLOYMENT_PARAMETER_IMAGE": "alpine:3.14",
},
templateName: "foo",
want: raw.StringSliceMap{"deployment_parameter_image": "alpine:3.14", "template_name": "foo"},
},
{
name: "with all vela prefixed vars",
slice: raw.StringSliceMap{
Expand All @@ -30,15 +38,18 @@ func Test_convertPlatformVars(t *testing.T) {
want: raw.StringSliceMap{"build_author": "octocat", "repo_full_name": "go-vela/hello-world", "user_admin": "true", "workspace": "/vela/src/github.com/go-vela/hello-world", "template_name": "foo"},
},
{
name: "with combination of vela and user vars",
name: "with combination of deployment parameter, vela, and user vars",
slice: raw.StringSliceMap{
"VELA_BUILD_AUTHOR": "octocat",
"VELA_REPO_FULL_NAME": "go-vela/hello-world",
"FOO_VAR1": "test1",
"BAR_VAR1": "test2",
"DEPLOYMENT_PARAMETER_IMAGE": "alpine:3.14",
"VELA_BUILD_AUTHOR": "octocat",
"VELA_REPO_FULL_NAME": "go-vela/hello-world",
"VELA_USER_ADMIN": "true",
"VELA_WORKSPACE": "/vela/src/github.com/go-vela/hello-world",
"FOO_VAR1": "test1",
"BAR_VAR1": "test2",
},
templateName: "foo",
want: raw.StringSliceMap{"build_author": "octocat", "repo_full_name": "go-vela/hello-world", "template_name": "foo"},
want: raw.StringSliceMap{"deployment_parameter_image": "alpine:3.14", "build_author": "octocat", "repo_full_name": "go-vela/hello-world", "user_admin": "true", "workspace": "/vela/src/github.com/go-vela/hello-world", "template_name": "foo"},
},
}

Expand Down Expand Up @@ -66,6 +77,46 @@ func Test_funcHandler_returnPlatformVar(t *testing.T) {
args args
want string
}{
{
name: "existing deployment parameter without prefix (lowercase)",
fields: fields{
envs: raw.StringSliceMap{
"image": "alpine",
},
},
args: args{input: "image"},
want: "alpine",
},
{
name: "existing deployment parameter without prefix (uppercase)",
fields: fields{
envs: raw.StringSliceMap{
"image": "alpine",
},
},
args: args{input: "IMAGE"},
want: "alpine",
},
{
name: "existing deployment parameter with prefix (lowercase)",
fields: fields{
envs: raw.StringSliceMap{
"image": "alpine",
},
},
args: args{input: "deployment_parameter_image"},
want: "alpine",
},
{
name: "existing deployment parameter with prefix (uppercase)",
fields: fields{
envs: raw.StringSliceMap{
"image": "alpine",
},
},
args: args{input: "DEPLOYMENT_PARAMETER_IMAGE"},
want: "alpine",
},
{
name: "existing platform var without prefix (lowercase)",
fields: fields{
Expand Down
68 changes: 45 additions & 23 deletions compiler/template/starlark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func convertTemplateVars(m map[string]interface{}) (*starlark.Dict, error) {
// https://pkg.go.dev/go.starlark.net/starlark#StringDict
func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict, error) {
build := starlark.NewDict(0)
deployment := starlark.NewDict(0)
repo := starlark.NewDict(0)
user := starlark.NewDict(0)
system := starlark.NewDict(0)
Expand All @@ -57,6 +58,11 @@ func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict,
return nil, err
}

err = dict.SetKey(starlark.String("deployment"), deployment)
if err != nil {
return nil, err
}

err = dict.SetKey(starlark.String("repo"), repo)
if err != nil {
return nil, err
Expand All @@ -77,31 +83,47 @@ func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict,
return nil, err
}

// iterate through the list of key/value pairs provided
for key, value := range slice {
// lowercase the key
key = strings.ToLower(key)
if strings.HasPrefix(key, "vela_") {
key = strings.TrimPrefix(key, "vela_")

switch {
case strings.HasPrefix(key, "build_"):
err := build.SetKey(starlark.String(strings.TrimPrefix(key, "build_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "repo_"):
err := repo.SetKey(starlark.String(strings.TrimPrefix(key, "repo_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "user_"):
err := user.SetKey(starlark.String(strings.TrimPrefix(key, "user_")), starlark.String(value))
if err != nil {
return nil, err
}
default:
err := system.SetKey(starlark.String(key), starlark.String(value))
if err != nil {
return nil, err

// iterate through the list of possible prefixes to look for
for _, prefix := range []string{"deployment_parameter_", "vela_"} {
// check if the key has the prefix
if strings.HasPrefix(key, prefix) {
// trim the prefix from the input key
key = strings.TrimPrefix(key, prefix)

// check if the prefix is from 'vela_*'
if strings.EqualFold(prefix, "vela_") {
switch {
case strings.HasPrefix(key, "build_"):
err := build.SetKey(starlark.String(strings.TrimPrefix(key, "build_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "repo_"):
err := repo.SetKey(starlark.String(strings.TrimPrefix(key, "repo_")), starlark.String(value))
if err != nil {
return nil, err
}
case strings.HasPrefix(key, "user_"):
err := user.SetKey(starlark.String(strings.TrimPrefix(key, "user_")), starlark.String(value))
if err != nil {
return nil, err
}
default:
err := system.SetKey(starlark.String(key), starlark.String(value))
if err != nil {
return nil, err
}
}
} else { // prefix is from 'deployment_parameter_*'
err := deployment.SetKey(starlark.String(key), starlark.String(value))
if err != nil {
return nil, err
}
}
}
}
Expand Down
Loading