Skip to content

Commit d0f24ff

Browse files
authored
1 parent 0407a40 commit d0f24ff

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

models/actions/variable.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func init() {
3131
}
3232

3333
func (v *ActionVariable) Validate() error {
34-
if v.OwnerID == 0 && v.RepoID == 0 {
35-
return errors.New("the variable is not bound to any scope")
34+
if v.OwnerID != 0 && v.RepoID != 0 {
35+
return errors.New("a variable should not be bound to an owner and a repository at the same time")
3636
}
3737
return nil
3838
}
@@ -58,12 +58,8 @@ type FindVariablesOpts struct {
5858

5959
func (opts FindVariablesOpts) ToConds() builder.Cond {
6060
cond := builder.NewCond()
61-
if opts.OwnerID > 0 {
62-
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
63-
}
64-
if opts.RepoID > 0 {
65-
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
66-
}
61+
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
62+
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
6763
return cond
6864
}
6965

routers/api/actions/runner/utils.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func getSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) map[s
9494
func getVariablesOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string {
9595
variables := map[string]string{}
9696

97+
// Global
98+
globalVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{})
99+
if err != nil {
100+
log.Error("find global variables: %v", err)
101+
}
102+
97103
// Org / User level
98104
ownerVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{OwnerID: task.Job.Run.Repo.OwnerID})
99105
if err != nil {
@@ -106,8 +112,8 @@ func getVariablesOfTask(ctx context.Context, task *actions_model.ActionTask) map
106112
log.Error("find variables of repo: %d, error: %v", task.Job.Run.RepoID, err)
107113
}
108114

109-
// Level precedence: Repo > Org / User
110-
for _, v := range append(ownerVariables, repoVariables...) {
115+
// Level precedence: Repo > Org / User > Global
116+
for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) {
111117
variables[v.Name] = v.Data
112118
}
113119

routers/web/repo/setting/variables.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
)
1616

1717
const (
18-
tplRepoVariables base.TplName = "repo/settings/actions"
19-
tplOrgVariables base.TplName = "org/settings/actions"
20-
tplUserVariables base.TplName = "user/settings/actions"
18+
tplRepoVariables base.TplName = "repo/settings/actions"
19+
tplOrgVariables base.TplName = "org/settings/actions"
20+
tplUserVariables base.TplName = "user/settings/actions"
21+
tplAdminVariables base.TplName = "admin/actions"
2122
)
2223

