Skip to content

Commit 0b78368

Browse files
committed
fix: find before delete
1 parent 66565d8 commit 0b78368

File tree

2 files changed

+56
-66
lines changed

2 files changed

+56
-66
lines changed

routers/web/repo/setting/variables.go

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"net/http"
99

1010
actions_model "code.gitea.io/gitea/models/actions"
11+
"code.gitea.io/gitea/models/db"
1112
"code.gitea.io/gitea/modules/log"
1213
"code.gitea.io/gitea/modules/setting"
1314
"code.gitea.io/gitea/modules/templates"
1415
"code.gitea.io/gitea/modules/web"
15-
shared "code.gitea.io/gitea/routers/web/shared/actions"
1616
shared_user "code.gitea.io/gitea/routers/web/shared/user"
1717
actions_service "code.gitea.io/gitea/services/actions"
1818
"code.gitea.io/gitea/services/context"
@@ -97,10 +97,15 @@ func Variables(ctx *context.Context) {
9797
return
9898
}
9999

100-
shared.SetVariablesContext(ctx, vCtx.OwnerID, vCtx.RepoID)
101-
if ctx.Written() {
100+
variables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{
101+
OwnerID: vCtx.OwnerID,
102+
RepoID: vCtx.RepoID,
103+
})
104+
if err != nil {
105+
ctx.ServerError("FindVariables", err)
102106
return
103107
}
108+
ctx.Data["Variables"] = variables
104109

105110
ctx.HTML(http.StatusOK, vCtx.VariablesTemplate)
106111
}
@@ -117,7 +122,17 @@ func VariableCreate(ctx *context.Context) {
117122
return
118123
}
119124

120-
shared.CreateVariable(ctx, vCtx.OwnerID, vCtx.RepoID, vCtx.RedirectLink)
125+
form := web.GetForm(ctx).(*forms.EditVariableForm)
126+
127+
v, err := actions_service.CreateVariable(ctx, vCtx.OwnerID, vCtx.RepoID, form.Name, form.Data)
128+
if err != nil {
129+
log.Error("CreateVariable: %v", err)
130+
ctx.JSONError(ctx.Tr("actions.variables.creation.failed"))
131+
return
132+
}
133+
134+
ctx.Flash.Success(ctx.Tr("actions.variables.creation.success", v.Name))
135+
ctx.JSONRedirect(vCtx.RedirectLink)
121136
}
122137

