diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-03-22 23:22:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 16:22:54 +0100 |
commit | 7a550b3af20dad94cf468012a6b817b43b91c1ba (patch) | |
tree | 1b836eabf8e911f35d0446c0cdad053cb3572c79 /modules | |
parent | 2b55422cd71b0b325f054646de5cebf39b72b502 (diff) | |
download | gitea-7a550b3af20dad94cf468012a6b817b43b91c1ba.tar.gz gitea-7a550b3af20dad94cf468012a6b817b43b91c1ba.zip |
Use `ctx` instead of `db.DefaultContext` in some packages(routers/services/modules) (#19163)
* Remove `db.DefaultContext` usage in routers, use `ctx` directly
* Use `ctx` directly if there is one, remove some `db.DefaultContext` in `services`
* Use ctx instead of db.DefaultContext for `cmd` and some `modules` packages
* fix incorrect context usage
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/repo.go | 7 | ||||
-rw-r--r-- | modules/doctor/checkOldArchives.go | 2 | ||||
-rw-r--r-- | modules/doctor/fix16961.go | 2 | ||||
-rw-r--r-- | modules/doctor/mergebase.go | 8 | ||||
-rw-r--r-- | modules/doctor/misc.go | 12 | ||||
-rw-r--r-- | modules/repository/repo.go | 2 | ||||
-rw-r--r-- | modules/test/context_tests.go | 3 |
7 files changed, 17 insertions, 19 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index d64380dd95..f8b07ffb05 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -15,7 +15,6 @@ import ( "strings" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -256,7 +255,7 @@ func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) { } ctx.ServerError("GetBaseRepo", err) return - } else if err = repo.BaseRepo.GetOwner(db.DefaultContext); err != nil { + } else if err = repo.BaseRepo.GetOwner(ctx); err != nil { ctx.ServerError("BaseRepo.GetOwner", err) return } @@ -273,7 +272,7 @@ func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) { } ctx.ServerError("GetTemplateRepo", err) return - } else if err = templateRepo.GetOwner(db.DefaultContext); err != nil { + } else if err = templateRepo.GetOwner(ctx); err != nil { ctx.ServerError("TemplateRepo.GetOwner", err) return } @@ -341,7 +340,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) { func repoAssignment(ctx *Context, repo *repo_model.Repository) { var err error - if err = repo.GetOwner(db.DefaultContext); err != nil { + if err = repo.GetOwner(ctx); err != nil { ctx.ServerError("GetOwner", err) return } diff --git a/modules/doctor/checkOldArchives.go b/modules/doctor/checkOldArchives.go index 6353eaddd2..cefb3817bf 100644 --- a/modules/doctor/checkOldArchives.go +++ b/modules/doctor/checkOldArchives.go @@ -17,7 +17,7 @@ import ( func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error { numRepos := 0 numReposUpdated := 0 - err := iterateRepositories(func(repo *repo_model.Repository) error { + err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { if repo.IsEmpty { return nil } diff --git a/modules/doctor/fix16961.go b/modules/doctor/fix16961.go index e241838068..92c77ba80f 100644 --- a/modules/doctor/fix16961.go +++ b/modules/doctor/fix16961.go @@ -268,7 +268,7 @@ func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix boo count := 0 err := db.Iterate( - db.DefaultContext, + ctx, new(RepoUnit), builder.Gt{ "id": 0, diff --git a/modules/doctor/mergebase.go b/modules/doctor/mergebase.go index a655826e1c..5a780ed2a2 100644 --- a/modules/doctor/mergebase.go +++ b/modules/doctor/mergebase.go @@ -18,9 +18,9 @@ import ( "xorm.io/builder" ) -func iteratePRs(repo *repo_model.Repository, each func(*repo_model.Repository, *models.PullRequest) error) error { +func iteratePRs(ctx context.Context, repo *repo_model.Repository, each func(*repo_model.Repository, *models.PullRequest) error) error { return db.Iterate( - db.DefaultContext, + ctx, new(models.PullRequest), builder.Eq{"base_repo_id": repo.ID}, func(idx int, bean interface{}) error { @@ -33,9 +33,9 @@ func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) erro numRepos := 0 numPRs := 0 numPRsUpdated := 0 - err := iterateRepositories(func(repo *repo_model.Repository) error { + err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { numRepos++ - return iteratePRs(repo, func(repo *repo_model.Repository, pr *models.PullRequest) error { + return iteratePRs(ctx, repo, func(repo *repo_model.Repository, pr *models.PullRequest) error { numPRs++ pr.BaseRepo = repo repoPath := repo.RepoPath() diff --git a/modules/doctor/misc.go b/modules/doctor/misc.go index 2f6baa814d..e96890fbab 100644 --- a/modules/doctor/misc.go +++ b/modules/doctor/misc.go @@ -27,9 +27,9 @@ import ( "xorm.io/builder" ) -func iterateRepositories(each func(*repo_model.Repository) error) error { +func iterateRepositories(ctx context.Context, each func(*repo_model.Repository) error) error { err := db.Iterate( - db.DefaultContext, + ctx, new(repo_model.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { @@ -50,7 +50,7 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error } func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error { - if err := iterateRepositories(func(repo *repo_model.Repository) error { + if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { results, err := repository.CheckDelegateHooks(repo.RepoPath()) if err != nil { logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err) @@ -86,7 +86,7 @@ func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool numRepos := 0 numNeedUpdate := 0 - if err := iterateRepositories(func(repo *repo_model.Repository) error { + if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { numRepos++ r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath()) if err != nil { @@ -132,13 +132,13 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err logger.Critical("Unable to create cache: %v", err) return err } - if err := iterateRepositories(func(repo *repo_model.Repository) error { + if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { numRepos++ if owner, has := cache.Get(repo.OwnerID); has { repo.Owner = owner.(*user_model.User) } else { - if err := repo.GetOwner(db.DefaultContext); err != nil { + if err := repo.GetOwner(ctx); err != nil { return err } cache.Add(repo.OwnerID, repo.Owner) diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 13710bd4ab..98388bbd7b 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -156,7 +156,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, } } - if err = models.UpdateRepoSize(db.DefaultContext, repo); err != nil { + if err = models.UpdateRepoSize(ctx, repo); err != nil { log.Error("Failed to update size for repository: %v", err) } diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index e5cea26eb9..e43f0429e7 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -14,7 +14,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -87,7 +86,7 @@ func LoadUser(t *testing.T, ctx *context.Context, userID int64) { // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has // already been populated. func LoadGitRepo(t *testing.T, ctx *context.Context) { - assert.NoError(t, ctx.Repo.Repository.GetOwner(db.DefaultContext)) + assert.NoError(t, ctx.Repo.Repository.GetOwner(ctx)) var err error ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath()) assert.NoError(t, err) |