feat(api): add new /repos/{owner}/{repo}/actions/runs/{run_id}/cancel API endpoint (#12957)

This new API endpoint makes it possible to cancel action runs via the API. Previously this was only natively possible through the UI, the same `CancelRun` func has been reused for this feature.

### Tests for Go changes

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/12957): <!--number 12957 --><!--line 0 --><!--description ZmVhdChhcGkpOiBhZGQgbmV3IGAvcmVwb3Mve293bmVyfS97cmVwb30vYWN0aW9ucy9ydW5zL3tydW5faWR9L2NhbmNlbGAgQVBJIGVuZHBvaW50-->feat(api): add new `/repos/{owner}/{repo}/actions/runs/{run_id}/cancel` API endpoint<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12957
Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org>
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
This commit is contained in:
OFHansen
2026-06-09 04:00:56 +02:00
committed by Mathieu Fenniak
parent 3dc2b52b5f
commit bb5919ea87
7 changed files with 251 additions and 0 deletions
+1
View File
@@ -1256,6 +1256,7 @@ func Routes() *web.Route {
m.Get("", repo.ListActionRuns)
m.Get("/{run_id}", repo.GetActionRun)
m.Delete("/{run_id}", reqToken(), reqAdmin(unit.TypeActions), repo.DeleteActionRun)
m.Post("/{run_id}/cancel", reqToken(), reqRepoWriter(unit.TypeActions), repo.CancelActionRun)
m.Get("/{run_id}/jobs", repo.ListActionRunJobs)
m.Get("/{run_id}/logs", repo.GetActionRunLogs)
m.Get("/{run_id}/artifacts", repo.ListActionRunArtifacts)
+61
View File
@@ -1095,6 +1095,67 @@ func DeleteActionRun(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent)
}
// CancelActionRun cancels a pending or running workflow run.
func CancelActionRun(ctx *context.APIContext) {
// swagger:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel repository CancelActionRun
// ---
// summary: Cancel a pending or running workflow run.
// description: >
// Cancel a particular workflow run. Pending or running jobs of the run are cancelled. A run that has
// already finished, whether cancelled, failed, skipped or succeeded, is left unchanged.
// In both cases the endpoint responds with HTTP 204.
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: run_id
// in: path
// description: ID of the workflow run
// type: integer
// format: int64
// required: true
// responses:
// "204":
// description: Workflow run has been cancelled
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
run, err := actions_model.GetRunByID(ctx, ctx.ParamsInt64(":run_id"))
if err != nil {
if errors.Is(err, util.ErrNotExist) {
ctx.Error(http.StatusNotFound, "GetRunById", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetRunByID", err)
return
}
if ctx.Repo.Repository.ID != run.RepoID {
ctx.Error(http.StatusNotFound, "GetRunById", util.ErrNotExist)
return
}
err = actions_service.CancelRun(ctx, run)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CancelRun", err)
return
}
ctx.Status(http.StatusNoContent)
}
// ListActionRunJobs return a filtered list of jobs that belong to a single workflow run
func ListActionRunJobs(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs repository ListActionRunJobs