@@ -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
123138func 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
175198func 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}
0 commit comments