2324
type variablesCtx struct {
@@ -26,13 +27,15 @@ type variablesCtx struct {
2627
IsRepo bool
2728
IsOrg bool
2829
IsUser bool
30+
IsGlobal bool
2931
VariablesTemplate base.TplName
3032
RedirectLink string
3133
}
3234

3335
func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) {
3436
if ctx.Data["PageIsRepoSettings"] == true {
3537
return &variablesCtx{
38+
OwnerID: 0,
3639
RepoID: ctx.Repo.Repository.ID,
3740
IsRepo: true,
3841
VariablesTemplate: tplRepoVariables,
@@ -48,6 +51,7 @@ func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) {
4851
}
4952
return &variablesCtx{
5053
OwnerID: ctx.ContextUser.ID,
54+
RepoID: 0,
5155
IsOrg: true,
5256
VariablesTemplate: tplOrgVariables,
5357
RedirectLink: ctx.Org.OrgLink + "/settings/actions/variables",
@@ -57,12 +61,23 @@ func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) {
5761
if ctx.Data["PageIsUserSettings"] == true {
5862
return &variablesCtx{
5963
OwnerID: ctx.Doer.ID,
64+
RepoID: 0,
6065
IsUser: true,
6166
VariablesTemplate: tplUserVariables,
6267
RedirectLink: setting.AppSubURL + "/user/settings/actions/variables",
6368
}, nil
6469
}
6570

71+
if ctx.Data["PageIsAdmin"] == true {
72+
return &variablesCtx{
73+
OwnerID: 0,
74+
RepoID: 0,
75+
IsGlobal: true,
76+
VariablesTemplate: tplAdminVariables,
77+
RedirectLink: setting.AppSubURL + "/admin/actions/variables",
78+
}, nil
79+
}
80+
6681
return nil, errors.New("unable to set Variables context")
6782
}
6883

routers/web/web.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func registerRoutes(m *web.Route) {
417417
m.Post("/packagist/{id}", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksEditPost)
418418
}
419419

420-
addSettingVariablesRoutes := func() {
420+
addSettingsVariablesRoutes := func() {
421421
m.Group("/variables", func() {
422422
m.Get("", repo_setting.Variables)
423423
m.Post("/new", web.Bind(forms.EditVariableForm{}), repo_setting.VariableCreate)
@@ -618,7 +618,7 @@ func registerRoutes(m *web.Route) {
618618
m.Get("", user_setting.RedirectToDefaultSetting)
619619
addSettingsRunnersRoutes()
620620
addSettingsSecretsRoutes()
621-
addSettingVariablesRoutes()
621+
addSettingsVariablesRoutes()
622622
}, actions.MustEnableActions)
623623

624624
m.Get("/organization", user_setting.Organization)
@@ -763,6 +763,7 @@ func registerRoutes(m *web.Route) {
763763
m.Group("/actions", func() {
764764
m.Get("", admin.RedirectToDefaultSetting)
765765
addSettingsRunnersRoutes()
766+
addSettingsVariablesRoutes()
766767
})
767768
}, adminReq, ctxDataSet("EnableOAuth2", setting.OAuth2.Enable, "EnablePackages", setting.Packages.Enabled))
768769
// ***** END: Admin *****
@@ -905,7 +906,7 @@ func registerRoutes(m *web.Route) {
905906
m.Get("", org_setting.RedirectToDefaultSetting)
906907
addSettingsRunnersRoutes()
907908
addSettingsSecretsRoutes()
908-
addSettingVariablesRoutes()
909+
addSettingsVariablesRoutes()
909910
}, actions.MustEnableActions)
910911

911912
m.Methods("GET,POST", "/delete", org.SettingsDelete)
@@ -1084,7 +1085,7 @@ func registerRoutes(m *web.Route) {
10841085
m.Get("", repo_setting.RedirectToDefaultSetting)
10851086
addSettingsRunnersRoutes()
10861087
addSettingsSecretsRoutes()
1087-
addSettingVariablesRoutes()
1088+
addSettingsVariablesRoutes()
10881089
}, actions.MustEnableActions)
10891090
// the follow handler must be under "settings", otherwise this incomplete repo can't be accessed
10901091
m.Group("/migrate", func() {

templates/admin/actions.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
{{if eq .PageType "runners"}}
44
{{template "shared/actions/runner_list" .}}
55
{{end}}
6+
{{if eq .PageType "variables"}}
7+
{{template "shared/variables/variable_list" .}}
8+
{{end}}
69
</div>
710
{{template "admin/layout_footer" .}}

templates/admin/navbar.tmpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@
6060
{{end}}
6161
{{end}}
6262
{{if .EnableActions}}
63-
<details class="item toggleable-item" {{if .PageIsSharedSettingsRunners}}open{{end}}>
63+
<details class="item toggleable-item" {{if or .PageIsSharedSettingsRunners .PageIsSharedSettingsVariables}}open{{end}}>
6464
<summary>{{ctx.Locale.Tr "actions.actions"}}</summary>
6565
<div class="menu">
6666
<a class="{{if .PageIsSharedSettingsRunners}}active {{end}}item" href="{{AppSubUrl}}/admin/actions/runners">
6767
{{ctx.Locale.Tr "actions.runners"}}
6868
</a>
69+
<a class="{{if .PageIsSharedSettingsVariables}}active {{end}}item" href="{{AppSubUrl}}/admin/actions/variables">
70+
{{ctx.Locale.Tr "actions.variables"}}
71+
</a>
6972
</div>
7073
</details>
7174
{{end}}

0 commit comments

Comments
 (0)