123138
func VariableUpdate(ctx *context.Context) {
@@ -134,6 +149,25 @@ func VariableUpdate(ctx *context.Context) {
134149

135150
id := ctx.PathParamInt64("variable_id")
136151

152+
variable, ok := findVariable(ctx, id, vCtx)
153+
if !ok {
154+
return
155+
}
156+
157+
form := web.GetForm(ctx).(*forms.EditVariableForm)
158+
variable.Name = form.Name
159+
variable.Data = form.Data
160+
161+
if ok, err := actions_service.UpdateVariable(ctx, variable); err != nil || !ok {
162+
log.Error("UpdateVariable: %v", err)
163+
ctx.JSONError(ctx.Tr("actions.variables.update.failed"))
164+
return
165+
}
166+
ctx.Flash.Success(ctx.Tr("actions.variables.update.success"))
167+
ctx.JSONRedirect(vCtx.RedirectLink)
168+
}
169+
170+
func findVariable(ctx *context.Context, id int64, vCtx *variablesCtx) (*actions_model.ActionVariable, bool) {
137171
opts := actions_model.FindVariablesOpts{
138172
IDs: []int64{id},
139173
}
@@ -151,25 +185,14 @@ func VariableUpdate(ctx *context.Context) {
151185
var variable *actions_model.ActionVariable
152186
if got, err := actions_model.FindVariables(ctx, opts); err != nil {
153187
ctx.ServerError("FindVariables", err)
154-
return
188+
return nil, false
155189
} else if len(got) == 0 {
156190
ctx.NotFound("FindVariables", nil)
157-
return
191+
return nil, false
158192
} else {
159193
variable = got[0]
160194
}
161-
162-
form := web.GetForm(ctx).(*forms.EditVariableForm)
163-
variable.Name = form.Name
164-
variable.Data = form.Data
165-
166-
if ok, err := actions_service.UpdateVariable(ctx, variable); err != nil || !ok {
167-
log.Error("UpdateVariable: %v", err)
168-
ctx.JSONError(ctx.Tr("actions.variables.update.failed"))
169-
return
170-
}
171-
ctx.Flash.Success(ctx.Tr("actions.variables.update.success"))
172-
ctx.JSONRedirect(vCtx.RedirectLink)
195+
return variable, true
173196
}
174197

175198
func VariableDelete(ctx *context.Context) {
@@ -178,5 +201,19 @@ func VariableDelete(ctx *context.Context) {
178201
ctx.ServerError("getVariablesCtx", err)
179202
return
180203
}
181-
shared.DeleteVariable(ctx, vCtx.RedirectLink)
204+
205+
id := ctx.PathParamInt64("variable_id")
206+
207+
variable, ok := findVariable(ctx, id, vCtx)
208+
if !ok {
209+
return
210+
}
211+
212+
if err := actions_service.DeleteVariableByID(ctx, variable.ID); err != nil {
213+
log.Error("Delete variable [%d] failed: %v", id, err)
214+
ctx.JSONError(ctx.Tr("actions.variables.deletion.failed"))
215+
return
216+
}
217+
ctx.Flash.Success(ctx.Tr("actions.variables.deletion.success"))
218+
ctx.JSONRedirect(vCtx.RedirectLink)
182219
}

routers/web/shared/actions/variables.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,3 @@
33

44
package actions
55

6-
import (
7-
actions_model "code.gitea.io/gitea/models/actions"
8-
"code.gitea.io/gitea/models/db"
9-
"code.gitea.io/gitea/modules/log"
10-
"code.gitea.io/gitea/modules/web"
11-
actions_service "code.gitea.io/gitea/services/actions"
12-
"code.gitea.io/gitea/services/context"
13-
"code.gitea.io/gitea/services/forms"
14-
)
15-
16-
func SetVariablesContext(ctx *context.Context, ownerID, repoID int64) {
17-
variables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{
18-
OwnerID: ownerID,
19-
RepoID: repoID,
20-
})
21-
if err != nil {
22-
ctx.ServerError("FindVariables", err)
23-
return
24-
}
25-
ctx.Data["Variables"] = variables
26-
}
27-
28-
func CreateVariable(ctx *context.Context, ownerID, repoID int64, redirectURL string) {
29-
form := web.GetForm(ctx).(*forms.EditVariableForm)
30-
31-
v, err := actions_service.CreateVariable(ctx, ownerID, repoID, form.Name, form.Data)
32-
if err != nil {
33-
log.Error("CreateVariable: %v", err)
34-
ctx.JSONError(ctx.Tr("actions.variables.creation.failed"))
35-
return
36-
}
37-
38-
ctx.Flash.Success(ctx.Tr("actions.variables.creation.success", v.Name))
39-
ctx.JSONRedirect(redirectURL)
40-
}
41-
42-
func DeleteVariable(ctx *context.Context, redirectURL string) {
43-
id := ctx.PathParamInt64("variable_id")
44-
45-
if err := actions_service.DeleteVariableByID(ctx, id); err != nil {
46-
log.Error("Delete variable [%d] failed: %v", id, err)
47-
ctx.JSONError(ctx.Tr("actions.variables.deletion.failed"))
48-
return
49-
}
50-
ctx.Flash.Success(ctx.Tr("actions.variables.deletion.success"))
51-
ctx.JSONRedirect(redirectURL)
52-
}

0 commit comments

Comments
 (0)