Skip to content

Commit

Permalink
Use ctx instead of db.DefaultContext in some packages(routers/ser…
Browse files Browse the repository at this point in the history
…vices/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
  • Loading branch information
wxiaoguang authored Mar 22, 2022
1 parent 2b55422 commit 7a550b3
Show file tree
Hide file tree
Showing 51 changed files with 117 additions and 124 deletions.
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func runChangePassword(c *cli.Context) error {
return err
}

if err = user_model.UpdateUserCols(db.DefaultContext, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
if err = user_model.UpdateUserCols(ctx, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
return err
}

Expand Down
7 changes: 3 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion modules/doctor/checkOldArchives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion modules/doctor/fix16961.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions modules/doctor/mergebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions modules/doctor/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func Edit(ctx *context.APIContext) {
if form.RepoAdminChangeTeamAccess != nil {
org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess
}
if err := user_model.UpdateUserCols(db.DefaultContext, org.AsUser(),
if err := user_model.UpdateUserCols(ctx, org.AsUser(),
"full_name", "description", "website", "location",
"visibility", "repo_admin_change_team_access",
); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions routers/api/v1/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/routers/api/v1/utils"
Expand Down Expand Up @@ -56,7 +55,7 @@ func StartIssueStopwatch(ctx *context.APIContext) {
return
}

if err := models.CreateIssueStopwatch(db.DefaultContext, ctx.Doer, issue); err != nil {
if err := models.CreateIssueStopwatch(ctx, ctx.Doer, issue); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
return
}
Expand Down Expand Up @@ -105,7 +104,7 @@ func StopIssueStopwatch(ctx *context.APIContext) {
return
}

if err := models.FinishIssueStopwatch(db.DefaultContext, ctx.Doer, issue); err != nil {
if err := models.FinishIssueStopwatch(ctx, ctx.Doer, issue); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ListDeployKeys(ctx *context.APIContext) {
Fingerprint: ctx.FormString("fingerprint"),
}

keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, opts)
keys, err := asymkey_model.ListDeployKeys(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func Search(ctx *context.APIContext) {

results := make([]*api.Repository, len(repos))
for i, repo := range repos {
if err = repo.GetOwner(db.DefaultContext); err != nil {
if err = repo.GetOwner(ctx); err != nil {
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/user/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, uid, listOptions)
keys, err := asymkey_model.ListGPGKeys(ctx, uid, listOptions)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/user/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -124,7 +123,7 @@ func ListMyRepos(ctx *context.APIContext) {

results := make([]*api.Repository, len(repos))
for i, repo := range repos {
if err = repo.GetOwner(db.DefaultContext); err != nil {
if err = repo.GetOwner(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "GetOwner", err)
return
}
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/utils/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
Expand Down Expand Up @@ -164,7 +163,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
if err := w.UpdateEvent(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
return nil, false
} else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
return nil, false
}
Expand Down
10 changes: 5 additions & 5 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func resetLocale(ctx *context.Context, u *user_model.User) error {
// If the user does not have a locale set, we save the current one.
if len(u.Language) == 0 {
u.Language = ctx.Locale.Language()
if err := user_model.UpdateUserCols(db.DefaultContext, u, "language"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "language"); err != nil {
return err
}
}
Expand Down Expand Up @@ -333,7 +333,7 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember, obeyRe
// If the user does not have a locale set, we save the current one.
if len(u.Language) == 0 {
u.Language = ctx.Locale.Language()
if err := user_model.UpdateUserCols(db.DefaultContext, u, "language"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "language"); err != nil {
ctx.ServerError("UpdateUserCols Language", fmt.Errorf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language))
return setting.AppSubURL + "/"
}
Expand All @@ -350,7 +350,7 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember, obeyRe

// Register last login
u.SetLastLogin()
if err := user_model.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "last_login_unix"); err != nil {
ctx.ServerError("UpdateUserCols", err)
return setting.AppSubURL + "/"
}
Expand Down Expand Up @@ -606,7 +606,7 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
u.IsAdmin = true
u.IsActive = true
u.SetLastLogin()
if err := user_model.UpdateUserCols(db.DefaultContext, u, "is_admin", "is_active", "last_login_unix"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "is_admin", "is_active", "last_login_unix"); err != nil {
ctx.ServerError("UpdateUser", err)
return
}
Expand Down Expand Up @@ -733,7 +733,7 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
ctx.ServerError("UpdateUser", err)
return
}
if err := user_model.UpdateUserCols(db.DefaultContext, user, "is_active", "rands"); err != nil {
if err := user_model.UpdateUserCols(ctx, user, "is_active", "rands"); err != nil {
if user_model.IsErrUserNotExist(err) {
ctx.NotFound("UpdateUserCols", err)
} else {
Expand Down
5 changes: 2 additions & 3 deletions routers/web/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -1021,7 +1020,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *auth.Source, u *user_model
cols = append(cols, "is_admin", "is_restricted")
}

if err := user_model.UpdateUserCols(db.DefaultContext, u, cols...); err != nil {
if err := user_model.UpdateUserCols(ctx, u, cols...); err != nil {
ctx.ServerError("UpdateUserCols", err)
return
}
Expand All @@ -1048,7 +1047,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *auth.Source, u *user_model

changed := setUserGroupClaims(source, u, &gothUser)
if changed {
if err := user_model.UpdateUserCols(db.DefaultContext, u, "is_admin", "is_restricted"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "is_admin", "is_restricted"); err != nil {
ctx.ServerError("UpdateUserCols", err)
return
}
Expand Down
5 changes: 2 additions & 3 deletions routers/web/auth/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"

"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -232,7 +231,7 @@ func ResetPasswdPost(ctx *context.Context) {
return
}
u.MustChangePassword = false
if err := user_model.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil {
ctx.ServerError("UpdateUser", err)
return
}
Expand Down Expand Up @@ -327,7 +326,7 @@ func MustChangePasswordPost(ctx *context.Context) {

u.MustChangePassword = false

if err := user_model.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil {
if err := user_model.UpdateUserCols(ctx, u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil {
ctx.ServerError("UpdateUser", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/org/org_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func InitializeLabels(ctx *context.Context) {
return
}

if err := models.InitializeLabels(db.DefaultContext, ctx.Org.Organization.ID, form.TemplateName, true); err != nil {
if err := models.InitializeLabels(ctx, ctx.Org.Organization.ID, form.TemplateName, true); err != nil {
if models.IsErrIssueLabelTemplateLoad(err) {
originalErr := err.(models.ErrIssueLabelTemplateLoad).OriginalError
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -269,7 +268,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
}
return nil
}
if err := ci.HeadRepo.GetOwner(db.DefaultContext); err != nil {
if err := ci.HeadRepo.GetOwner(ctx); err != nil {
if user_model.IsErrUserNotExist(err) {
ctx.NotFound("GetUserByName", nil)
} else {
Expand Down
3 changes: 1 addition & 2 deletions routers/web/repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
Expand Down Expand Up @@ -159,7 +158,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {

// don't allow anonymous pulls if organization is not public
if isPublicPull {
if err := repo.GetOwner(db.DefaultContext); err != nil {
if err := repo.GetOwner(ctx); err != nil {
ctx.ServerError("GetOwner", err)
return
}
Expand Down
Loading

0 comments on commit 7a550b3

Please sign in to comment.