mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	more context for models (#19511)
make more usage of context, to have more db transaction in one session (make diff of #9307 smaller)
This commit is contained in:
		@@ -183,7 +183,7 @@ func repoAssignment() func(ctx *context.APIContext) {
 | 
			
		||||
		repo.Owner = owner
 | 
			
		||||
		ctx.Repo.Repository = repo
 | 
			
		||||
 | 
			
		||||
		ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.Doer)
 | 
			
		||||
		ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx, repo, ctx.Doer)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
 | 
			
		||||
			return
 | 
			
		||||
 
 | 
			
		||||
@@ -498,7 +498,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
 | 
			
		||||
		BlockOnOutdatedBranch:         form.BlockOnOutdatedBranch,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
 | 
			
		||||
	err = models.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
 | 
			
		||||
		UserIDs:          whitelistUsers,
 | 
			
		||||
		TeamIDs:          whitelistTeams,
 | 
			
		||||
		MergeUserIDs:     mergeWhitelistUsers,
 | 
			
		||||
@@ -733,7 +733,7 @@ func EditBranchProtection(ctx *context.APIContext) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
 | 
			
		||||
	err = models.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
 | 
			
		||||
		UserIDs:          whitelistUsers,
 | 
			
		||||
		TeamIDs:          whitelistTeams,
 | 
			
		||||
		MergeUserIDs:     mergeWhitelistUsers,
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
package repo
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	stdCtx "context"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
@@ -183,7 +184,7 @@ func ListIssueCommentsAndTimeline(ctx *context.APIContext) {
 | 
			
		||||
 | 
			
		||||
	var apiComments []*api.TimelineComment
 | 
			
		||||
	for _, comment := range comments {
 | 
			
		||||
		if comment.Type != models.CommentTypeCode && isXRefCommentAccessible(ctx.Doer, comment, issue.RepoID) {
 | 
			
		||||
		if comment.Type != models.CommentTypeCode && isXRefCommentAccessible(ctx, ctx.Doer, comment, issue.RepoID) {
 | 
			
		||||
			comment.Issue = issue
 | 
			
		||||
			apiComments = append(apiComments, convert.ToTimelineComment(comment, ctx.Doer))
 | 
			
		||||
		}
 | 
			
		||||
@@ -193,16 +194,16 @@ func ListIssueCommentsAndTimeline(ctx *context.APIContext) {
 | 
			
		||||
	ctx.JSON(http.StatusOK, &apiComments)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func isXRefCommentAccessible(user *user_model.User, c *models.Comment, issueRepoID int64) bool {
 | 
			
		||||
func isXRefCommentAccessible(ctx stdCtx.Context, user *user_model.User, c *models.Comment, issueRepoID int64) bool {
 | 
			
		||||
	// Remove comments that the user has no permissions to see
 | 
			
		||||
	if models.CommentTypeIsRef(c.Type) && c.RefRepoID != issueRepoID && c.RefRepoID != 0 {
 | 
			
		||||
		var err error
 | 
			
		||||
		// Set RefRepo for description in template
 | 
			
		||||
		c.RefRepo, err = repo_model.GetRepositoryByID(c.RefRepoID)
 | 
			
		||||
		c.RefRepo, err = repo_model.GetRepositoryByIDCtx(ctx, c.RefRepoID)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
		perm, err := models.GetUserRepoPermission(c.RefRepo, user)
 | 
			
		||||
		perm, err := models.GetUserRepoPermission(ctx, c.RefRepo, user)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -111,11 +111,11 @@ func ListPullRequests(ctx *context.APIContext) {
 | 
			
		||||
			ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if err = prs[i].LoadBaseRepo(); err != nil {
 | 
			
		||||
		if err = prs[i].LoadBaseRepoCtx(ctx); err != nil {
 | 
			
		||||
			ctx.Error(http.StatusInternalServerError, "LoadBaseRepo", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if err = prs[i].LoadHeadRepo(); err != nil {
 | 
			
		||||
		if err = prs[i].LoadHeadRepoCtx(ctx); err != nil {
 | 
			
		||||
			ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
@@ -167,11 +167,11 @@ func GetPullRequest(ctx *context.APIContext) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = pr.LoadBaseRepo(); err != nil {
 | 
			
		||||
	if err = pr.LoadBaseRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "LoadBaseRepo", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if err = pr.LoadHeadRepo(); err != nil {
 | 
			
		||||
	if err = pr.LoadHeadRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@@ -724,7 +724,7 @@ func MergePullRequest(ctx *context.APIContext) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := pr.LoadHeadRepo(); err != nil {
 | 
			
		||||
	if err := pr.LoadHeadRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@@ -943,7 +943,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// user should have permission to read baseRepo's codes and pulls, NOT headRepo's
 | 
			
		||||
	permBase, err := models.GetUserRepoPermission(baseRepo, ctx.Doer)
 | 
			
		||||
	permBase, err := models.GetUserRepoPermission(ctx, baseRepo, ctx.Doer)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		headGitRepo.Close()
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
 | 
			
		||||
@@ -962,7 +962,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// user should have permission to read headrepo's codes
 | 
			
		||||
	permHead, err := models.GetUserRepoPermission(headRepo, ctx.Doer)
 | 
			
		||||
	permHead, err := models.GetUserRepoPermission(ctx, headRepo, ctx.Doer)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		headGitRepo.Close()
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
 | 
			
		||||
@@ -1063,18 +1063,18 @@ func UpdatePullRequest(ctx *context.APIContext) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = pr.LoadBaseRepo(); err != nil {
 | 
			
		||||
	if err = pr.LoadBaseRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "LoadBaseRepo", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if err = pr.LoadHeadRepo(); err != nil {
 | 
			
		||||
	if err = pr.LoadHeadRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rebase := ctx.FormString("style") == "rebase"
 | 
			
		||||
 | 
			
		||||
	allowedUpdateByMerge, allowedUpdateByRebase, err := pull_service.IsUserAllowedToUpdate(pr, ctx.Doer)
 | 
			
		||||
	allowedUpdateByMerge, allowedUpdateByRebase, err := pull_service.IsUserAllowedToUpdate(ctx, pr, ctx.Doer)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "IsUserAllowedToMerge", err)
 | 
			
		||||
		return
 | 
			
		||||
@@ -1151,7 +1151,7 @@ func GetPullRequestCommits(ctx *context.APIContext) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := pr.LoadBaseRepo(); err != nil {
 | 
			
		||||
	if err := pr.LoadBaseRepoCtx(ctx); err != nil {
 | 
			
		||||
		ctx.InternalServerError(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -664,7 +664,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
 | 
			
		||||
 | 
			
		||||
	reviewers := make([]*user_model.User, 0, len(opts.Reviewers))
 | 
			
		||||
 | 
			
		||||
	permDoer, err := models.GetUserRepoPermission(pr.Issue.Repo, ctx.Doer)
 | 
			
		||||
	permDoer, err := models.GetUserRepoPermission(ctx, pr.Issue.Repo, ctx.Doer)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
 | 
			
		||||
		return
 | 
			
		||||
@@ -687,7 +687,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = issue_service.IsValidReviewRequest(reviewer, ctx.Doer, isAdd, pr.Issue, &permDoer)
 | 
			
		||||
		err = issue_service.IsValidReviewRequest(ctx, reviewer, ctx.Doer, isAdd, pr.Issue, &permDoer)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if models.IsErrNotValidReviewRequest(err) {
 | 
			
		||||
				ctx.Error(http.StatusUnprocessableEntity, "NotValidReviewRequest", err)
 | 
			
		||||
@@ -736,7 +736,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = issue_service.IsValidTeamReviewRequest(teamReviewer, ctx.Doer, isAdd, pr.Issue)
 | 
			
		||||
			err = issue_service.IsValidTeamReviewRequest(ctx, teamReviewer, ctx.Doer, isAdd, pr.Issue)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				if models.IsErrNotValidReviewRequest(err) {
 | 
			
		||||
					ctx.Error(http.StatusUnprocessableEntity, "NotValidReviewRequest", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -555,7 +555,7 @@ func GetByID(ctx *context.APIContext) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	perm, err := models.GetUserRepoPermission(repo, ctx.Doer)
 | 
			
		||||
	perm, err := models.GetUserRepoPermission(ctx, repo, ctx.Doer)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
 | 
			
		||||
		return
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user