1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Reduce usage of db.DefaultContext (#27073)

Part of #27065

This reduces the usage of `db.DefaultContext`. I think I've got enough
files for the first PR. When this is merged, I will continue working on
this.

Considering how many files this PR affect, I hope it won't take to long
to merge, so I don't end up in the merge conflict hell.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
JakobDev
2023-09-14 19:09:32 +02:00
committed by GitHub
parent 0de09d3afc
commit 76659b1114
83 changed files with 339 additions and 324 deletions

View File

@@ -37,7 +37,7 @@ func GetAllEmails(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
emails, maxResults, err := user_model.SearchEmails(&user_model.SearchEmailOptions{
emails, maxResults, err := user_model.SearchEmails(ctx, &user_model.SearchEmailOptions{
Keyword: ctx.Params(":email"),
ListOptions: listOptions,
})

View File

@@ -101,7 +101,7 @@ func GetAllOrgs(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
Type: user_model.UserTypeOrganization,
OrderBy: db.SearchOrderByAlphabetically,

View File

@@ -128,7 +128,7 @@ func CreateUser(ctx *context.APIContext) {
u.UpdatedUnix = u.CreatedUnix
}
if err := user_model.CreateUser(u, overwriteDefault); err != nil {
if err := user_model.CreateUser(ctx, u, overwriteDefault); err != nil {
if user_model.IsErrUserAlreadyExist(err) ||
user_model.IsErrEmailAlreadyUsed(err) ||
db.IsErrNameReserved(err) ||
@@ -450,7 +450,7 @@ func SearchUsers(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
Type: user_model.UserTypeIndividual,
LoginName: ctx.FormTrim("login_name"),

View File

@@ -34,12 +34,12 @@ func NodeInfo(ctx *context.APIContext) {
nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
}
if !cached {
usersTotal := int(user_model.CountUsers(nil))
usersTotal := int(user_model.CountUsers(ctx, nil))
now := time.Now()
timeOneMonthAgo := now.AddDate(0, -1, 0).Unix()
timeHaveYearAgo := now.AddDate(0, -6, 0).Unix()
usersActiveMonth := int(user_model.CountUsers(&user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo}))
usersActiveHalfyear := int(user_model.CountUsers(&user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo}))
usersActiveMonth := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo}))
usersActiveHalfyear := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo}))
allIssues, _ := issues_model.CountIssues(ctx, &issues_model.IssuesOptions{})
allComments, _ := issues_model.CountComments(&issues_model.FindCommentsOptions{})

View File

@@ -31,7 +31,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) {
return
}
members, _, err := organization.FindOrgMembers(opts)
members, _, err := organization.FindOrgMembers(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return

View File

@@ -203,7 +203,7 @@ func GetAll(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
publicOrgs, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
publicOrgs, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
ListOptions: listOptions,
Type: user_model.UserTypeOrganization,

View File

@@ -239,7 +239,7 @@ func CreateTeam(ctx *context.APIContext) {
attachAdminTeamUnits(team)
}
if err := models.NewTeam(team); err != nil {
if err := models.NewTeam(ctx, team); err != nil {
if organization.IsErrTeamAlreadyExist(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
@@ -330,7 +330,7 @@ func EditTeam(ctx *context.APIContext) {
attachAdminTeamUnits(team)
}
if err := models.UpdateTeam(team, isAuthChanged, isIncludeAllChanged); err != nil {
if err := models.UpdateTeam(ctx, team, isAuthChanged, isIncludeAllChanged); err != nil {
ctx.Error(http.StatusInternalServerError, "EditTeam", err)
return
}
@@ -361,7 +361,7 @@ func DeleteTeam(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
if err := models.DeleteTeam(ctx.Org.Team); err != nil {
if err := models.DeleteTeam(ctx, ctx.Org.Team); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteTeam", err)
return
}
@@ -493,7 +493,7 @@ func AddTeamMember(ctx *context.APIContext) {
if ctx.Written() {
return
}
if err := models.AddTeamMember(ctx.Org.Team, u.ID); err != nil {
if err := models.AddTeamMember(ctx, ctx.Org.Team, u.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "AddMember", err)
return
}
@@ -530,7 +530,7 @@ func RemoveTeamMember(ctx *context.APIContext) {
return
}
if err := models.RemoveTeamMember(ctx.Org.Team, u.ID); err != nil {
if err := models.RemoveTeamMember(ctx, ctx.Org.Team, u.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "RemoveTeamMember", err)
return
}

View File

@@ -447,7 +447,7 @@ func GetBranchProtection(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
}
// ListBranchProtections list branch protections for a repo
@@ -480,7 +480,7 @@ func ListBranchProtections(ctx *context.APIContext) {
}
apiBps := make([]*api.BranchProtection, len(bps))
for i := range bps {
apiBps[i] = convert.ToBranchProtection(bps[i])
apiBps[i] = convert.ToBranchProtection(ctx, bps[i])
}
ctx.JSON(http.StatusOK, apiBps)
@@ -688,7 +688,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(bp))
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp))
}
// EditBranchProtection edits a branch protection for a repo
@@ -960,7 +960,7 @@ func EditBranchProtection(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
}
// DeleteBranchProtection deletes a branch protection for a repo

View File

@@ -55,7 +55,7 @@ func ListForks(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
forks, err := repo_model.GetForks(ctx.Repo.Repository, utils.GetListOptions(ctx))
forks, err := repo_model.GetForks(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetForks", err)
return

View File

@@ -273,7 +273,7 @@ func GetIssueSubscribers(ctx *context.APIContext) {
userIDs = append(userIDs, iw.UserID)
}
users, err := user_model.GetUsersByIDs(userIDs)
users, err := user_model.GetUsersByIDs(ctx, userIDs)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
return

View File

@@ -985,7 +985,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
}
// Check if current user has fork of repository or in the same repository.
headRepo := repo_model.GetForkedRepo(headUser.ID, baseRepo.ID)
headRepo := repo_model.GetForkedRepo(ctx, headUser.ID, baseRepo.ID)
if headRepo == nil && !isSameRepo {
log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
ctx.NotFound("GetForkedRepo")

View File

@@ -27,7 +27,7 @@ func ListEmails(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/EmailList"
emails, err := user_model.GetEmailAddresses(ctx.Doer.ID)
emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
return
@@ -71,7 +71,7 @@ func AddEmail(ctx *context.APIContext) {
}
}
if err := user_model.AddEmailAddresses(emails); err != nil {
if err := user_model.AddEmailAddresses(ctx, emails); err != nil {
if user_model.IsErrEmailAlreadyUsed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
@@ -129,7 +129,7 @@ func DeleteEmail(ctx *context.APIContext) {
}
}
if err := user_model.DeleteEmailAddresses(emails); err != nil {
if err := user_model.DeleteEmailAddresses(ctx, emails); err != nil {
if user_model.IsErrEmailAddressNotExist(err) {
ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
return

View File

@@ -54,7 +54,7 @@ func Search(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
Keyword: ctx.FormTrim("q"),
UID: ctx.FormInt64("uid"),