Skip to content

Commit ef1653b

Browse files
authored
Merge branch 'master' into feat/hook-event-parity
2 parents 1152350 + 048364c commit ef1653b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2222
-1911
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,4 @@ issues:
157157
- funlen
158158
- goconst
159159
- gocyclo
160+
- wsl

api/admin/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func AllUsers(c *gin.Context) {
4545
logrus.Info("Admin: reading all users")
4646

4747
// send API call to capture all users
48-
u, err := database.FromContext(c).GetUserList()
48+
u, err := database.FromContext(c).ListUsers()
4949
if err != nil {
5050
retErr := fmt.Errorf("unable to capture all users: %w", err)
5151

api/authenticate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func Authenticate(c *gin.Context) {
9999
}
100100

101101
// send API call to capture the user logging in
102-
u, err := database.FromContext(c).GetUserName(newUser.GetName())
102+
u, err := database.FromContext(c).GetUserForName(newUser.GetName())
103103
// create a new user account
104104
if len(u.GetName()) == 0 || err != nil {
105105
// create unique id for the user
@@ -314,7 +314,7 @@ func AuthenticateToken(c *gin.Context) {
314314
}
315315

316316
// check if the user exists
317-
u, err = database.FromContext(c).GetUserName(u.GetName())
317+
u, err = database.FromContext(c).GetUserForName(u.GetName())
318318
if err != nil {
319319
retErr := fmt.Errorf("user %s not found", u.GetName())
320320

api/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func CustomMetrics(c *gin.Context) {
7474
// helper function to get the totals of resource types.
7575
func recordGauges(c *gin.Context) {
7676
// send API call to capture the total number of users
77-
u, err := database.FromContext(c).GetUserCount()
77+
u, err := database.FromContext(c).CountUsers()
7878
if err != nil {
7979
logrus.Errorf("unable to get count of all users: %v", err)
8080
}

api/user.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func CreateUser(c *gin.Context) {
8787
}
8888

8989
// send API call to capture the created user
90-
user, _ := database.FromContext(c).GetUserName(input.GetName())
90+
user, _ := database.FromContext(c).GetUserForName(input.GetName())
9191

9292
c.JSON(http.StatusCreated, user)
9393
}
@@ -172,18 +172,8 @@ func GetUsers(c *gin.Context) {
172172
// ensure per_page isn't above or below allowed values
173173
perPage = util.MaxInt(1, util.MinInt(100, perPage))
174174

175-
// send API call to capture the total number of users
176-
t, err := database.FromContext(c).GetUserCount()
177-
if err != nil {
178-
retErr := fmt.Errorf("unable to get users count: %w", err)
179-
180-
util.HandleError(c, http.StatusInternalServerError, retErr)
181-
182-
return
183-
}
184-
185175
// send API call to capture the list of users
186-
users, err := database.FromContext(c).GetUserLiteList(page, perPage)
176+
users, t, err := database.FromContext(c).ListLiteUsers(page, perPage)
187177
if err != nil {
188178
retErr := fmt.Errorf("unable to get users: %w", err)
189179

@@ -311,7 +301,7 @@ func UpdateCurrentUser(c *gin.Context) {
311301
}
312302

313303
// send API call to capture the updated user
314-
u, err = database.FromContext(c).GetUserName(u.GetName())
304+
u, err = database.FromContext(c).GetUserForName(u.GetName())
315305
if err != nil {
316306
retErr := fmt.Errorf("unable to get updated user %s: %w", u.GetName(), err)
317307

@@ -363,7 +353,7 @@ func GetUser(c *gin.Context) {
363353
}).Infof("reading user %s", user)
364354

365355
// send API call to capture the user
366-
u, err := database.FromContext(c).GetUserName(user)
356+
u, err := database.FromContext(c).GetUserForName(user)
367357
if err != nil {
368358
retErr := fmt.Errorf("unable to get user %s: %w", user, err)
369359

@@ -548,7 +538,7 @@ func UpdateUser(c *gin.Context) {
548538
}
549539

550540
// send API call to capture the user
551-
u, err = database.FromContext(c).GetUserName(user)
541+
u, err = database.FromContext(c).GetUserForName(user)
552542
if err != nil {
553543
retErr := fmt.Errorf("unable to get user %s: %w", user, err)
554544

@@ -584,7 +574,7 @@ func UpdateUser(c *gin.Context) {
584574
}
585575

586576
// send API call to capture the updated user
587-
u, _ = database.FromContext(c).GetUserName(user)
577+
u, _ = database.FromContext(c).GetUserForName(user)
588578

589579
c.JSON(http.StatusOK, u)
590580
}
@@ -633,7 +623,7 @@ func DeleteUser(c *gin.Context) {
633623
}).Infof("deleting user %s", user)
634624

635625
// send API call to capture the user
636-
u, err := database.FromContext(c).GetUserName(user)
626+
u, err := database.FromContext(c).GetUserForName(user)
637627
if err != nil {
638628
retErr := fmt.Errorf("unable to get user %s: %w", user, err)
639629

@@ -643,7 +633,7 @@ func DeleteUser(c *gin.Context) {
643633
}
644634

645635
// send API call to remove the user
646-
err = database.FromContext(c).DeleteUser(u.GetID())
636+
err = database.FromContext(c).DeleteUser(u)
647637
if err != nil {
648638
retErr := fmt.Errorf("unable to delete user %s: %w", u.GetName(), err)
649639

compiler/template/native/convert.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,22 @@ import (
1818
func convertPlatformVars(slice raw.StringSliceMap, name string) raw.StringSliceMap {
1919
envs := make(map[string]string)
2020

21+
// iterate through the list of key/value pairs provided
2122
for key, value := range slice {
23+
// lowercase the key
2224
key = strings.ToLower(key)
25+
26+
// check if the key has a 'deployment_parameter_*' prefix
27+
if strings.HasPrefix(key, "deployment_parameter_") {
28+
// add the key/value pair with the 'deployment_parameter_` prefix
29+
//
30+
// this is used to ensure we prevent conflicts with `vela_*` prefixed variables
31+
envs[key] = value
32+
}
33+
34+
// check if the key has a 'vela_*' prefix
2335
if strings.HasPrefix(key, "vela_") {
36+
// add the key/value pair without the 'vela_` prefix
2437
envs[strings.TrimPrefix(key, "vela_")] = value
2538
}
2639
}
@@ -54,14 +67,21 @@ type funcHandler struct {
5467

5568
// returnPlatformVar returns the value of the platform
5669
// variable if it exists within the environment map.
57-
func (h funcHandler) returnPlatformVar(input string) string {
58-
input = strings.ToLower(input)
59-
input = strings.TrimPrefix(input, "vela_")
60-
// check if key exists within map
61-
if _, ok := h.envs[input]; ok {
62-
// return value if exists
63-
return h.envs[input]
70+
func (h funcHandler) returnPlatformVar(key string) string {
71+
// lowercase the key
72+
key = strings.ToLower(key)
73+
74+
// iterate through the list of possible prefixes to look for
75+
for _, prefix := range []string{"deployment_parameter_", "vela_"} {
76+
// trim the prefix from the input key
77+
trimmed := strings.TrimPrefix(key, prefix)
78+
// check if the key exists within map
79+
if _, ok := h.envs[trimmed]; ok {
80+
// return the non-prefixed value if exists
81+
return h.envs[trimmed]
82+
}
6483
}
84+
6585
// return empty string if not exists
6686
return ""
6787
}

compiler/template/native/convert_test.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ func Test_convertPlatformVars(t *testing.T) {
1818
templateName string
1919
want raw.StringSliceMap
2020
}{
21+
{
22+
name: "with all deployment parameter prefixed vars",
23+
slice: raw.StringSliceMap{
24+
"DEPLOYMENT_PARAMETER_IMAGE": "alpine:3.14",
25+
},
26+
templateName: "foo",
27+
want: raw.StringSliceMap{"deployment_parameter_image": "alpine:3.14", "template_name": "foo"},
28+
},
2129
{
2230
name: "with all vela prefixed vars",
2331
slice: raw.StringSliceMap{
@@ -30,15 +38,18 @@ func Test_convertPlatformVars(t *testing.T) {
3038
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"},
3139
},
3240
{
33-
name: "with combination of vela and user vars",
41+
name: "with combination of deployment parameter, vela, and user vars",
3442
slice: raw.StringSliceMap{
35-
"VELA_BUILD_AUTHOR": "octocat",
36-
"VELA_REPO_FULL_NAME": "go-vela/hello-world",
37-
"FOO_VAR1": "test1",
38-
"BAR_VAR1": "test2",
43+
"DEPLOYMENT_PARAMETER_IMAGE": "alpine:3.14",
44+
"VELA_BUILD_AUTHOR": "octocat",
45+
"VELA_REPO_FULL_NAME": "go-vela/hello-world",
46+
"VELA_USER_ADMIN": "true",
47+
"VELA_WORKSPACE": "/vela/src/github.com/go-vela/hello-world",
48+
"FOO_VAR1": "test1",
49+
"BAR_VAR1": "test2",
3950
},
4051
templateName: "foo",
41-
want: raw.StringSliceMap{"build_author": "octocat", "repo_full_name": "go-vela/hello-world", "template_name": "foo"},
52+
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"},
4253
},
4354
}
4455

@@ -66,6 +77,46 @@ func Test_funcHandler_returnPlatformVar(t *testing.T) {
6677
args args
6778
want string
6879
}{
80+
{
81+
name: "existing deployment parameter without prefix (lowercase)",
82+
fields: fields{
83+
envs: raw.StringSliceMap{
84+
"image": "alpine",
85+
},
86+
},
87+
args: args{input: "image"},
88+
want: "alpine",
89+
},
90+
{
91+
name: "existing deployment parameter without prefix (uppercase)",
92+
fields: fields{
93+
envs: raw.StringSliceMap{
94+
"image": "alpine",
95+
},
96+
},
97+
args: args{input: "IMAGE"},
98+
want: "alpine",
99+
},
100+
{
101+
name: "existing deployment parameter with prefix (lowercase)",
102+
fields: fields{
103+
envs: raw.StringSliceMap{
104+
"image": "alpine",
105+
},
106+
},
107+
args: args{input: "deployment_parameter_image"},
108+
want: "alpine",
109+
},
110+
{
111+
name: "existing deployment parameter with prefix (uppercase)",
112+
fields: fields{
113+
envs: raw.StringSliceMap{
114+
"image": "alpine",
115+
},
116+
},
117+
args: args{input: "DEPLOYMENT_PARAMETER_IMAGE"},
118+
want: "alpine",
119+
},
69120
{
70121
name: "existing platform var without prefix (lowercase)",
71122
fields: fields{

compiler/template/starlark/convert.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func convertTemplateVars(m map[string]interface{}) (*starlark.Dict, error) {
4747
// https://pkg.go.dev/go.starlark.net/starlark#StringDict
4848
func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict, error) {
4949
build := starlark.NewDict(0)
50+
deployment := starlark.NewDict(0)
5051
repo := starlark.NewDict(0)
5152
user := starlark.NewDict(0)
5253
system := starlark.NewDict(0)
@@ -57,6 +58,11 @@ func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict,
5758
return nil, err
5859
}
5960

61+
err = dict.SetKey(starlark.String("deployment"), deployment)
62+
if err != nil {
63+
return nil, err
64+
}
65+
6066
err = dict.SetKey(starlark.String("repo"), repo)
6167
if err != nil {
6268
return nil, err
@@ -77,31 +83,47 @@ func convertPlatformVars(slice raw.StringSliceMap, name string) (*starlark.Dict,
7783
return nil, err
7884
}
7985

86+
// iterate through the list of key/value pairs provided
8087
for key, value := range slice {
88+
// lowercase the key
8189
key = strings.ToLower(key)
82-
if strings.HasPrefix(key, "vela_") {
83-
key = strings.TrimPrefix(key, "vela_")
84-
85-
switch {
86-
case strings.HasPrefix(key, "build_"):
87-
err := build.SetKey(starlark.String(strings.TrimPrefix(key, "build_")), starlark.String(value))
88-
if err != nil {
89-
return nil, err
90-
}
91-
case strings.HasPrefix(key, "repo_"):
92-
err := repo.SetKey(starlark.String(strings.TrimPrefix(key, "repo_")), starlark.String(value))
93-
if err != nil {
94-
return nil, err
95-
}
96-
case strings.HasPrefix(key, "user_"):
97-
err := user.SetKey(starlark.String(strings.TrimPrefix(key, "user_")), starlark.String(value))
98-
if err != nil {
99-
return nil, err
100-
}
101-
default:
102-
err := system.SetKey(starlark.String(key), starlark.String(value))
103-
if err != nil {
104-
return nil, err
90+
91+
// iterate through the list of possible prefixes to look for
92+
for _, prefix := range []string{"deployment_parameter_", "vela_"} {
93+
// check if the key has the prefix
94+
if strings.HasPrefix(key, prefix) {
95+
// trim the prefix from the input key
96+
key = strings.TrimPrefix(key, prefix)
97+
98+
// check if the prefix is from 'vela_*'
99+
if strings.EqualFold(prefix, "vela_") {
100+
switch {
101+
case strings.HasPrefix(key, "build_"):
102+
err := build.SetKey(starlark.String(strings.TrimPrefix(key, "build_")), starlark.String(value))
103+
if err != nil {
104+
return nil, err
105+
}
106+
case strings.HasPrefix(key, "repo_"):
107+
err := repo.SetKey(starlark.String(strings.TrimPrefix(key, "repo_")), starlark.String(value))
108+
if err != nil {
109+
return nil, err
110+
}
111+
case strings.HasPrefix(key, "user_"):
112+
err := user.SetKey(starlark.String(strings.TrimPrefix(key, "user_")), starlark.String(value))
113+
if err != nil {
114+
return nil, err
115+
}
116+
default:
117+
err := system.SetKey(starlark.String(key), starlark.String(value))
118+
if err != nil {
119+
return nil, err
120+
}
121+
}
122+
} else { // prefix is from 'deployment_parameter_*'
123+
err := deployment.SetKey(starlark.String(key), starlark.String(value))
124+
if err != nil {
125+
return nil, err
126+
}
105127
}
106128
}
107129
}

0 commit comments

Comments
 (0)