From db622afd87f8f6b1d0ca4228b9eac7095cda3a58 Mon Sep 17 00:00:00 2001 From: Andreas Ahlenstorf Date: Fri, 24 Apr 2026 04:36:42 +0200 Subject: [PATCH] refactor: delegate to service for run cancellation (#12142) ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Tests for JavaScript changes (can be removed for Go changes) - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12142 Reviewed-by: Mathieu Fenniak --- routers/web/repo/actions/view.go | 41 +++++--------------------------- services/actions/run.go | 2 +- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index 31b757d2b3..9769f0f11a 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -5,7 +5,6 @@ package actions import ( - "context" "errors" "fmt" "html/template" @@ -26,14 +25,11 @@ import ( "forgejo.org/modules/json" "forgejo.org/modules/log" "forgejo.org/modules/templates" - "forgejo.org/modules/timeutil" "forgejo.org/modules/translation" "forgejo.org/modules/util" "forgejo.org/modules/web" actions_service "forgejo.org/services/actions" app_context "forgejo.org/services/context" - - "xorm.io/builder" ) func RedirectToLatestAttempt(ctx *app_context.Context) { @@ -591,40 +587,15 @@ func Logs(ctx *app_context.Context) { func Cancel(ctx *app_context.Context) { runIndex := ctx.ParamsInt64("run") - _, jobs := getRunJobs(ctx, runIndex, -1) - if ctx.Written() { - return - } - - if err := db.WithTx(ctx, func(ctx context.Context) error { - for _, job := range jobs { - status := job.Status - if status.IsDone() { - continue - } - if job.TaskID == 0 { - job.Status = actions_model.StatusCancelled - job.Stopped = timeutil.TimeStampNow() - n, err := actions_service.UpdateRunJob(ctx, job, builder.Eq{"task_id": 0}, "status", "stopped") - if err != nil { - return err - } - if n == 0 { - return errors.New("job has changed, try again") - } - continue - } - if err := actions_service.StopTask(ctx, job.TaskID, actions_model.StatusCancelled); err != nil { - return err - } - } - return nil - }); err != nil { + run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) + if err != nil { + ctx.Error(http.StatusInternalServerError, err.Error()) + return + } + if err := actions_service.CancelRun(ctx, run); err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) return } - - actions_service.CreateCommitStatus(ctx, jobs...) ctx.JSON(http.StatusOK, struct{}{}) } diff --git a/services/actions/run.go b/services/actions/run.go index bee0b1581d..891d9d8f03 100644 --- a/services/actions/run.go +++ b/services/actions/run.go @@ -27,7 +27,7 @@ func killRun(ctx context.Context, run *actions_model.ActionRun, newStatus action if job.TaskID == 0 { job.Status = newStatus job.Stopped = timeutil.TimeStampNow() - _, err := actions_model.UpdateRunJobWithoutNotification(ctx, job, nil, "status", "stopped") + _, err := UpdateRunJob(ctx, job, nil, "status", "stopped") if err != nil { return err }