refactor(actions): pass secret and variable scope as a struct (#39719)

Secret and variable functions took an adjacent `ownerID, repoID int64`
pair, so swapping the two compiled and only failed at runtime. They now
take a `BelongingScope` value instead.

`BelongingScope` also replaces the "drop OwnerID when RepoID is set"
step that `InsertEncryptedSecret` and `InsertVariable` each implemented
separately

No behaviour change. It also makes adding another scope dimension, such
as the environment ID in https://github.com/go-gitea/gitea/pull/38149, a
one-field change.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-10 18:21:28 +00:00
1 parent 23fb49fea7
commit 321d3ec104
13 files changed
+135 -163

No files matched your search

+14 -14
View File
@@ -49,8 +49,8 @@ func (Action) ListActionsSecrets(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
opts := &secret_model.FindSecretsOptions{
OwnerID: ctx.Org.Organization.ID,
ListOptions: utils.GetListOptions(ctx),
BelongingScope: actions_model.BelongingScopeOwner(ctx.Org.Organization.ID),
ListOptions: utils.GetListOptions(ctx),
}
secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts)
@@ -109,7 +109,7 @@ func (Action) CreateOrUpdateSecret(ctx *context.APIContext) {
opt := web.GetForm[*api.CreateOrUpdateSecretOption](ctx)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.PathParam("secretname"), opt.Data, opt.Description)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, actions_model.BelongingScopeOwner(ctx.Org.Organization.ID), ctx.PathParam("secretname"), opt.Data, opt.Description)
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -156,7 +156,7 @@ func (Action) DeleteSecret(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
s, err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.PathParam("secretname"))
s, err := secret_service.DeleteSecretByName(ctx, actions_model.BelongingScopeOwner(ctx.Org.Organization.ID), ctx.PathParam("secretname"))
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -219,8 +219,8 @@ func (Action) ListVariables(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
OwnerID: ctx.Org.Organization.ID,
ListOptions: listOptions,
BelongingScope: actions_model.BelongingScopeOwner(ctx.Org.Organization.ID),
ListOptions: listOptions,
})
if err != nil {
ctx.APIErrorInternal(err)
@@ -269,8 +269,8 @@ func (Action) GetVariable(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ctx.Org.Organization.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeOwner(ctx.Org.Organization.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
@@ -318,7 +318,7 @@ func (Action) DeleteVariable(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
if err := actions_service.DeleteVariableByName(ctx, ctx.Org.Organization.ID, 0, ctx.PathParam("variablename")); err != nil {
if err := actions_service.DeleteVariableByName(ctx, actions_model.BelongingScopeOwner(ctx.Org.Organization.ID), ctx.PathParam("variablename")); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -366,8 +366,8 @@ func (Action) CreateVariable(ctx *context.APIContext) {
variableName := ctx.PathParam("variablename")
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ownerID,
Name: variableName,
BelongingScope: actions_model.BelongingScopeOwner(ownerID),
Name: variableName,
})
if err != nil && !errors.Is(err, util.ErrNotExist) {
ctx.APIErrorInternal(err)
@@ -378,7 +378,7 @@ func (Action) CreateVariable(ctx *context.APIContext) {
return
}
if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value, opt.Description); err != nil {
if _, err := actions_service.CreateVariable(ctx, actions_model.BelongingScopeOwner(ownerID), variableName, opt.Value, opt.Description); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -423,8 +423,8 @@ func (Action) UpdateVariable(ctx *context.APIContext) {
opt := web.GetForm[*api.UpdateVariableOption](ctx)
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ctx.Org.Organization.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeOwner(ctx.Org.Organization.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
+14 -14
View File
@@ -72,8 +72,8 @@ func (Action) ListActionsSecrets(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
opts := &secret_model.FindSecretsOptions{
RepoID: repo.ID,
ListOptions: listOptions,
BelongingScope: actions_model.BelongingScopeRepo(repo.ID),
ListOptions: listOptions,
}
secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts)
@@ -138,7 +138,7 @@ func (Action) CreateOrUpdateSecret(ctx *context.APIContext) {
opt := web.GetForm[*api.CreateOrUpdateSecretOption](ctx)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, 0, repo.ID, ctx.PathParam("secretname"), opt.Data, opt.Description)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, actions_model.BelongingScopeRepo(repo.ID), ctx.PathParam("secretname"), opt.Data, opt.Description)
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -192,7 +192,7 @@ func (Action) DeleteSecret(ctx *context.APIContext) {
repo := ctx.Repo.Repository
s, err := secret_service.DeleteSecretByName(ctx, 0, repo.ID, ctx.PathParam("secretname"))
s, err := secret_service.DeleteSecretByName(ctx, actions_model.BelongingScopeRepo(repo.ID), ctx.PathParam("secretname"))
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -234,8 +234,8 @@ func (Action) GetVariable(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
RepoID: ctx.Repo.Repository.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeRepo(ctx.Repo.Repository.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
@@ -288,7 +288,7 @@ func (Action) DeleteVariable(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
if err := actions_service.DeleteVariableByName(ctx, 0, ctx.Repo.Repository.ID, ctx.PathParam("variablename")); err != nil {
if err := actions_service.DeleteVariableByName(ctx, actions_model.BelongingScopeRepo(ctx.Repo.Repository.ID), ctx.PathParam("variablename")); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -339,8 +339,8 @@ func (Action) CreateVariable(ctx *context.APIContext) {
variableName := ctx.PathParam("variablename")
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
RepoID: repoID,
Name: variableName,
BelongingScope: actions_model.BelongingScopeRepo(repoID),
Name: variableName,
})
if err != nil && !errors.Is(err, util.ErrNotExist) {
ctx.APIErrorInternal(err)
@@ -351,7 +351,7 @@ func (Action) CreateVariable(ctx *context.APIContext) {
return
}
if _, err := actions_service.CreateVariable(ctx, 0, repoID, variableName, opt.Value, opt.Description); err != nil {
if _, err := actions_service.CreateVariable(ctx, actions_model.BelongingScopeRepo(repoID), variableName, opt.Value, opt.Description); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -399,8 +399,8 @@ func (Action) UpdateVariable(ctx *context.APIContext) {
opt := web.GetForm[*api.UpdateVariableOption](ctx)
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
RepoID: ctx.Repo.Repository.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeRepo(ctx.Repo.Repository.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
@@ -460,8 +460,8 @@ func (Action) ListVariables(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
RepoID: ctx.Repo.Repository.ID,
ListOptions: listOptions,
BelongingScope: actions_model.BelongingScopeRepo(ctx.Repo.Repository.ID),
ListOptions: listOptions,
})
if err != nil {
ctx.APIErrorInternal(err)
+12 -12
View File
@@ -51,7 +51,7 @@ func CreateOrUpdateSecret(ctx *context.APIContext) {
opt := web.GetForm[*api.CreateOrUpdateSecretOption](ctx)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, ctx.PathParam("secretname"), opt.Data, opt.Description)
s, created, err := secret_service.CreateOrUpdateSecret(ctx, actions_model.BelongingScopeOwner(ctx.Doer.ID), ctx.PathParam("secretname"), opt.Data, opt.Description)
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -93,7 +93,7 @@ func DeleteSecret(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
s, err := secret_service.DeleteSecretByName(ctx, ctx.Doer.ID, 0, ctx.PathParam("secretname"))
s, err := secret_service.DeleteSecretByName(ctx, actions_model.BelongingScopeOwner(ctx.Doer.ID), ctx.PathParam("secretname"))
if err != nil {
ctx.APIErrorAuto(err)
return
@@ -137,8 +137,8 @@ func CreateVariable(ctx *context.APIContext) {
variableName := ctx.PathParam("variablename")
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ownerID,
Name: variableName,
BelongingScope: actions_model.BelongingScopeOwner(ownerID),
Name: variableName,
})
if err != nil && !errors.Is(err, util.ErrNotExist) {
ctx.APIErrorInternal(err)
@@ -149,7 +149,7 @@ func CreateVariable(ctx *context.APIContext) {
return
}
if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value, opt.Description); err != nil {
if _, err := actions_service.CreateVariable(ctx, actions_model.BelongingScopeOwner(ownerID), variableName, opt.Value, opt.Description); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -189,8 +189,8 @@ func UpdateVariable(ctx *context.APIContext) {
opt := web.GetForm[*api.UpdateVariableOption](ctx)
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ctx.Doer.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeOwner(ctx.Doer.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
@@ -236,7 +236,7 @@ func DeleteVariable(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
if err := actions_service.DeleteVariableByName(ctx, ctx.Doer.ID, 0, ctx.PathParam("variablename")); err != nil {
if err := actions_service.DeleteVariableByName(ctx, actions_model.BelongingScopeOwner(ctx.Doer.ID), ctx.PathParam("variablename")); err != nil {
ctx.APIErrorAuto(err)
return
}
@@ -266,8 +266,8 @@ func GetVariable(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
OwnerID: ctx.Doer.ID,
Name: ctx.PathParam("variablename"),
BelongingScope: actions_model.BelongingScopeOwner(ctx.Doer.ID),
Name: ctx.PathParam("variablename"),
})
if err != nil {
ctx.APIErrorAuto(err)
@@ -310,8 +310,8 @@ func ListVariables(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
listOptions := utils.GetListOptions(ctx)
vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
OwnerID: ctx.Doer.ID,
ListOptions: listOptions,
BelongingScope: actions_model.BelongingScopeOwner(ctx.Doer.ID),
ListOptions: listOptions,
})
if err != nil {
ctx.APIErrorInternal(err)