Skip to content

Commit

Permalink
Remove db.DefaultContext usage in routers, use ctx directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 22, 2022
1 parent 80fd255 commit 6d5f307
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 65 deletions.
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -144,7 +144,7 @@ func GetDeployKey(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/DeployKey"

key, err := asymkey_model.GetDeployKeyByID(db.DefaultContext, ctx.ParamsInt64(":id"))
key, err := asymkey_model.GetDeployKeyByID(ctx, ctx.ParamsInt64(":id"))
if err != nil {
if asymkey_model.IsErrDeployKeyNotExist(err) {
ctx.NotFound()
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
2 changes: 1 addition & 1 deletion routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
}
if reviewID < 0 {
// negative reviewIDs represent team requests
if err := issue.Repo.GetOwner(db.DefaultContext); err != nil {
if err := issue.Repo.GetOwner(ctx); err != nil {
ctx.ServerError("issue.Repo.GetOwner", err)
return
}
Expand Down
11 changes: 5 additions & 6 deletions routers/web/repo/issue_content_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
issuesModel "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/context"
Expand All @@ -32,7 +31,7 @@ func GetContentHistoryOverview(ctx *context.Context) {
}

lang := ctx.Locale.Language()
editedHistoryCountMap, _ := issuesModel.QueryIssueContentHistoryEditedCountMap(db.DefaultContext, issue.ID)
editedHistoryCountMap, _ := issuesModel.QueryIssueContentHistoryEditedCountMap(ctx, issue.ID)
ctx.JSON(http.StatusOK, map[string]interface{}{
"i18n": map[string]interface{}{
"textEdited": i18n.Tr(lang, "repo.issues.content_history.edited"),
Expand All @@ -52,7 +51,7 @@ func GetContentHistoryList(ctx *context.Context) {
return
}

items, _ := issuesModel.FetchIssueContentHistoryList(db.DefaultContext, issue.ID, commentID)
items, _ := issuesModel.FetchIssueContentHistoryList(ctx, issue.ID, commentID)

// render history list to HTML for frontend dropdown items: (name, value)
// name is HTML of "avatar + userName + userAction + timeSince"
Expand Down Expand Up @@ -119,7 +118,7 @@ func GetContentHistoryDetail(ctx *context.Context) {
}

historyID := ctx.FormInt64("history_id")
history, prevHistory, err := issuesModel.GetIssueContentHistoryAndPrev(db.DefaultContext, historyID)
history, prevHistory, err := issuesModel.GetIssueContentHistoryAndPrev(ctx, historyID)
if err != nil {
ctx.JSON(http.StatusNotFound, map[string]interface{}{
"message": "Can not find the content history",
Expand Down Expand Up @@ -196,7 +195,7 @@ func SoftDeleteContentHistory(ctx *context.Context) {
return
}
}
if history, err = issuesModel.GetIssueContentHistoryByID(db.DefaultContext, historyID); err != nil {
if history, err = issuesModel.GetIssueContentHistoryByID(ctx, historyID); err != nil {
log.Error("can not get issue content history %v. err=%v", historyID, err)
return
}
Expand All @@ -209,7 +208,7 @@ func SoftDeleteContentHistory(ctx *context.Context) {
return
}

err = issuesModel.SoftDeleteIssueContentHistory(db.DefaultContext, historyID)
err = issuesModel.SoftDeleteIssueContentHistory(ctx, historyID)
log.Debug("soft delete issue content history. issue=%d, comment=%d, history=%d", issue.ID, commentID, historyID)
ctx.JSON(http.StatusOK, map[string]interface{}{
"ok": err == nil,
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func InitializeLabels(ctx *context.Context) {
return
}

if err := models.InitializeLabels(db.DefaultContext, ctx.Repo.Repository.ID, form.TemplateName, false); err != nil {
if err := models.InitializeLabels(ctx, ctx.Repo.Repository.ID, form.TemplateName, false); 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
4 changes: 2 additions & 2 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
return nil
}

if err := forkRepo.GetOwner(db.DefaultContext); err != nil {
if err := forkRepo.GetOwner(ctx); err != nil {
ctx.ServerError("GetOwner", err)
return nil
}
Expand Down Expand Up @@ -1255,7 +1255,7 @@ func CleanUpPullRequest(ctx *context.Context) {
} else if err = pr.LoadBaseRepo(); err != nil {
ctx.ServerError("LoadBaseRepo", err)
return
} else if err = pr.HeadRepo.GetOwner(db.DefaultContext); err != nil {
} else if err = pr.HeadRepo.GetOwner(ctx); err != nil {
ctx.ServerError("HeadRepo.GetOwner", err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func RedirectDownload(ctx *context.Context) {
)
tagNames := []string{vTag}
curRepo := ctx.Repo.Repository
releases, err := models.GetReleasesByRepoIDAndNames(db.DefaultContext, curRepo.ID, tagNames)
releases, err := models.GetReleasesByRepoIDAndNames(ctx, curRepo.ID, tagNames)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.Error(http.StatusNotFound)
Expand Down Expand Up @@ -394,7 +394,7 @@ func Download(ctx *context.Context) {
return
}

archiver, err := repo_model.GetRepoArchiver(db.DefaultContext, aReq.RepoID, aReq.Type, aReq.CommitID)
archiver, err := repo_model.GetRepoArchiver(ctx, aReq.RepoID, aReq.Type, aReq.CommitID)
if err != nil {
ctx.ServerError("models.GetRepoArchiver", err)
return
Expand Down Expand Up @@ -424,7 +424,7 @@ func Download(ctx *context.Context) {
return
}
times++
archiver, err = repo_model.GetRepoArchiver(db.DefaultContext, aReq.RepoID, aReq.Type, aReq.CommitID)
archiver, err = repo_model.GetRepoArchiver(ctx, aReq.RepoID, aReq.Type, aReq.CommitID)
if err != nil {
ctx.ServerError("archiver_service.StartArchive", err)
return
Expand Down Expand Up @@ -480,7 +480,7 @@ func InitiateDownload(ctx *context.Context) {
return
}

archiver, err := repo_model.GetRepoArchiver(db.DefaultContext, aReq.RepoID, aReq.Type, aReq.CommitID)
archiver, err := repo_model.GetRepoArchiver(ctx, aReq.RepoID, aReq.Type, aReq.CommitID)
if err != nil {
ctx.ServerError("archiver_service.StartArchive", err)
return
Expand Down
6 changes: 3 additions & 3 deletions routers/web/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func SettingsPost(ctx *context.Context) {
ctx.Error(http.StatusNotFound)
return
}
if err := repo.GetOwner(db.DefaultContext); err != nil {
if err := repo.GetOwner(ctx); err != nil {
ctx.ServerError("Convert Fork", err)
return
}
Expand Down Expand Up @@ -1055,7 +1055,7 @@ func DeployKeys(ctx *context.Context) {
ctx.Data["PageIsSettingsKeys"] = true
ctx.Data["DisableSSH"] = setting.SSH.Disabled

keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID})
keys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID})
if err != nil {
ctx.ServerError("ListDeployKeys", err)
return
Expand All @@ -1071,7 +1071,7 @@ func DeployKeysPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
ctx.Data["PageIsSettingsKeys"] = true

keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID})
keys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID})
if err != nil {
ctx.ServerError("ListDeployKeys", err)
return
Expand Down
Loading

0 comments on commit 6d5f307

Please sign in to comment.