Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete repos of org when purge delete user #27273

Merged
merged 9 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func Delete(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"

if err := org.DeleteOrganization(ctx.Org.Organization); err != nil {
if err := org.DeleteOrganization(ctx, ctx.Org.Organization, false); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteOrganization", err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions routers/web/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func SettingsPost(ctx *context.Context) {

// Check if organization name has been changed.
if nameChanged {
err := org_service.RenameOrganization(ctx, org, form.Name)
6543 marked this conversation as resolved.
Show resolved Hide resolved
err := user_service.RenameUser(ctx, org.AsUser(), form.Name)
switch {
case user_model.IsErrUserAlreadyExist(err):
ctx.Data["OrgName"] = true
Expand Down Expand Up @@ -180,7 +180,7 @@ func SettingsDelete(ctx *context.Context) {
return
}

if err := org_service.DeleteOrganization(ctx.Org.Organization); err != nil {
if err := org_service.DeleteOrganization(ctx, ctx.Org.Organization, false); err != nil {
if models.IsErrUserOwnRepos(err) {
ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
Expand Down
18 changes: 10 additions & 8 deletions services/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
user_service "code.gitea.io/gitea/services/user"
repo_service "code.gitea.io/gitea/services/repository"
)

// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(org *org_model.Organization) error {
ctx, commiter, err := db.TxContext(db.DefaultContext)
func DeleteOrganization(ctx context.Context, org *org_model.Organization, purge bool) error {
if purge {
err := repo_service.DeleteOwnerRepositoriesDirectly(ctx, org.AsUser())
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}

ctx, commiter, err := db.TxContext(ctx)
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -67,8 +74,3 @@ func DeleteOrganization(org *org_model.Organization) error {

return nil
}

// RenameOrganization renames an organization.
6543 marked this conversation as resolved.
Show resolved Hide resolved
func RenameOrganization(ctx context.Context, org *org_model.Organization, newName string) error {
return user_service.RenameUser(ctx, org.AsUser(), newName)
}
7 changes: 4 additions & 3 deletions services/org/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -24,17 +25,17 @@ func TestMain(m *testing.M) {
func TestDeleteOrganization(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 6})
assert.NoError(t, DeleteOrganization(org))
assert.NoError(t, DeleteOrganization(db.DefaultContext, org, false))
unittest.AssertNotExistsBean(t, &organization.Organization{ID: 6})
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: 6})
unittest.AssertNotExistsBean(t, &organization.Team{OrgID: 6})

org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
err := DeleteOrganization(org)
err := DeleteOrganization(db.DefaultContext, org, false)
assert.Error(t, err)
assert.True(t, models.IsErrUserOwnRepos(err))

user := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 5})
assert.Error(t, DeleteOrganization(user))
assert.Error(t, DeleteOrganization(db.DefaultContext, user, false))
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
}
27 changes: 27 additions & 0 deletions services/repository/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,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(&repo_model.SearchRepoOptions{
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
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, owner.ID, 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
}
9 changes: 9 additions & 0 deletions services/repository/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -43,3 +44,11 @@ func TestTeam_RemoveRepository(t *testing.T) {
testSuccess(2, 5)
testSuccess(1, unittest.NonexistentID)
}

func TestDeleteOwnerRepositoriesDirectly(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

assert.NoError(t, DeleteOwnerRepositoriesDirectly(db.DefaultContext, user))
}
30 changes: 8 additions & 22 deletions services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/agit"
org_service "code.gitea.io/gitea/services/org"
"code.gitea.io/gitea/services/packages"
container_service "code.gitea.io/gitea/services/packages/container"
repo_service "code.gitea.io/gitea/services/repository"
Expand Down Expand Up @@ -158,27 +159,9 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
//
// An alternative option here would be write a DeleteAllRepositoriesForUserID function which would delete all of the repos
// but such a function would likely get out of date
for {
repos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
ListOptions: db.ListOptions{
PageSize: repo_model.RepositoryListDefaultPageSize,
Page: 1,
},
Private: true,
OwnerID: u.ID,
Actor: u,
})
if err != nil {
return fmt.Errorf("GetUserRepositories: %w", err)
}
if len(repos) == 0 {
break
}
for _, repo := range repos {
if err := repo_service.DeleteRepositoryDirectly(ctx, u, u.ID, repo.ID); err != nil {
return fmt.Errorf("unable to delete repository %s for %s[%d]. Error: %w", repo.Name, u.Name, u.ID, err)
}
}
err := repo_service.DeleteOwnerRepositoriesDirectly(ctx, u)
if err != nil {
return err
}

// Remove from Organizations and delete last owner organizations
Expand Down Expand Up @@ -206,7 +189,10 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
for _, org := range orgs {
if err := models.RemoveOrgUser(org.ID, u.ID); err != nil {
if organization.IsErrLastOrgOwner(err) {
err = organization.DeleteOrganization(ctx, org)
err = org_service.DeleteOrganization(ctx, org, true)
if err != nil {
return fmt.Errorf("unable to delete organization %d: %w", org.ID, err)
}
}
if err != nil {
return fmt.Errorf("unable to remove user %s[%d] from org %s[%d]. Error: %w", u.Name, u.ID, org.Name, org.ID, err)
Expand Down
Loading