Skip to content

Commit

Permalink
Enhance routers for the Actions variable operations (#33547)
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
3 people authored Feb 10, 2025
1 parent e3adb68 commit c422f17
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 174 deletions.
22 changes: 16 additions & 6 deletions models/actions/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,23 @@ func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data strin

type FindVariablesOpts struct {
db.ListOptions
IDs []int64
RepoID int64
OwnerID int64 // it will be ignored if RepoID is set
Name string
}

func (opts FindVariablesOpts) ToConds() builder.Cond {
cond := builder.NewCond()

if len(opts.IDs) > 0 {
if len(opts.IDs) == 1 {
cond = cond.And(builder.Eq{"id": opts.IDs[0]})
} else {
cond = cond.And(builder.In("id", opts.IDs))
}
}

// Since we now support instance-level variables,
// there is no need to check for null values for `owner_id` and `repo_id`
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
Expand All @@ -85,12 +95,12 @@ func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariab
return db.Find[ActionVariable](ctx, opts)
}

func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error) {
count, err := db.GetEngine(ctx).ID(variable.ID).Cols("name", "data").
Update(&ActionVariable{
Name: variable.Name,
Data: variable.Data,
})
func UpdateVariableCols(ctx context.Context, variable *ActionVariable, cols ...string) (bool, error) {
variable.Name = strings.ToUpper(variable.Name)
count, err := db.GetEngine(ctx).
ID(variable.ID).
Cols(cols...).
Update(variable)
return count != 0, err
}

Expand Down
6 changes: 5 additions & 1 deletion routers/api/v1/org/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,11 @@ func (Action) UpdateVariable(ctx *context.APIContext) {
if opt.Name == "" {
opt.Name = ctx.PathParam("variablename")
}
if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {

v.Name = opt.Name
v.Data = opt.Value

if _, err := actions_service.UpdateVariableNameData(ctx, v); err != nil {
if errors.Is(err, util.ErrInvalidArgument) {
ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
} else {
Expand Down
6 changes: 5 additions & 1 deletion routers/api/v1/repo/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ func (Action) UpdateVariable(ctx *context.APIContext) {
if opt.Name == "" {
opt.Name = ctx.PathParam("variablename")
}
if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {

v.Name = opt.Name
v.Data = opt.Value

if _, err := actions_service.UpdateVariableNameData(ctx, v); err != nil {
if errors.Is(err, util.ErrInvalidArgument) {
ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
} else {
Expand Down
6 changes: 5 additions & 1 deletion routers/api/v1/user/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ func UpdateVariable(ctx *context.APIContext) {
if opt.Name == "" {
opt.Name = ctx.PathParam("variablename")
}
if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {

v.Name = opt.Name
v.Data = opt.Value

if _, err := actions_service.UpdateVariableNameData(ctx, v); err != nil {
if errors.Is(err, util.ErrInvalidArgument) {
ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
} else {
Expand Down
140 changes: 0 additions & 140 deletions routers/web/repo/setting/variables.go

This file was deleted.

Loading

0 comments on commit c422f17

Please # to comment.