feat: make it possible to remove workflow runs (#12478)

Add the ability to remove workflow runs, either using the UI or the HTTP API. Workflow runs can only be removed once a workflow run has completed. For security reasons, only a repository administrator or a token with `write:repository` permissions can remove runs.

Resolves https://codeberg.org/forgejo/forgejo/issues/2184.

## 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...
  - [x] 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

### 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

- [x] 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.
- [ ] 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/<pull request number>.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.

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

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/12478): <!--number 12478 --><!--line 0 --><!--description bWFrZSBpdCBwb3NzaWJsZSB0byByZW1vdmUgd29ya2Zsb3cgcnVucw==-->make it possible to remove workflow runs<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12478
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
This commit is contained in:
Andreas Ahlenstorf 2026-05-11 16:02:36 +02:00 committed by Mathieu Fenniak
parent 2d5dd62cf3
commit 03312e4f46
60 changed files with 1221 additions and 6 deletions

View file

@ -222,6 +222,15 @@ func SetArtifactDeleted(ctx context.Context, artifactID int64) error {
return err
}
// SetArtifactsOfRunDeleted marks all artifacts of the given run as deleted.
func SetArtifactsOfRunDeleted(ctx context.Context, runID int64) error {
_, err := db.GetEngine(ctx).
Where("run_id=?", runID).
Cols("status").
Update(&ActionArtifact{Status: int64(ArtifactStatusPendingDeletion)})
return err
}
// aggregatedArtifactConds returns the common WHERE clause used by aggregated
// artifact queries: restrict to visible statuses and apply the caller's filters.
// The Status field on opts is ignored — visibility is fixed to UploadConfirmed/Expired.

View file

@ -612,4 +612,11 @@ func ComputeRunStatus(ctx context.Context, runID int64) (run *ActionRun, columns
return run, columns, nil
}
// DeleteRun removes the given run. It is the caller's responsibility to handle the run's dependencies like artifacts or
// jobs. Nothing happens if the run does not exist.
func DeleteRun(ctx context.Context, runID int64) error {
_, err := db.GetEngine(ctx).Delete(&ActionRun{ID: runID})
return err
}
type ActionRunIndex db.ResourceIndex

View file

@ -365,3 +365,10 @@ func (job *ActionRunJob) AllNeedsExist(allExistingJobIDs container.Set[string])
return unknownJobIDs, len(unknownJobIDs) == 0
}
// DeleteJob removes the given job. Removing all associated tasks is up to the caller. If the given job does not exist,
// nothing happens.
func DeleteJob(ctx context.Context, jobID int64) error {
_, err := db.GetEngine(ctx).Delete(&ActionRunJob{ID: jobID})
return err
}

View file

