Skip to content

Commit 1378cd8

Browse files
committed
fix: RunnerDeletePost
1 parent 704b65e commit 1378cd8

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

models/actions/runner.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func init() {
167167

168168
type FindRunnerOptions struct {
169169
db.ListOptions
170+
IDs []int64
170171
RepoID int64
171172
OwnerID int64 // it will be ignored if RepoID is set
172173
Sort string
@@ -178,6 +179,14 @@ type FindRunnerOptions struct {
178179
func (opts FindRunnerOptions) ToConds() builder.Cond {
179180
cond := builder.NewCond()
180181

182+
if len(opts.IDs) > 0 {
183+
if len(opts.IDs) == 1 {
184+
cond = cond.And(builder.Eq{"id": opts.IDs[0]})
185+
} else {
186+
cond = cond.And(builder.In("id", opts.IDs))
187+
}
188+
}
189+
181190
if opts.RepoID > 0 {
182191
c := builder.NewCond().And(builder.Eq{"repo_id": opts.RepoID})
183192
if opts.WithAvailable {

routers/web/repo/setting/runners.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
actions_model "code.gitea.io/gitea/models/actions"
1212
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/templates"
1516
actions_shared "code.gitea.io/gitea/routers/web/shared/actions"
@@ -179,9 +180,57 @@ func RunnerDeletePost(ctx *context.Context) {
179180
ctx.ServerError("getRunnersCtx", err)
180181
return
181182
}
182-
actions_shared.RunnerDeletePost(ctx, ctx.PathParamInt64("runnerid"), rCtx.RedirectLink, rCtx.RedirectLink+url.PathEscape(ctx.PathParam("runnerid")))
183+
184+
runner, ok := findRunner(ctx, rCtx)
185+
if !ok {
186+
return
187+
}
188+
189+
successRedirectTo := rCtx.RedirectLink
190+
failedRedirectTo := rCtx.RedirectLink + url.PathEscape(ctx.PathParam("runnerid"))
191+
192+
if err := actions_model.DeleteRunner(ctx, runner.ID); err != nil {
193+
log.Warn("DeleteRunnerPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL)
194+
ctx.Flash.Warning(ctx.Tr("actions.runners.delete_runner_failed"))
195+
196+
ctx.JSONRedirect(failedRedirectTo)
197+
return
198+
}
199+
200+
log.Info("DeleteRunnerPost success: %s", ctx.Req.URL)
201+
202+
ctx.Flash.Success(ctx.Tr("actions.runners.delete_runner_success"))
203+
204+
ctx.JSONRedirect(successRedirectTo)
183205
}
184206

185207
func RedirectToDefaultSetting(ctx *context.Context) {
186208
ctx.Redirect(ctx.Repo.RepoLink + "/settings/actions/runners")
187209
}
210+
211+
func findRunner(ctx *context.Context, rCtx *runnersCtx) (*actions_model.ActionRunner, bool) {
212+
runnerID := ctx.PathParamInt64("runnerid")
213+
opts := &actions_model.FindRunnerOptions{
214+
IDs: []int64{runnerID},
215+
}
216+
switch {
217+
case rCtx.IsRepo:
218+
opts.RepoID = rCtx.RepoID
219+
case rCtx.IsOrg, rCtx.IsUser:
220+
opts.OwnerID = rCtx.OwnerID
221+
case rCtx.IsAdmin:
222+
// do nothing
223+
}
224+
225+
var runner *actions_model.ActionRunner
226+
if got, err := db.Find[actions_model.ActionRunner](ctx, opts); err != nil {
227+
ctx.ServerError("FindRunner", err)
228+
return nil, false
229+
} else if len(got) == 0 {
230+
ctx.NotFound("FindRunner", errors.New("runner not found"))
231+
return nil, false
232+
} else {
233+
runner = got[0]
234+
}
235+
return runner, true
236+
}

routers/web/shared/actions/runners.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,3 @@ func RunnerResetRegistrationToken(ctx *context.Context, ownerID, repoID int64, r
139139
ctx.Flash.Success(ctx.Tr("actions.runners.reset_registration_token_success"))
140140
ctx.JSONRedirect(redirectTo)
141141
}
142-
143-
// RunnerDeletePost response for deleting a runner
144-
func RunnerDeletePost(ctx *context.Context, runnerID int64,
145-
successRedirectTo, failedRedirectTo string,
146-
) {
147-
if err := actions_model.DeleteRunner(ctx, runnerID); err != nil {
148-
log.Warn("DeleteRunnerPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL)
149-
ctx.Flash.Warning(ctx.Tr("actions.runners.delete_runner_failed"))
150-
151-
ctx.JSONRedirect(failedRedirectTo)
152-
return
153-
}
154-
155-
log.Info("DeleteRunnerPost success: %s", ctx.Req.URL)
156-
157-
ctx.Flash.Success(ctx.Tr("actions.runners.delete_runner_success"))
158-
159-
ctx.JSONRedirect(successRedirectTo)
160-
}

0 commit comments

Comments
 (0)