2021-11-19 02:42:27 +09:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 10:27:50 +09:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-19 02:42:27 +09:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteOrganization completely and permanently deletes everything of organization.
|
2021-11-19 20:41:40 +09:00
|
|
|
func DeleteOrganization(org *models.Organization) error {
|
2021-11-19 02:42:27 +09:00
|
|
|
ctx, commiter, err := db.TxContext()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer commiter.Close()
|
|
|
|
|
|
|
|
// Check ownership of repository.
|
2021-12-10 10:27:50 +09:00
|
|
|
count, err := repo_model.GetRepositoryCount(ctx, org.ID)
|
2021-11-19 02:42:27 +09:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepositoryCount: %v", err)
|
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserOwnRepos{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.DeleteOrganization(ctx, org); err != nil {
|
|
|
|
return fmt.Errorf("DeleteOrganization: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commiter.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: system notice
|
|
|
|
// Note: There are something just cannot be roll back,
|
|
|
|
// so just keep error logs of those operations.
|
2021-11-24 18:49:20 +09:00
|
|
|
path := user_model.UserPath(org.Name)
|
2021-11-19 02:42:27 +09:00
|
|
|
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
|
|
|
return fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(org.Avatar) > 0 {
|
|
|
|
avatarPath := org.CustomAvatarRelativePath()
|
|
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
|
|
|
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|