mirror of https://github.com/go-gitea/gitea
Enhance routers for the Actions variable operations (#33547)
- Find the variable before updating or deleting - Move the main logic from `routers/web/repo/setting/variables.go` to `routers/web/shared/actions/variables.go`. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>pull/33551/head^2
parent
e3adb686bb
commit
c422f179dd
@ -1,140 +0,0 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package setting |
||||
|
||||
import ( |
||||
"errors" |
||||
"net/http" |
||||
|
||||
"code.gitea.io/gitea/modules/setting" |
||||
"code.gitea.io/gitea/modules/templates" |
||||
shared "code.gitea.io/gitea/routers/web/shared/actions" |
||||
shared_user "code.gitea.io/gitea/routers/web/shared/user" |
||||
"code.gitea.io/gitea/services/context" |
||||
) |
||||
|
||||
const ( |
||||
tplRepoVariables templates.TplName = "repo/settings/actions" |
||||
tplOrgVariables templates.TplName = "org/settings/actions" |
||||
tplUserVariables templates.TplName = "user/settings/actions" |
||||
tplAdminVariables templates.TplName = "admin/actions" |
||||
) |
||||
|
||||
type variablesCtx struct { |
||||
OwnerID int64 |
||||
RepoID int64 |
||||
IsRepo bool |
||||
IsOrg bool |
||||
IsUser bool |
||||
IsGlobal bool |
||||
VariablesTemplate templates.TplName |
||||
RedirectLink string |
||||
} |
||||
|
||||
func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) { |
||||
if ctx.Data["PageIsRepoSettings"] == true { |
||||
return &variablesCtx{ |
||||
OwnerID: 0, |
||||
RepoID: ctx.Repo.Repository.ID, |
||||
IsRepo: true, |
||||
VariablesTemplate: tplRepoVariables, |
||||
RedirectLink: ctx.Repo.RepoLink + "/settings/actions/variables", |
||||
}, nil |
||||
} |
||||
|
||||
if ctx.Data["PageIsOrgSettings"] == true { |
||||
err := shared_user.LoadHeaderCount(ctx) |
||||
if err != nil { |
||||
ctx.ServerError("LoadHeaderCount", err) |
||||
return nil, nil |
||||
} |
||||
return &variablesCtx{ |
||||
OwnerID: ctx.ContextUser.ID, |
||||
RepoID: 0, |
||||
IsOrg: true, |
||||
VariablesTemplate: tplOrgVariables, |
||||
RedirectLink: ctx.Org.OrgLink + "/settings/actions/variables", |
||||
}, nil |
||||
} |
||||
|
||||
if ctx.Data["PageIsUserSettings"] == true { |
||||
return &variablesCtx{ |
||||
OwnerID: ctx.Doer.ID, |
||||
RepoID: 0, |
||||
IsUser: true, |
||||
VariablesTemplate: tplUserVariables, |
||||
RedirectLink: setting.AppSubURL + "/user/settings/actions/variables", |
||||
}, nil |
||||
} |
||||
|
||||
if ctx.Data["PageIsAdmin"] == true { |
||||
return &variablesCtx{ |
||||
OwnerID: 0, |
||||
RepoID: 0, |
||||
IsGlobal: true, |
||||
VariablesTemplate: tplAdminVariables, |
||||
RedirectLink: setting.AppSubURL + "/-/admin/actions/variables", |
||||
}, nil |
||||
} |
||||
|
||||
return nil, errors.New("unable to set Variables context") |
||||
} |
||||
|
||||
func Variables(ctx *context.Context) { |
||||
ctx.Data["Title"] = ctx.Tr("actions.variables") |
||||
ctx.Data["PageType"] = "variables" |
||||
ctx.Data["PageIsSharedSettingsVariables"] = true |
||||
|
||||
vCtx, err := getVariablesCtx(ctx) |
||||
if err != nil { |
||||
ctx.ServerError("getVariablesCtx", err) |
||||
return |
||||
} |
||||
|
||||
shared.SetVariablesContext(ctx, vCtx.OwnerID, vCtx.RepoID) |
||||
if ctx.Written() { |
||||
return |
||||
} |
||||
|
||||
ctx.HTML(http.StatusOK, vCtx.VariablesTemplate) |
||||
} |
||||
|
||||
func VariableCreate(ctx *context.Context) { |
||||
vCtx, err := getVariablesCtx(ctx) |
||||
if err != nil { |
||||
ctx.ServerError("getVariablesCtx", err) |
||||
return |
||||
} |
||||
|
||||
if ctx.HasError() { // form binding validation error
|
||||
ctx.JSONError(ctx.GetErrMsg()) |
||||
return |
||||
} |
||||
|
||||
shared.CreateVariable(ctx, vCtx.OwnerID, vCtx.RepoID, vCtx.RedirectLink) |
||||
} |
||||
|
||||
func VariableUpdate(ctx *context.Context) { |
||||
vCtx, err := getVariablesCtx(ctx) |
||||
if err != nil { |
||||
ctx.ServerError("getVariablesCtx", err) |
||||
return |
||||
} |
||||
|
||||
if ctx.HasError() { // form binding validation error
|
||||
ctx.JSONError(ctx.GetErrMsg()) |
||||
return |
||||
} |
||||
|
||||
shared.UpdateVariable(ctx, vCtx.RedirectLink) |
||||
} |
||||
|
||||
func VariableDelete(ctx *context.Context) { |
||||
vCtx, err := getVariablesCtx(ctx) |
||||
if err != nil { |
||||
ctx.ServerError("getVariablesCtx", err) |
||||
return |
||||
} |
||||
shared.DeleteVariable(ctx, vCtx.RedirectLink) |
||||
} |
@ -0,0 +1,149 @@ |
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"net/http" |
||||
"testing" |
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions" |
||||
"code.gitea.io/gitea/models/db" |
||||
repo_model "code.gitea.io/gitea/models/repo" |
||||
"code.gitea.io/gitea/models/unittest" |
||||
user_model "code.gitea.io/gitea/models/user" |
||||
"code.gitea.io/gitea/tests" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestActionsVariables(t *testing.T) { |
||||
defer tests.PrepareTestEnv(t)() |
||||
|
||||
ctx := context.Background() |
||||
|
||||
require.NoError(t, db.DeleteAllRecords("action_variable")) |
||||
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
||||
_, _ = actions_model.InsertVariable(ctx, user2.ID, 0, "VAR", "user2-var") |
||||
user2Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{OwnerID: user2.ID, Name: "VAR"}) |
||||
userWebURL := "/user/settings/actions/variables" |
||||
|
||||
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3, Type: user_model.UserTypeOrganization}) |
||||
_, _ = actions_model.InsertVariable(ctx, org3.ID, 0, "VAR", "org3-var") |
||||
org3Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{OwnerID: org3.ID, Name: "VAR"}) |
||||
orgWebURL := "/org/org3/settings/actions/variables" |
||||
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
||||
_, _ = actions_model.InsertVariable(ctx, 0, repo1.ID, "VAR", "repo1-var") |
||||
repo1Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{RepoID: repo1.ID, Name: "VAR"}) |
||||
repoWebURL := "/user2/repo1/settings/actions/variables" |
||||
|
||||
_, _ = actions_model.InsertVariable(ctx, 0, 0, "VAR", "global-var") |
||||
globalVar := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{Name: "VAR", Data: "global-var"}) |
||||
adminWebURL := "/-/admin/actions/variables" |
||||
|
||||
sessionAdmin := loginUser(t, "user1") |
||||
sessionUser2 := loginUser(t, user2.Name) |
||||
|
||||
doUpdate := func(t *testing.T, sess *TestSession, baseURL string, id int64, data string, expectedStatus int) { |
||||
req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d/edit", baseURL, id), map[string]string{ |
||||
"_csrf": GetUserCSRFToken(t, sess), |
||||
"name": "VAR", |
||||
"data": data, |
||||
}) |
||||
sess.MakeRequest(t, req, expectedStatus) |
||||
} |
||||
|
||||
doDelete := func(t *testing.T, sess *TestSession, baseURL string, id int64, expectedStatus int) { |
||||
req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d/delete", baseURL, id), map[string]string{ |
||||
"_csrf": GetUserCSRFToken(t, sess), |
||||
}) |
||||
sess.MakeRequest(t, req, expectedStatus) |
||||
} |
||||
|
||||
assertDenied := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
||||
doUpdate(t, sess, baseURL, id, "ChangedData", http.StatusNotFound) |
||||
doDelete(t, sess, baseURL, id, http.StatusNotFound) |
||||
v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{ID: id}) |
||||
assert.Contains(t, v.Data, "-var") |
||||
} |
||||
|
||||
assertSuccess := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
||||
doUpdate(t, sess, baseURL, id, "ChangedData", http.StatusOK) |
||||
v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{ID: id}) |
||||
assert.Equal(t, "ChangedData", v.Data) |
||||
doDelete(t, sess, baseURL, id, http.StatusOK) |
||||
unittest.AssertNotExistsBean(t, &actions_model.ActionVariable{ID: id}) |
||||
} |
||||
|
||||
t.Run("UpdateUserVar", func(t *testing.T) { |
||||
theVar := user2Var |
||||
t.Run("FromOrg", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromRepo", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromAdmin", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("UpdateOrgVar", func(t *testing.T) { |
||||
theVar := org3Var |
||||
t.Run("FromRepo", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromUser", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromAdmin", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("UpdateRepoVar", func(t *testing.T) { |
||||
theVar := repo1Var |
||||
t.Run("FromOrg", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromUser", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromAdmin", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("UpdateGlobalVar", func(t *testing.T) { |
||||
theVar := globalVar |
||||
t.Run("FromOrg", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromUser", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
||||
}) |
||||
t.Run("FromRepo", func(t *testing.T) { |
||||
assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("UpdateSuccess", func(t *testing.T) { |
||||
t.Run("User", func(t *testing.T) { |
||||
assertSuccess(t, sessionUser2, userWebURL, user2Var.ID) |
||||
}) |
||||
t.Run("Org", func(t *testing.T) { |
||||
assertSuccess(t, sessionAdmin, orgWebURL, org3Var.ID) |
||||
}) |
||||
t.Run("Repo", func(t *testing.T) { |
||||
assertSuccess(t, sessionUser2, repoWebURL, repo1Var.ID) |
||||
}) |
||||
t.Run("Admin", func(t *testing.T) { |
||||
assertSuccess(t, sessionAdmin, adminWebURL, globalVar.ID) |
||||
}) |
||||
}) |
||||
} |
Loading…
Reference in new issue