diff --git a/models/bots/runner.go b/models/bots/runner.go index f2824b1dc9..37f42e9d3f 100644 --- a/models/bots/runner.go +++ b/models/bots/runner.go @@ -97,10 +97,11 @@ func init() { type FindRunnerOptions struct { db.ListOptions - RepoID int64 - OwnerID int64 - Sort string - Filter string + RepoID int64 + OwnerID int64 + Sort string + Filter string + WithDeleted bool } func (opts FindRunnerOptions) toCond() builder.Cond { @@ -115,6 +116,9 @@ func (opts FindRunnerOptions) toCond() builder.Cond { if opts.Filter != "" { cond = cond.And(builder.Like{"name", opts.Filter}) } + if !opts.WithDeleted { + cond = cond.And(builder.IsNull{"deleted"}) + } return cond } @@ -208,9 +212,9 @@ func GetRunnerByToken(token string) (*Runner, error) { } // UpdateRunner updates runner's information. -func UpdateRunner(ctx context.Context, r *Runner, cols ...string) (err error) { +func UpdateRunner(ctx context.Context, r *Runner, cols ...string) error { e := db.GetEngine(ctx) - + var err error if len(cols) == 0 { _, err = e.ID(r.ID).AllCols().Update(r) } else { @@ -219,6 +223,12 @@ func UpdateRunner(ctx context.Context, r *Runner, cols ...string) (err error) { return err } +func DeleteRunner(ctx context.Context, r *Runner) error { + e := db.GetEngine(ctx) + _, err := e.Delete(r) + return err +} + // FindRunnersByRepoID returns all workers for the repository func FindRunnersByRepoID(repoID int64) ([]*Runner, error) { var runners []*Runner diff --git a/routers/api/bots/runner/unary.go b/routers/api/bots/runner/unary.go index f644a90fe1..70e8de98b2 100644 --- a/routers/api/bots/runner/unary.go +++ b/routers/api/bots/runner/unary.go @@ -28,7 +28,7 @@ var WithRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar token := request.Header().Get("X-Runner-Token") // TODO: shouldn't be X-Runner-Token, maybe X-Runner-UUID runner, err := bots_model.GetRunnerByToken(token) if err != nil { - if _, ok := err.(*bots_model.ErrRunnerNotExist); ok { + if _, ok := err.(bots_model.ErrRunnerNotExist); ok { return nil, status.Error(codes.Unauthenticated, "unregistered runner") } return nil, status.Error(codes.Internal, err.Error()) diff --git a/routers/web/admin/runners.go b/routers/web/admin/runners.go index c44a425973..cf7bddd291 100644 --- a/routers/web/admin/runners.go +++ b/routers/web/admin/runners.go @@ -42,8 +42,9 @@ func Runners(ctx *context.Context) { Page: page, PageSize: 100, }, - Sort: ctx.Req.URL.Query().Get("sort"), - Filter: ctx.Req.URL.Query().Get("q"), + Sort: ctx.Req.URL.Query().Get("sort"), + Filter: ctx.Req.URL.Query().Get("q"), + WithDeleted: false, } count, err := bots_model.CountRunners(opts) @@ -122,11 +123,26 @@ func EditRunnerPost(ctx *context.Context) { } // DeleteRunner response for deleting a runner -func DeleteRunner(ctx *context.Context) { +func DeleteRunnerPost(ctx *context.Context) { + runner, err := bots_model.GetRunnerByID(ctx.ParamsInt64(":runnerid")) + if err != nil { + log.Warn("DeleteRunnerPost.GetRunnerByID failed: %v, url: %s", err, ctx.Req.URL) + ctx.ServerError("DeleteRunnerPost.GetRunnerByID", err) + return + } + + err = bots_model.DeleteRunner(ctx, runner) + if err != nil { + log.Warn("DeleteRunnerPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL) + ctx.Flash.Warning(ctx.Tr("admin.runners.delete_failed")) + ctx.Redirect(setting.AppSubURL + "/admin/runners/" + url.PathEscape(ctx.Params(":runnerid"))) + return + } + + log.Info("DeleteRunnerPost success: %s", ctx.Req.URL) + ctx.Flash.Success(ctx.Tr("admin.runners.deletion_success")) - ctx.JSON(http.StatusOK, map[string]interface{}{ - "redirect": setting.AppSubURL + "/admin/runners", - }) + ctx.Redirect(setting.AppSubURL + "/admin/runners/") } /** diff --git a/routers/web/web.go b/routers/web/web.go index ecd8e856f6..08252e3c15 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -629,7 +629,7 @@ func RegisterRoutes(m *web.Route) { m.Group("/runners", func() { m.Get("", admin.Runners) m.Combo("/{runnerid}").Get(admin.EditRunner).Post(bindIgnErr(forms.AdminEditRunnerForm{}), admin.EditRunnerPost) - m.Post("/{runnerid}/delete", admin.DeleteRunner) + m.Post("/{runnerid}/delete", admin.DeleteRunnerPost) }) }, func(ctx *context.Context) { ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable diff --git a/templates/admin/runner/edit.tmpl b/templates/admin/runner/edit.tmpl index b12b53101c..023114185d 100644 --- a/templates/admin/runner/edit.tmpl +++ b/templates/admin/runner/edit.tmpl @@ -48,9 +48,9 @@
- -
- {{.locale.Tr "admin.runners.delete_runner"}}
+ +
@@ -64,14 +64,22 @@ -