More db.DefaultContext
refactor (#27265)
Part of #27065 This PR touches functions used in templates. As templates are not static typed, errors are harder to find, but I hope I catch it all. I think some tests from other persons do not hurt.
This commit is contained in:
parent
3945c26722
commit
cf0df023be
66 changed files with 455 additions and 456 deletions
|
@ -371,42 +371,42 @@ func (c *Comment) AfterDelete(ctx context.Context) {
|
|||
}
|
||||
|
||||
// HTMLURL formats a URL-string to the issue-comment
|
||||
func (c *Comment) HTMLURL() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) HTMLURL(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
}
|
||||
return c.Issue.HTMLURL() + c.hashLink()
|
||||
return c.Issue.HTMLURL() + c.hashLink(ctx)
|
||||
}
|
||||
|
||||
// Link formats a relative URL-string to the issue-comment
|
||||
func (c *Comment) Link() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) Link(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
}
|
||||
return c.Issue.Link() + c.hashLink()
|
||||
return c.Issue.Link() + c.hashLink(ctx)
|
||||
}
|
||||
|
||||
func (c *Comment) hashLink() string {
|
||||
func (c *Comment) hashLink(ctx context.Context) string {
|
||||
if c.Type == CommentTypeCode {
|
||||
if c.ReviewID == 0 {
|
||||
return "/files#" + c.HashTag()
|
||||
}
|
||||
if c.Review == nil {
|
||||
if err := c.LoadReview(); err != nil {
|
||||
if err := c.LoadReview(ctx); err != nil {
|
||||
log.Warn("LoadReview(%d): %v", c.ReviewID, err)
|
||||
return "/files#" + c.HashTag()
|
||||
}
|
||||
|
@ -419,13 +419,13 @@ func (c *Comment) hashLink() string {
|
|||
}
|
||||
|
||||
// APIURL formats a API-string to the issue-comment
|
||||
func (c *Comment) APIURL() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) APIURL(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
|
@ -435,8 +435,8 @@ func (c *Comment) APIURL() string {
|
|||
}
|
||||
|
||||
// IssueURL formats a URL-string to the issue
|
||||
func (c *Comment) IssueURL() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) IssueURL(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
|
@ -446,7 +446,7 @@ func (c *Comment) IssueURL() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
|
@ -455,14 +455,14 @@ func (c *Comment) IssueURL() string {
|
|||
}
|
||||
|
||||
// PRURL formats a URL-string to the pull-request
|
||||
func (c *Comment) PRURL() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) PRURL(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
|
@ -490,9 +490,9 @@ func (c *Comment) EventTag() string {
|
|||
}
|
||||
|
||||
// LoadLabel if comment.Type is CommentTypeLabel, then load Label
|
||||
func (c *Comment) LoadLabel() error {
|
||||
func (c *Comment) LoadLabel(ctx context.Context) error {
|
||||
var label Label
|
||||
has, err := db.GetEngine(db.DefaultContext).ID(c.LabelID).Get(&label)
|
||||
has, err := db.GetEngine(ctx).ID(c.LabelID).Get(&label)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
|
@ -506,10 +506,10 @@ func (c *Comment) LoadLabel() error {
|
|||
}
|
||||
|
||||
// LoadProject if comment.Type is CommentTypeProject, then load project.
|
||||
func (c *Comment) LoadProject() error {
|
||||
func (c *Comment) LoadProject(ctx context.Context) error {
|
||||
if c.OldProjectID > 0 {
|
||||
var oldProject project_model.Project
|
||||
has, err := db.GetEngine(db.DefaultContext).ID(c.OldProjectID).Get(&oldProject)
|
||||
has, err := db.GetEngine(ctx).ID(c.OldProjectID).Get(&oldProject)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
|
@ -519,7 +519,7 @@ func (c *Comment) LoadProject() error {
|
|||
|
||||
if c.ProjectID > 0 {
|
||||
var project project_model.Project
|
||||
has, err := db.GetEngine(db.DefaultContext).ID(c.ProjectID).Get(&project)
|
||||
has, err := db.GetEngine(ctx).ID(c.ProjectID).Get(&project)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
|
@ -569,8 +569,8 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// UpdateAttachments update attachments by UUIDs for the comment
|
||||
func (c *Comment) UpdateAttachments(uuids []string) error {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -591,11 +591,11 @@ func (c *Comment) UpdateAttachments(uuids []string) error {
|
|||
}
|
||||
|
||||
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
|
||||
func (c *Comment) LoadAssigneeUserAndTeam() error {
|
||||
func (c *Comment) LoadAssigneeUserAndTeam(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
if c.AssigneeID > 0 && c.Assignee == nil {
|
||||
c.Assignee, err = user_model.GetUserByID(db.DefaultContext, c.AssigneeID)
|
||||
c.Assignee, err = user_model.GetUserByID(ctx, c.AssigneeID)
|
||||
if err != nil {
|
||||
if !user_model.IsErrUserNotExist(err) {
|
||||
return err
|
||||
|
@ -603,20 +603,20 @@ func (c *Comment) LoadAssigneeUserAndTeam() error {
|
|||
c.Assignee = user_model.NewGhostUser()
|
||||
}
|
||||
} else if c.AssigneeTeamID > 0 && c.AssigneeTeam == nil {
|
||||
if err = c.LoadIssue(db.DefaultContext); err != nil {
|
||||
if err = c.LoadIssue(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = c.Issue.LoadRepo(db.DefaultContext); err != nil {
|
||||
if err = c.Issue.LoadRepo(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = c.Issue.Repo.LoadOwner(db.DefaultContext); err != nil {
|
||||
if err = c.Issue.Repo.LoadOwner(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.Issue.Repo.Owner.IsOrganization() {
|
||||
c.AssigneeTeam, err = organization.GetTeamByID(db.DefaultContext, c.AssigneeTeamID)
|
||||
c.AssigneeTeam, err = organization.GetTeamByID(ctx, c.AssigneeTeamID)
|
||||
if err != nil && !organization.IsErrTeamNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
@ -626,11 +626,11 @@ func (c *Comment) LoadAssigneeUserAndTeam() error {
|
|||
}
|
||||
|
||||
// LoadResolveDoer if comment.Type is CommentTypeCode and ResolveDoerID not zero, then load resolveDoer
|
||||
func (c *Comment) LoadResolveDoer() (err error) {
|
||||
func (c *Comment) LoadResolveDoer(ctx context.Context) (err error) {
|
||||
if c.ResolveDoerID == 0 || c.Type != CommentTypeCode {
|
||||
return nil
|
||||
}
|
||||
c.ResolveDoer, err = user_model.GetUserByID(db.DefaultContext, c.ResolveDoerID)
|
||||
c.ResolveDoer, err = user_model.GetUserByID(ctx, c.ResolveDoerID)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
c.ResolveDoer = user_model.NewGhostUser()
|
||||
|
@ -646,11 +646,11 @@ func (c *Comment) IsResolved() bool {
|
|||
}
|
||||
|
||||
// LoadDepIssueDetails loads Dependent Issue Details
|
||||
func (c *Comment) LoadDepIssueDetails() (err error) {
|
||||
func (c *Comment) LoadDepIssueDetails(ctx context.Context) (err error) {
|
||||
if c.DependentIssueID <= 0 || c.DependentIssue != nil {
|
||||
return nil
|
||||
}
|
||||
c.DependentIssue, err = GetIssueByID(db.DefaultContext, c.DependentIssueID)
|
||||
c.DependentIssue, err = GetIssueByID(ctx, c.DependentIssueID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -683,8 +683,8 @@ func (c *Comment) loadReactions(ctx context.Context, repo *repo_model.Repository
|
|||
}
|
||||
|
||||
// LoadReactions loads comment reactions
|
||||
func (c *Comment) LoadReactions(repo *repo_model.Repository) error {
|
||||
return c.loadReactions(db.DefaultContext, repo)
|
||||
func (c *Comment) LoadReactions(ctx context.Context, repo *repo_model.Repository) error {
|
||||
return c.loadReactions(ctx, repo)
|
||||
}
|
||||
|
||||
func (c *Comment) loadReview(ctx context.Context) (err error) {
|
||||
|
@ -698,8 +698,8 @@ func (c *Comment) loadReview(ctx context.Context) (err error) {
|
|||
}
|
||||
|
||||
// LoadReview loads the associated review
|
||||
func (c *Comment) LoadReview() error {
|
||||
return c.loadReview(db.DefaultContext)
|
||||
func (c *Comment) LoadReview(ctx context.Context) error {
|
||||
return c.loadReview(ctx)
|
||||
}
|
||||
|
||||
// DiffSide returns "previous" if Comment.Line is a LOC of the previous changes and "proposed" if it is a LOC of the proposed changes.
|
||||
|
@ -719,13 +719,13 @@ func (c *Comment) UnsignedLine() uint64 {
|
|||
}
|
||||
|
||||
// CodeCommentLink returns the url to a comment in code
|
||||
func (c *Comment) CodeCommentLink() string {
|
||||
err := c.LoadIssue(db.DefaultContext)
|
||||
func (c *Comment) CodeCommentLink(ctx context.Context) string {
|
||||
err := c.LoadIssue(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.LoadRepo(db.DefaultContext)
|
||||
err = c.Issue.LoadRepo(ctx)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
|
@ -1074,8 +1074,8 @@ func FindComments(ctx context.Context, opts *FindCommentsOptions) (CommentList,
|
|||
}
|
||||
|
||||
// CountComments count all comments according options by ignoring pagination
|
||||
func CountComments(opts *FindCommentsOptions) (int64, error) {
|
||||
sess := db.GetEngine(db.DefaultContext).Where(opts.ToConds())
|
||||
func CountComments(ctx context.Context, opts *FindCommentsOptions) (int64, error) {
|
||||
sess := db.GetEngine(ctx).Where(opts.ToConds())
|
||||
if opts.RepoID > 0 {
|
||||
sess.Join("INNER", "issue", "issue.id = comment.issue_id")
|
||||
}
|
||||
|
@ -1089,8 +1089,8 @@ func UpdateCommentInvalidate(ctx context.Context, c *Comment) error {
|
|||
}
|
||||
|
||||
// UpdateComment updates information of comment.
|
||||
func UpdateComment(c *Comment, doer *user_model.User) error {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
func UpdateComment(ctx context.Context, c *Comment, doer *user_model.User) error {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1147,8 +1147,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
|
|||
}
|
||||
|
||||
// UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id
|
||||
func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
|
||||
_, err := db.GetEngine(db.DefaultContext).Table("comment").
|
||||
func UpdateCommentsMigrationsByType(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
|
||||
_, err := db.GetEngine(ctx).Table("comment").
|
||||
Where(builder.In("issue_id",
|
||||
builder.Select("issue.id").
|
||||
From("issue").
|
||||
|
@ -1250,7 +1250,7 @@ func (c *Comment) HasOriginalAuthor() bool {
|
|||
}
|
||||
|
||||
// InsertIssueComments inserts many comments of issues.
|
||||
func InsertIssueComments(comments []*Comment) error {
|
||||
func InsertIssueComments(ctx context.Context, comments []*Comment) error {
|
||||
if len(comments) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -1260,7 +1260,7 @@ func InsertIssueComments(comments []*Comment) error {
|
|||
issueIDs.Add(comment.IssueID)
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue