diff --git a/services/repository/adopt.go b/services/repository/adopt.go index dde796b916..ed3e4f22cc 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -66,7 +66,10 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR } } - if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil { + // create the repository database operations first + if err := db.WithTx(ctx, func(ctx context.Context) error { + return repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false) + }); err != nil { return nil, err } diff --git a/services/repository/create.go b/services/repository/create.go index 7b8dafc886..1d840d3a25 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -241,9 +241,11 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt ObjectFormatName: opts.ObjectFormatName, } - needsUpdateStatus := opts.Status == 0 + needsUpdateStatus := opts.Status != repo_model.RepositoryReady - if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, false, false); err != nil { + if err := db.WithTx(ctx, func(ctx context.Context) error { + return repo_module.CreateRepositoryByExample(ctx, doer, u, repo, false, false) + }); err != nil { return nil, err } diff --git a/services/repository/generate.go b/services/repository/generate.go index 941e22a1c1..4029415cd2 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -15,8 +15,10 @@ import ( "strings" "time" + "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" + system_model "code.gitea.io/gitea/models/system" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" @@ -338,12 +340,26 @@ func generateRepository(ctx context.Context, doer, owner *user_model.User, templ Status: repo_model.RepositoryBeingMigrated, } - if err = repo_module.CreateRepositoryByExample(ctx, doer, owner, generateRepo, false, false); err != nil { + if err := db.WithTx(ctx, func(ctx context.Context) error { + return repo_module.CreateRepositoryByExample(ctx, doer, owner, generateRepo, false, false) + }); err != nil { return nil, err } repoPath := generateRepo.RepoPath() isExist, err := util.IsExist(repoPath) + defer func() { + if err != nil { + if errDelete := DeleteRepositoryDirectly(ctx, doer, generateRepo.ID); errDelete != nil { + log.Error("Rollback deleteRepository: %v", errDelete) + // add system notice + if err := system_model.CreateRepositoryNotice("DeleteRepositoryDirectly failed when generate repository: %v", errDelete); err != nil { + log.Error("CreateRepositoryNotice: %v", err) + } + } + } + }() + if err != nil { log.Error("Unable to check if %s exists. Error: %v", repoPath, err) return nil, err