@ -396,6 +396,13 @@ func FixRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
return res.RowsAffected()
}
// DeleteEphemeralRunner removes the ephemeral runner with the given ID. If the runner with the given ID is not an
// ephemeral runner, nothing happens.
func DeleteEphemeralRunner(ctx context.Context, id int64) error {
_, err := db.GetEngine(ctx).Where(builder.Eq{"id": id, "ephemeral": true}).Delete(&ActionRunner{})
return err
}
func DeleteOfflineRunners(ctx context.Context, olderThan timeutil.TimeStamp, globalOnly bool) error {
log.Info("Doing: DeleteOfflineRunners")

View file

@ -479,3 +479,62 @@ func TestRunner_FindRunnerOptionsToConds(t *testing.T) {
})
}
}
func TestDeleteEphemeralRunner(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
persistentRunnerOne := &ActionRunner{
ID: 606526,
UUID: "d53a1222-ae7a-4430-97f8-8fcb6efd04c9",
Name: "persistent-runner-one",
OwnerID: 2,
RepoID: 0,
Ephemeral: false,
TokenHash: "J9YDsQL",
}
persistentRunnerTwo := &ActionRunner{
ID: 606527,
UUID: "3dc23067-b2fd-4daf-b428-dddad80d7f37",
Name: "persistent-runner-two",
OwnerID: 2,
RepoID: 0,
Ephemeral: false,
TokenHash: "jvIylZtHsS",
}
ephemeralRunnerOne := &ActionRunner{
ID: 606528,
UUID: "2d9bc0a1-7019-4ed3-ba67-6415415ac2a9",
Name: "ephemeral-runner-one",
OwnerID: 2,
RepoID: 0,
Ephemeral: true,
TokenHash: "t9C8L0kM3W",
}
ephemeralRunnerTwo := &ActionRunner{
ID: 606529,
UUID: "da7a03f8-ab39-4c54-9ec9-2bd312fe3be1",
Name: "ephemeral-runner-two",
OwnerID: 2,
RepoID: 0,
Ephemeral: true,
TokenHash: "g9oTOFM",
}
require.NoError(t, CreateRunner(t.Context(), persistentRunnerOne))
require.NoError(t, CreateRunner(t.Context(), persistentRunnerTwo))
require.NoError(t, CreateRunner(t.Context(), ephemeralRunnerOne))
require.NoError(t, CreateRunner(t.Context(), ephemeralRunnerTwo))
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: persistentRunnerOne.ID})
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: persistentRunnerTwo.ID})
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: ephemeralRunnerOne.ID})
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: ephemeralRunnerTwo.ID})
require.NoError(t, DeleteEphemeralRunner(t.Context(), persistentRunnerOne.ID))
require.NoError(t, DeleteEphemeralRunner(t.Context(), ephemeralRunnerOne.ID))
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: persistentRunnerOne.ID})
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: persistentRunnerTwo.ID})
unittest.AssertNotExistsBean(t, &ActionRunner{ID: ephemeralRunnerOne.ID})
unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: ephemeralRunnerTwo.ID})
}

View file

@ -192,6 +192,15 @@ func HasTaskForRunner(ctx context.Context, runnerID int64) (bool, error) {
return db.GetEngine(ctx).Where("runner_id = ?", runnerID).Exist(&ActionTask{})
}
func GetTasksOfJob(ctx context.Context, jobID int64) ([]*ActionTask, error) {
var tasks []*ActionTask
err := db.GetEngine(ctx).Where("job_id=?", jobID).Find(&tasks)
if err != nil {
return nil, fmt.Errorf("cannot fetch tasks of job %d: %w", jobID, err)
}
return tasks, nil
}
func GetTaskByJobAttempt(ctx context.Context, jobID, attempt int64) (*ActionTask, error) {
var task ActionTask
has, err := db.GetEngine(ctx).Where("job_id=?", jobID).Where("attempt=?", attempt).Get(&task)
@ -497,6 +506,27 @@ func UpdateTask(ctx context.Context, task *ActionTask, cols ...string) error {
return err
}
// DeleteTask removes the given task including all its steps and outputs. Removing logs and ephemeral runners is the
// caller's responsibility.
func DeleteTask(ctx context.Context, taskID int64) error {
return db.WithTx(ctx, func(ctx context.Context) error {
var err error
_, err = db.GetEngine(ctx).Delete(&ActionTaskStep{TaskID: taskID})
if err != nil {
return fmt.Errorf("unable to delete steps of task %d: %w", taskID, err)
}
_, err = db.GetEngine(ctx).Delete(&ActionTaskOutput{TaskID: taskID})
if err != nil {
return fmt.Errorf("unable to delete outputs of task %d: %w", taskID, err)
}
_, err = db.GetEngine(ctx).Delete(&ActionTask{ID: taskID})
if err != nil {
return fmt.Errorf("unable to delete task %d: %w", taskID, err)
}
return nil
})
}
func FindOldTasksToExpire(ctx context.Context, olderThan timeutil.TimeStamp, limit int) ([]*ActionTask, error) {
e := db.GetEngine(ctx)