summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2023-11-05 21:20:00 +0800
committerGitHub <noreply@github.com>2023-11-05 13:20:00 +0000
commit28e3d0b0d356edf10267cdf51a652174b57cc314 (patch)
treec024e07abfe950ecff685f8e5807313b9e8d0101 /models
parent4c67c054805791c0446409fd1ca96ee2a9cb79f6 (diff)
downloadgitea-28e3d0b0d356edf10267cdf51a652174b57cc314.tar.gz
gitea-28e3d0b0d356edf10267cdf51a652174b57cc314.zip
Remove action runners on user deletion (#27902) (#27908)
Backport #27902 by @earl-warren - 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: Earl Warren <109468362+earl-warren@users.noreply.github.com> Co-authored-by: Gusted <postmaster@gusted.xyz>
Diffstat (limited to 'models')
-rw-r--r--models/actions/runner.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/models/actions/runner.go b/models/actions/runner.go
index ec6b49cf16..2c092c2b4a 100644
--- a/models/actions/runner.go
+++ b/models/actions/runner.go
@@ -266,3 +266,27 @@ func CreateRunner(ctx context.Context, t *ActionRunner) error {
_, err := db.GetEngine(ctx).Insert(t)
return err
}
+
+func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
+ // Only affect action runners were a owner ID is set, as actions runners
+ // could also be created on a repository.
+ return db.GetEngine(ctx).Table("action_runner").
+ Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
+ Where("`action_runner`.owner_id != ?", 0).
+ And(builder.IsNull{"`user`.id"}).
+ Count(new(ActionRunner))
+}
+
+func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
+ subQuery := builder.Select("`action_runner`.id").
+ From("`action_runner`").
+ Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
+ Where(builder.Neq{"`action_runner`.owner_id": 0}).
+ And(builder.IsNull{"`user`.id"})
+ b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
+ res, err := db.GetEngine(ctx).Exec(b)
+ if err != nil {
+ return 0, err
+ }
+ return res.RowsAffected()
+}