diff options
author | JakobDev <jakobdev@gmx.de> | 2023-10-19 15:16:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-19 13:16:11 +0000 |
commit | 3dc0c962bf3a74188bed02c01b4d34636112805a (patch) | |
tree | 588d9e7d8fbc3a4a862f466cada768558a09cc93 /services/repository | |
parent | 398eccb322f64dded83a119467af2cb38f6dbd46 (diff) | |
download | gitea-3dc0c962bf3a74188bed02c01b4d34636112805a.tar.gz gitea-3dc0c962bf3a74188bed02c01b4d34636112805a.zip |
Delete repos of org when purge delete user (#27273)
Fixes https://codeberg.org/forgejo/forgejo/issues/1514
I had to remove `RenameOrganization` to avoid circular import.
We should really add some foreign keys to the database.
Diffstat (limited to 'services/repository')
-rw-r--r-- | services/repository/delete.go | 27 | ||||
-rw-r--r-- | services/repository/delete_test.go | 16 | ||||
-rw-r--r-- | services/repository/lfs_test.go | 7 |
3 files changed, 44 insertions, 6 deletions
diff --git a/services/repository/delete.go b/services/repository/delete.go index b7fe4282b3..9b0a988ea3 100644 --- a/services/repository/delete.go +++ b/services/repository/delete.go @@ -420,3 +420,30 @@ func RemoveRepositoryFromTeam(ctx context.Context, t *organization.Team, repoID return committer.Commit() } + +// DeleteOwnerRepositoriesDirectly calls DeleteRepositoryDirectly for all repos of the given owner +func DeleteOwnerRepositoriesDirectly(ctx context.Context, owner *user_model.User) error { + for { + repos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{ + ListOptions: db.ListOptions{ + PageSize: repo_model.RepositoryListDefaultPageSize, + Page: 1, + }, + Private: true, + OwnerID: owner.ID, + Actor: owner, + }) + if err != nil { + return fmt.Errorf("GetUserRepositories: %w", err) + } + if len(repos) == 0 { + break + } + for _, repo := range repos { + if err := DeleteRepositoryDirectly(ctx, owner, repo.ID); err != nil { + return fmt.Errorf("unable to delete repository %s for %s[%d]. Error: %w", repo.Name, owner.Name, owner.ID, err) + } + } + } + return nil +} diff --git a/services/repository/delete_test.go b/services/repository/delete_test.go index ae5fd53962..869b8af11d 100644 --- a/services/repository/delete_test.go +++ b/services/repository/delete_test.go @@ -1,7 +1,7 @@ // Copyright 2017 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package repository +package repository_test import ( "testing" @@ -10,6 +10,8 @@ import ( "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" + repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" ) @@ -19,7 +21,7 @@ func TestTeam_HasRepository(t *testing.T) { test := func(teamID, repoID int64, expected bool) { team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}) - assert.Equal(t, expected, HasRepository(db.DefaultContext, team, repoID)) + assert.Equal(t, expected, repo_service.HasRepository(db.DefaultContext, team, repoID)) } test(1, 1, false) test(1, 3, true) @@ -35,7 +37,7 @@ func TestTeam_RemoveRepository(t *testing.T) { testSuccess := func(teamID, repoID int64) { team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}) - assert.NoError(t, RemoveRepositoryFromTeam(db.DefaultContext, team, repoID)) + assert.NoError(t, repo_service.RemoveRepositoryFromTeam(db.DefaultContext, team, repoID)) unittest.AssertNotExistsBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repoID}) unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &repo_model.Repository{ID: repoID}) } @@ -43,3 +45,11 @@ func TestTeam_RemoveRepository(t *testing.T) { testSuccess(2, 5) testSuccess(1, unittest.NonexistentID) } + +func TestDeleteOwnerRepositoriesDirectly(t *testing.T) { + unittest.PrepareTestEnv(t) + + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + + assert.NoError(t, repo_service.DeleteOwnerRepositoriesDirectly(db.DefaultContext, user)) +} diff --git a/services/repository/lfs_test.go b/services/repository/lfs_test.go index e88befdfef..61348143d0 100644 --- a/services/repository/lfs_test.go +++ b/services/repository/lfs_test.go @@ -1,7 +1,7 @@ // Copyright 2023 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package repository +package repository_test import ( "bytes" @@ -16,12 +16,13 @@ import ( "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" + repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" ) func TestGarbageCollectLFSMetaObjects(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) + unittest.PrepareTestEnv(t) setting.LFS.StartServer = true err := storage.Init() @@ -35,7 +36,7 @@ func TestGarbageCollectLFSMetaObjects(t *testing.T) { lfsOid := storeObjectInRepo(t, repo.ID, &lfsContent) // gc - err = GarbageCollectLFSMetaObjects(context.Background(), GarbageCollectLFSMetaObjectsOptions{ + err = repo_service.GarbageCollectLFSMetaObjects(context.Background(), repo_service.GarbageCollectLFSMetaObjectsOptions{ AutoFix: true, OlderThan: time.Now().Add(7 * 24 * time.Hour).Add(5 * 24 * time.Hour), UpdatedLessRecentlyThan: time.Now().Add(7 * 24 * time.Hour).Add(3 * 24 * time.Hour), |