Skip to content

Commit 59f9ef9

Browse files
earl-warrenGusted
andauthored
Remove action runners on user deletion (#27902)
- On user deletion, delete action runners that the user has created. - Add a database consistency check to remove action runners that have nonexistent belonging owner. - Resolves https://codeberg.org/forgejo/forgejo/issues/1720 (cherry picked from commit 009ca7223dab054f7f760b7ccae69e745eebfabb) Co-authored-by: Gusted <[email protected]>
1 parent da0c4b8 commit 59f9ef9

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

models/actions/runner.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,27 @@ func CreateRunner(ctx context.Context, t *ActionRunner) error {
266266
_, err := db.GetEngine(ctx).Insert(t)
267267
return err
268268
}
269+
270+
func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
271+
// Only affect action runners were a owner ID is set, as actions runners
272+
// could also be created on a repository.
273+
return db.GetEngine(ctx).Table("action_runner").
274+
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
275+
Where("`action_runner`.owner_id != ?", 0).
276+
And(builder.IsNull{"`user`.id"}).
277+
Count(new(ActionRunner))
278+
}
279+
280+
func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
281+
subQuery := builder.Select("`action_runner`.id").
282+
From("`action_runner`").
283+
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
284+
Where(builder.Neq{"`action_runner`.owner_id": 0}).
285+
And(builder.IsNull{"`user`.id"})
286+
b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
287+
res, err := db.GetEngine(ctx).Exec(b)
288+
if err != nil {
289+
return 0, err
290+
}
291+
return res.RowsAffected()
292+
}

modules/doctor/dbconsistency.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package doctor
66
import (
77
"context"
88

9+
actions_model "code.gitea.io/gitea/models/actions"
910
activities_model "code.gitea.io/gitea/models/activities"
1011
"code.gitea.io/gitea/models/db"
1112
issues_model "code.gitea.io/gitea/models/issues"
@@ -151,6 +152,12 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
151152
Fixer: activities_model.FixActionCreatedUnixString,
152153
FixedMessage: "Set to zero",
153154
},
155+
{
156+
Name: "Action Runners without existing owner",
157+
Counter: actions_model.CountRunnersWithoutBelongingOwner,
158+
Fixer: actions_model.FixRunnersWithoutBelongingOwner,
159+
FixedMessage: "Removed",
160+
},
154161
}
155162

156163
// TODO: function to recalc all counters

services/user/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
_ "image/jpeg" // Needed for jpeg support
1212

13+
actions_model "code.gitea.io/gitea/models/actions"
1314
activities_model "code.gitea.io/gitea/models/activities"
1415
asymkey_model "code.gitea.io/gitea/models/asymkey"
1516
auth_model "code.gitea.io/gitea/models/auth"
@@ -90,6 +91,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
9091
&pull_model.AutoMerge{DoerID: u.ID},
9192
&pull_model.ReviewState{UserID: u.ID},
9293
&user_model.Redirect{RedirectUserID: u.ID},
94+
&actions_model.ActionRunner{OwnerID: u.ID},
9395
); err != nil {
9496
return fmt.Errorf("deleteBeans: %w", err)
9597
}

0 commit comments

Comments
 (0)