1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Rework create/fork/adopt/generate repository to make sure resources will be cleanup once failed (#31035)

Fix #28144 

To make the resources will be cleanup once failed. All repository
operations now follow a consistent pattern:

- 1. Create a database record for the repository with the status
being_migrated.
- 2. Register a deferred cleanup function to delete the repository and
its related data if the operation fails.
- 3.	Perform the actual Git and database operations step by step.
- 4. Upon successful completion, update the repository’s status to
ready.

The adopt operation is a special case — if it fails, the repository on
disk should not be deleted.
This commit is contained in:
Lunny Xiao
2025-04-07 22:12:54 -07:00
committed by GitHub
parent 90b509aafb
commit a100ac3306
14 changed files with 562 additions and 390 deletions

View File

@@ -32,6 +32,19 @@ import (
"xorm.io/builder"
)
func deleteDBRepository(ctx context.Context, repoID int64) error {
if cnt, err := db.GetEngine(ctx).ID(repoID).Delete(&repo_model.Repository{}); err != nil {
return err
} else if cnt != 1 {
return repo_model.ErrRepoNotExist{
ID: repoID,
OwnerName: "",
Name: "",
}
}
return nil
}
// DeleteRepository deletes a repository for a user or organization.
// make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock)
func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID int64, ignoreOrgTeams ...bool) error {
@@ -82,14 +95,8 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
}
needRewriteKeysFile := deleted > 0
if cnt, err := sess.ID(repoID).Delete(&repo_model.Repository{}); err != nil {
if err := deleteDBRepository(ctx, repoID); err != nil {
return err
} else if cnt != 1 {
return repo_model.ErrRepoNotExist{
ID: repoID,
OwnerName: "",
Name: "",
}
}
if org != nil && org.IsOrganization() {