From 48ccd325a1b81a58ac6d1d5d94fc4e90974599ea Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 16 Nov 2021 21:30:11 +0800 Subject: [PATCH] Move some functions into services/repository (#17660) --- integrations/pull_update_test.go | 3 +- modules/repository/init.go | 86 ----------------- routers/api/v1/admin/adopt.go | 8 +- routers/api/v1/admin/cron.go | 2 +- routers/init.go | 2 +- routers/web/admin/admin.go | 2 +- routers/web/admin/repos.go | 7 +- routers/web/repo/setting.go | 2 +- routers/web/user/setting/adopt.go | 6 +- {modules => services}/cron/cron.go | 0 {modules => services}/cron/setting.go | 0 {modules => services}/cron/tasks.go | 0 {modules => services}/cron/tasks_basic.go | 0 {modules => services}/cron/tasks_extended.go | 3 +- {modules => services}/repository/adopt.go | 93 ++++++++++++++++++- {modules => services}/repository/archive.go | 0 {modules => services}/repository/fork.go | 6 +- {modules => services}/repository/fork_test.go | 0 services/repository/repository.go | 30 ------ 19 files changed, 114 insertions(+), 136 deletions(-) rename {modules => services}/cron/cron.go (100%) rename {modules => services}/cron/setting.go (100%) rename {modules => services}/cron/tasks.go (100%) rename {modules => services}/cron/tasks_basic.go (100%) rename {modules => services}/cron/tasks_extended.go (97%) rename {modules => services}/repository/adopt.go (74%) rename {modules => services}/repository/archive.go (100%) rename {modules => services}/repository/fork.go (95%) rename {modules => services}/repository/fork_test.go (100%) diff --git a/integrations/pull_update_test.go b/integrations/pull_update_test.go index d9112a4b9f..43cfe7debf 100644 --- a/integrations/pull_update_test.go +++ b/integrations/pull_update_test.go @@ -13,7 +13,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/repofiles" - repo_module "code.gitea.io/gitea/modules/repository" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" @@ -89,7 +88,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullReq assert.NoError(t, err) assert.NotEmpty(t, baseRepo) - headRepo, err := repo_module.ForkRepository(actor, forkOrg, models.ForkRepoOptions{ + headRepo, err := repo_service.ForkRepository(actor, forkOrg, models.ForkRepoOptions{ BaseRepo: baseRepo, Name: "repo-pr-update", Description: "desc", diff --git a/modules/repository/init.go b/modules/repository/init.go index 5a1ff7e98b..076dbf748e 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -196,92 +196,6 @@ func checkInitRepository(owner, name string) (err error) { return nil } -func adoptRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) { - isExist, err := util.IsExist(repoPath) - if err != nil { - log.Error("Unable to check if %s exists. Error: %v", repoPath, err) - return err - } - if !isExist { - return fmt.Errorf("adoptRepository: path does not already exist: %s", repoPath) - } - - if err := createDelegateHooks(repoPath); err != nil { - return fmt.Errorf("createDelegateHooks: %v", err) - } - - // Re-fetch the repository from database before updating it (else it would - // override changes that were done earlier with sql) - if repo, err = models.GetRepositoryByIDCtx(ctx, repo.ID); err != nil { - return fmt.Errorf("getRepositoryByID: %v", err) - } - - repo.IsEmpty = false - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return fmt.Errorf("openRepository: %v", err) - } - defer gitRepo.Close() - if len(opts.DefaultBranch) > 0 { - repo.DefaultBranch = opts.DefaultBranch - - if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { - return fmt.Errorf("setDefaultBranch: %v", err) - } - } else { - repo.DefaultBranch, err = gitRepo.GetDefaultBranch() - if err != nil { - repo.DefaultBranch = setting.Repository.DefaultBranch - if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { - return fmt.Errorf("setDefaultBranch: %v", err) - } - } - - repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix) - } - branches, _, _ := gitRepo.GetBranches(0, 0) - found := false - hasDefault := false - hasMaster := false - hasMain := false - for _, branch := range branches { - if branch == repo.DefaultBranch { - found = true - break - } else if branch == setting.Repository.DefaultBranch { - hasDefault = true - } else if branch == "master" { - hasMaster = true - } else if branch == "main" { - hasMain = true - } - } - if !found { - if hasDefault { - repo.DefaultBranch = setting.Repository.DefaultBranch - } else if hasMaster { - repo.DefaultBranch = "master" - } else if hasMain { - repo.DefaultBranch = "main" - } else if len(branches) > 0 { - repo.DefaultBranch = branches[0] - } else { - repo.IsEmpty = true - repo.DefaultBranch = setting.Repository.DefaultBranch - } - - if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { - return fmt.Errorf("setDefaultBranch: %v", err) - } - } - - if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil { - return fmt.Errorf("updateRepository: %v", err) - } - - return nil -} - // InitRepository initializes README and .gitignore if needed. func initRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) { if err = checkInitRepository(repo.OwnerName, repo.Name); err != nil { diff --git a/routers/api/v1/admin/adopt.go b/routers/api/v1/admin/adopt.go index 9c1b9fc0f8..184a2941a8 100644 --- a/routers/api/v1/admin/adopt.go +++ b/routers/api/v1/admin/adopt.go @@ -9,9 +9,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/utils" + repo_service "code.gitea.io/gitea/services/repository" ) // ListUnadoptedRepositories lists the unadopted repositories that match the provided names @@ -41,7 +41,7 @@ func ListUnadoptedRepositories(ctx *context.APIContext) { // "$ref": "#/responses/forbidden" listOptions := utils.GetListOptions(ctx) - repoNames, count, err := repository.ListUnadoptedRepositories(ctx.FormString("query"), &listOptions) + repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx.FormString("query"), &listOptions) if err != nil { ctx.InternalServerError(err) } @@ -104,7 +104,7 @@ func AdoptRepository(ctx *context.APIContext) { ctx.NotFound() return } - if _, err := repository.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{ + if _, err := repo_service.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{ Name: repoName, IsPrivate: true, }); err != nil { @@ -167,7 +167,7 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) { return } - if err := repository.DeleteUnadoptedRepository(ctx.User, ctxUser, repoName); err != nil { + if err := repo_service.DeleteUnadoptedRepository(ctx.User, ctxUser, repoName); err != nil { ctx.InternalServerError(err) return } diff --git a/routers/api/v1/admin/cron.go b/routers/api/v1/admin/cron.go index 970fad8388..1476872a90 100644 --- a/routers/api/v1/admin/cron.go +++ b/routers/api/v1/admin/cron.go @@ -8,11 +8,11 @@ import ( "net/http" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/utils" + "code.gitea.io/gitea/services/cron" ) // ListCronTasks api for getting cron tasks diff --git a/routers/init.go b/routers/init.go index 80e2fec095..6df524dd57 100644 --- a/routers/init.go +++ b/routers/init.go @@ -15,7 +15,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/appstate" "code.gitea.io/gitea/modules/cache" - "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" @@ -42,6 +41,7 @@ import ( "code.gitea.io/gitea/services/archiver" "code.gitea.io/gitea/services/auth" "code.gitea.io/gitea/services/auth/source/oauth2" + "code.gitea.io/gitea/services/cron" "code.gitea.io/gitea/services/mailer" mirror_service "code.gitea.io/gitea/services/mirror" pull_service "code.gitea.io/gitea/services/pull" diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 223114dae1..8cbe852718 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -28,6 +27,7 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/updatechecker" "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/services/cron" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go index 2f4d182af8..a13f7317e4 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -14,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/web/explore" @@ -95,7 +94,7 @@ func UnadoptedRepos(ctx *context.Context) { } ctx.Data["Keyword"] = q - repoNames, count, err := repository.ListUnadoptedRepositories(q, &opts) + repoNames, count, err := repo_service.ListUnadoptedRepositories(q, &opts) if err != nil { ctx.ServerError("ListUnadoptedRepositories", err) } @@ -147,7 +146,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { if has || !isDir { // Fallthrough to failure mode } else if action == "adopt" { - if _, err := repository.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{ + if _, err := repo_service.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{ Name: dirSplit[1], IsPrivate: true, }); err != nil { @@ -156,7 +155,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.adopt_preexisting_success", dir)) } else if action == "delete" { - if err := repository.DeleteUnadoptedRepository(ctx.User, ctxUser, dirSplit[1]); err != nil { + if err := repo_service.DeleteUnadoptedRepository(ctx.User, ctxUser, dirSplit[1]); err != nil { ctx.ServerError("repository.AdoptRepository", err) return } diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index b104ede005..456084f086 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -557,7 +557,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := repository.ConvertForkToNormalRepository(repo); err != nil { + if err := repo_service.ConvertForkToNormalRepository(repo); err != nil { log.Error("Unable to convert repository %-v from fork. Error: %v", repo, err) ctx.ServerError("Convert Fork", err) return diff --git a/routers/web/user/setting/adopt.go b/routers/web/user/setting/adopt.go index 2caccb69a5..948cc1e936 100644 --- a/routers/web/user/setting/adopt.go +++ b/routers/web/user/setting/adopt.go @@ -9,9 +9,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + repo_service "code.gitea.io/gitea/services/repository" ) // AdoptOrDeleteRepository adopts or deletes a repository @@ -44,7 +44,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { if has || !isDir { // Fallthrough to failure mode } else if action == "adopt" && allowAdopt { - if _, err := repository.AdoptRepository(ctxUser, ctxUser, models.CreateRepoOptions{ + if _, err := repo_service.AdoptRepository(ctxUser, ctxUser, models.CreateRepoOptions{ Name: dir, IsPrivate: true, }); err != nil { @@ -53,7 +53,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.adopt_preexisting_success", dir)) } else if action == "delete" && allowDelete { - if err := repository.DeleteUnadoptedRepository(ctxUser, ctxUser, dir); err != nil { + if err := repo_service.DeleteUnadoptedRepository(ctxUser, ctxUser, dir); err != nil { ctx.ServerError("repository.AdoptRepository", err) return } diff --git a/modules/cron/cron.go b/services/cron/cron.go similarity index 100% rename from modules/cron/cron.go rename to services/cron/cron.go diff --git a/modules/cron/setting.go b/services/cron/setting.go similarity index 100% rename from modules/cron/setting.go rename to services/cron/setting.go diff --git a/modules/cron/tasks.go b/services/cron/tasks.go similarity index 100% rename from modules/cron/tasks.go rename to services/cron/tasks.go diff --git a/modules/cron/tasks_basic.go b/services/cron/tasks_basic.go similarity index 100% rename from modules/cron/tasks_basic.go rename to services/cron/tasks_basic.go diff --git a/modules/cron/tasks_extended.go b/services/cron/tasks_extended.go similarity index 97% rename from modules/cron/tasks_extended.go rename to services/cron/tasks_extended.go index 9a37c40faf..4ddcd44537 100644 --- a/modules/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -12,6 +12,7 @@ import ( repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/updatechecker" + repo_service "code.gitea.io/gitea/services/repository" ) func registerDeleteInactiveUsers() { @@ -34,7 +35,7 @@ func registerDeleteRepositoryArchives() { RunAtStart: false, Schedule: "@annually", }, func(ctx context.Context, _ *models.User, _ Config) error { - return repo_module.DeleteRepositoryArchives(ctx) + return repo_service.DeleteRepositoryArchives(ctx) }) } diff --git a/modules/repository/adopt.go b/services/repository/adopt.go similarity index 74% rename from modules/repository/adopt.go rename to services/repository/adopt.go index 3d895ac484..d48411fbb4 100644 --- a/modules/repository/adopt.go +++ b/services/repository/adopt.go @@ -15,12 +15,15 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/notification" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + "github.com/gobwas/glob" ) -// AdoptRepository adopts a repository for the user/organization. +// AdoptRepository adopts pre-existing repository files for the user/organization. func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*models.Repository, error) { if !doer.IsAdmin && !u.CanCreateRepo() { return nil, models.ErrReachLimitOfRepo{ @@ -90,9 +93,97 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode return nil, err } + notification.NotifyCreateRepository(doer, u, repo) + return repo, nil } +func adoptRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) { + isExist, err := util.IsExist(repoPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", repoPath, err) + return err + } + if !isExist { + return fmt.Errorf("adoptRepository: path does not already exist: %s", repoPath) + } + + if err := repo_module.CreateDelegateHooks(repoPath); err != nil { + return fmt.Errorf("createDelegateHooks: %v", err) + } + + // Re-fetch the repository from database before updating it (else it would + // override changes that were done earlier with sql) + if repo, err = models.GetRepositoryByIDCtx(ctx, repo.ID); err != nil { + return fmt.Errorf("getRepositoryByID: %v", err) + } + + repo.IsEmpty = false + gitRepo, err := git.OpenRepository(repo.RepoPath()) + if err != nil { + return fmt.Errorf("openRepository: %v", err) + } + defer gitRepo.Close() + if len(opts.DefaultBranch) > 0 { + repo.DefaultBranch = opts.DefaultBranch + + if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { + return fmt.Errorf("setDefaultBranch: %v", err) + } + } else { + repo.DefaultBranch, err = gitRepo.GetDefaultBranch() + if err != nil { + repo.DefaultBranch = setting.Repository.DefaultBranch + if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { + return fmt.Errorf("setDefaultBranch: %v", err) + } + } + + repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix) + } + branches, _, _ := gitRepo.GetBranches(0, 0) + found := false + hasDefault := false + hasMaster := false + hasMain := false + for _, branch := range branches { + if branch == repo.DefaultBranch { + found = true + break + } else if branch == setting.Repository.DefaultBranch { + hasDefault = true + } else if branch == "master" { + hasMaster = true + } else if branch == "main" { + hasMain = true + } + } + if !found { + if hasDefault { + repo.DefaultBranch = setting.Repository.DefaultBranch + } else if hasMaster { + repo.DefaultBranch = "master" + } else if hasMain { + repo.DefaultBranch = "main" + } else if len(branches) > 0 { + repo.DefaultBranch = branches[0] + } else { + repo.IsEmpty = true + repo.DefaultBranch = setting.Repository.DefaultBranch + } + + if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { + return fmt.Errorf("setDefaultBranch: %v", err) + } + } + + if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil { + return fmt.Errorf("updateRepository: %v", err) + } + + return nil +} + // DeleteUnadoptedRepository deletes unadopted repository files from the filesystem func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error { if err := models.IsUsableRepoName(repoName); err != nil { diff --git a/modules/repository/archive.go b/services/repository/archive.go similarity index 100% rename from modules/repository/archive.go rename to services/repository/archive.go diff --git a/modules/repository/fork.go b/services/repository/fork.go similarity index 95% rename from modules/repository/fork.go rename to services/repository/fork.go index df1dccc596..f052d18a75 100644 --- a/modules/repository/fork.go +++ b/services/repository/fork.go @@ -14,6 +14,8 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/notification" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" ) @@ -116,7 +118,7 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m return fmt.Errorf("git update-server-info: %v", err) } - if err = createDelegateHooks(repoPath); err != nil { + if err = repo_module.CreateDelegateHooks(repoPath); err != nil { return fmt.Errorf("createDelegateHooks: %v", err) } return nil @@ -136,6 +138,8 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m log.Error("Copy language stat from oldRepo failed") } + notification.NotifyForkRepository(doer, opts.BaseRepo, repo) + return repo, nil } diff --git a/modules/repository/fork_test.go b/services/repository/fork_test.go similarity index 100% rename from modules/repository/fork_test.go rename to services/repository/fork_test.go diff --git a/services/repository/repository.go b/services/repository/repository.go index cde4af2afa..98d160c223 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -28,36 +28,6 @@ func CreateRepository(doer, owner *models.User, opts models.CreateRepoOptions) ( return repo, nil } -// AdoptRepository adopts pre-existing repository files for the user/organization. -func AdoptRepository(doer, owner *models.User, opts models.CreateRepoOptions) (*models.Repository, error) { - repo, err := repo_module.AdoptRepository(doer, owner, opts) - if err != nil { - // No need to rollback here we should do this in AdoptRepository... - return nil, err - } - - notification.NotifyCreateRepository(doer, owner, repo) - - return repo, nil -} - -// DeleteUnadoptedRepository adopts pre-existing repository files for the user/organization. -func DeleteUnadoptedRepository(doer, owner *models.User, name string) error { - return repo_module.DeleteUnadoptedRepository(doer, owner, name) -} - -// ForkRepository forks a repository -func ForkRepository(doer, u *models.User, opts models.ForkRepoOptions) (*models.Repository, error) { - repo, err := repo_module.ForkRepository(doer, u, opts) - if err != nil { - return nil, err - } - - notification.NotifyForkRepository(doer, opts.BaseRepo, repo) - - return repo, nil -} - // DeleteRepository deletes a repository for a user or organization. func DeleteRepository(doer *models.User, repo *models.Repository) error { if err := pull_service.CloseRepoBranchesPulls(doer, repo); err != nil {