mirror of
https://github.com/go-gitea/gitea
synced 2025-07-25 19:58:36 +00:00
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:
@@ -19,9 +19,9 @@ func ToAPIComment(ctx context.Context, repo *repo_model.Repository, c *issues_mo
|
||||
return &api.Comment{
|
||||
ID: c.ID,
|
||||
Poster: ToUser(ctx, c.Poster, nil),
|
||||
HTMLURL: c.HTMLURL(),
|
||||
IssueURL: c.IssueURL(),
|
||||
PRURL: c.PRURL(),
|
||||
HTMLURL: c.HTMLURL(ctx),
|
||||
IssueURL: c.IssueURL(ctx),
|
||||
PRURL: c.PRURL(ctx),
|
||||
Body: c.Content,
|
||||
Attachments: ToAPIAttachments(repo, c.Attachments),
|
||||
Created: c.CreatedUnix.AsTime(),
|
||||
@@ -37,19 +37,19 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu
|
||||
return nil
|
||||
}
|
||||
|
||||
err = c.LoadAssigneeUserAndTeam()
|
||||
err = c.LoadAssigneeUserAndTeam(ctx)
|
||||
if err != nil {
|
||||
log.Error("LoadAssigneeUserAndTeam: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
err = c.LoadResolveDoer()
|
||||
err = c.LoadResolveDoer(ctx)
|
||||
if err != nil {
|
||||
log.Error("LoadResolveDoer: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
err = c.LoadDepIssueDetails()
|
||||
err = c.LoadDepIssueDetails(ctx)
|
||||
if err != nil {
|
||||
log.Error("LoadDepIssueDetails: %v", err)
|
||||
return nil
|
||||
@@ -61,7 +61,7 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu
|
||||
return nil
|
||||
}
|
||||
|
||||
err = c.LoadLabel()
|
||||
err = c.LoadLabel(ctx)
|
||||
if err != nil {
|
||||
log.Error("LoadLabel: %v", err)
|
||||
return nil
|
||||
@@ -82,9 +82,9 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu
|
||||
ID: c.ID,
|
||||
Type: c.Type.String(),
|
||||
Poster: ToUser(ctx, c.Poster, nil),
|
||||
HTMLURL: c.HTMLURL(),
|
||||
IssueURL: c.IssueURL(),
|
||||
PRURL: c.PRURL(),
|
||||
HTMLURL: c.HTMLURL(ctx),
|
||||
IssueURL: c.IssueURL(ctx),
|
||||
PRURL: c.PRURL(ctx),
|
||||
Body: c.Content,
|
||||
Created: c.CreatedUnix.AsTime(),
|
||||
Updated: c.UpdatedUnix.AsTime(),
|
||||
|
@@ -4,17 +4,17 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
access_model "code.gitea.io/gitea/models/perm/access"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToNotificationThread convert a Notification to api.NotificationThread
|
||||
func ToNotificationThread(n *activities_model.Notification) *api.NotificationThread {
|
||||
func ToNotificationThread(ctx context.Context, n *activities_model.Notification) *api.NotificationThread {
|
||||
result := &api.NotificationThread{
|
||||
ID: n.ID,
|
||||
Unread: !(n.Status == activities_model.NotificationStatusRead || n.Status == activities_model.NotificationStatusPinned),
|
||||
@@ -25,7 +25,7 @@ func ToNotificationThread(n *activities_model.Notification) *api.NotificationThr
|
||||
|
||||
// since user only get notifications when he has access to use minimal access mode
|
||||
if n.Repository != nil {
|
||||
result.Repository = ToRepo(db.DefaultContext, n.Repository, access_model.Permission{AccessMode: perm.AccessModeRead})
|
||||
result.Repository = ToRepo(ctx, n.Repository, access_model.Permission{AccessMode: perm.AccessModeRead})
|
||||
|
||||
// This permission is not correct and we should not be reporting it
|
||||
for repository := result.Repository; repository != nil; repository = repository.Parent {
|
||||
@@ -44,8 +44,8 @@ func ToNotificationThread(n *activities_model.Notification) *api.NotificationThr
|
||||
result.Subject.State = n.Issue.State()
|
||||
comment, err := n.Issue.GetLastComment()
|
||||
if err == nil && comment != nil {
|
||||
result.Subject.LatestCommentURL = comment.APIURL()
|
||||
result.Subject.LatestCommentHTMLURL = comment.HTMLURL()
|
||||
result.Subject.LatestCommentURL = comment.APIURL(ctx)
|
||||
result.Subject.LatestCommentHTMLURL = comment.HTMLURL(ctx)
|
||||
}
|
||||
}
|
||||
case activities_model.NotificationSourcePullRequest:
|
||||
@@ -57,8 +57,8 @@ func ToNotificationThread(n *activities_model.Notification) *api.NotificationThr
|
||||
result.Subject.State = n.Issue.State()
|
||||
comment, err := n.Issue.GetLastComment()
|
||||
if err == nil && comment != nil {
|
||||
result.Subject.LatestCommentURL = comment.APIURL()
|
||||
result.Subject.LatestCommentHTMLURL = comment.HTMLURL()
|
||||
result.Subject.LatestCommentURL = comment.APIURL(ctx)
|
||||
result.Subject.LatestCommentHTMLURL = comment.HTMLURL(ctx)
|
||||
}
|
||||
|
||||
pr, _ := n.Issue.GetPullRequest()
|
||||
@@ -88,10 +88,10 @@ func ToNotificationThread(n *activities_model.Notification) *api.NotificationThr
|
||||
}
|
||||
|
||||
// ToNotifications convert list of Notification to api.NotificationThread list
|
||||
func ToNotifications(nl activities_model.NotificationList) []*api.NotificationThread {
|
||||
func ToNotifications(ctx context.Context, nl activities_model.NotificationList) []*api.NotificationThread {
|
||||
result := make([]*api.NotificationThread, 0, len(nl))
|
||||
for _, n := range nl {
|
||||
result = append(result, ToNotificationThread(n))
|
||||
result = append(result, ToNotificationThread(ctx, n))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
@@ -36,10 +36,10 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.
|
||||
Stale: r.Stale,
|
||||
Official: r.Official,
|
||||
Dismissed: r.Dismissed,
|
||||
CodeCommentsCount: r.GetCodeCommentsCount(),
|
||||
CodeCommentsCount: r.GetCodeCommentsCount(ctx),
|
||||
Submitted: r.CreatedUnix.AsTime(),
|
||||
Updated: r.UpdatedUnix.AsTime(),
|
||||
HTMLURL: r.HTMLURL(),
|
||||
HTMLURL: r.HTMLURL(ctx),
|
||||
HTMLPullURL: r.Issue.HTMLURL(),
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, d
|
||||
CommitID: comment.CommitSHA,
|
||||
OrigCommitID: comment.OldRef,
|
||||
DiffHunk: patch2diff(comment.Patch),
|
||||
HTMLURL: comment.HTMLURL(),
|
||||
HTMLURL: comment.HTMLURL(ctx),
|
||||
HTMLPullURL: review.Issue.HTMLURL(),
|
||||
}
|
||||
|
||||
|
@@ -135,7 +135,7 @@ func registerDeleteOldActions() {
|
||||
OlderThan: 365 * 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
olderThanConfig := config.(*OlderThanConfig)
|
||||
return activities_model.DeleteOldActions(olderThanConfig.OlderThan)
|
||||
return activities_model.DeleteOldActions(ctx, olderThanConfig.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
|
@@ -82,11 +82,11 @@ func UpdateExternalUser(user *user_model.User, gothUser goth.User) error {
|
||||
|
||||
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
|
||||
func UpdateMigrationsByType(ctx context.Context, tp structs.GitServiceType, externalUserID string, userID int64) error {
|
||||
if err := issues_model.UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
|
||||
if err := issues_model.UpdateIssuesMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := issues_model.UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
|
||||
if err := issues_model.UpdateCommentsMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -94,8 +94,8 @@ func UpdateMigrationsByType(ctx context.Context, tp structs.GitServiceType, exte
|
||||
return err
|
||||
}
|
||||
|
||||
if err := issues_model.UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
|
||||
if err := issues_model.UpdateReactionsMigrationsByType(ctx, tp, externalUserID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
return issues_model.UpdateReviewsMigrationsByType(tp, externalUserID, userID)
|
||||
return issues_model.UpdateReviewsMigrationsByType(ctx, tp, externalUserID, userID)
|
||||
}
|
||||
|
@@ -265,7 +265,7 @@ func (a *actionNotifier) PullRequestReview(ctx context.Context, pr *issues_model
|
||||
actions = append(actions, action)
|
||||
}
|
||||
|
||||
if err := activities_model.NotifyWatchersActions(actions); err != nil {
|
||||
if err := activities_model.NotifyWatchersActions(ctx, actions); err != nil {
|
||||
log.Error("notify watchers '%d/%d': %v", review.Reviewer.ID, review.Issue.RepoID, err)
|
||||
}
|
||||
}
|
||||
|
@@ -84,7 +84,7 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, doer *user_mode
|
||||
}
|
||||
}
|
||||
|
||||
if err := issues_model.UpdateComment(c, doer); err != nil {
|
||||
if err := issues_model.UpdateComment(ctx, c, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@@ -15,7 +15,7 @@ import (
|
||||
func ChangeContent(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, content string) (err error) {
|
||||
oldContent := issue.Content
|
||||
|
||||
if err := issues_model.ChangeIssueContent(issue, doer, content); err != nil {
|
||||
if err := issues_model.ChangeIssueContent(ctx, issue, doer, content); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@@ -22,7 +22,7 @@ import (
|
||||
|
||||
// NewIssue creates new issue with labels for repository.
|
||||
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
|
||||
if err := issues_model.NewIssue(repo, issue, labelIDs, uuids); err != nil {
|
||||
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func ChangeIssueRef(ctx context.Context, issue *issues_model.Issue, doer *user_m
|
||||
oldRef := issue.Ref
|
||||
issue.Ref = ref
|
||||
|
||||
if err := issues_model.ChangeIssueRef(issue, doer, oldRef); err != nil {
|
||||
if err := issues_model.ChangeIssueRef(ctx, issue, doer, oldRef); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@@ -510,7 +510,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
||||
if len(cms) == 0 {
|
||||
return nil
|
||||
}
|
||||
return issues_model.InsertIssueComments(cms)
|
||||
return issues_model.InsertIssueComments(g.ctx, cms)
|
||||
}
|
||||
|
||||
// CreatePullRequests creates pull requests
|
||||
@@ -917,7 +917,7 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
|
||||
}
|
||||
}
|
||||
|
||||
return issues_model.InsertReviews(cms)
|
||||
return issues_model.InsertReviews(g.ctx, cms)
|
||||
}
|
||||
|
||||
// Rollback when migrating failed, this will rollback all the changes.
|
||||
|
@@ -46,7 +46,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
|
||||
var referenceID int64
|
||||
|
||||
if m, ok := bean.(*repo_model.Mirror); ok {
|
||||
if m.GetRepository() == nil {
|
||||
if m.GetRepository(ctx) == nil {
|
||||
log.Error("Disconnected mirror found: %d", m.ID)
|
||||
return nil
|
||||
}
|
||||
@@ -90,7 +90,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
|
||||
|
||||
pullMirrorsRequested := 0
|
||||
if pullLimit != 0 {
|
||||
if err := repo_model.MirrorsIterate(pullLimit, func(idx int, bean any) error {
|
||||
if err := repo_model.MirrorsIterate(ctx, pullLimit, func(idx int, bean any) error {
|
||||
if err := handler(idx, bean); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ const gitShortEmptySha = "0000000"
|
||||
// UpdateAddress writes new address to Git repository and database
|
||||
func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error {
|
||||
remoteName := m.GetRemoteName()
|
||||
repoPath := m.GetRepository().RepoPath()
|
||||
repoPath := m.GetRepository(ctx).RepoPath()
|
||||
// Remove old remote
|
||||
_, _, err := git.NewCommand(ctx, "remote", "rm").AddDynamicArguments(remoteName).RunStdString(&git.RunOpts{Dir: repoPath})
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
|
||||
@@ -428,7 +428,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
|
||||
log.Error("SyncMirrors [repo_id: %v]: unable to GetMirrorByRepoID: %v", repoID, err)
|
||||
return false
|
||||
}
|
||||
_ = m.GetRepository() // force load repository of mirror
|
||||
_ = m.GetRepository(ctx) // force load repository of mirror
|
||||
|
||||
ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing Mirror %s/%s", m.Repo.OwnerName, m.Repo.Name))
|
||||
defer finished()
|
||||
|
@@ -336,7 +336,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
|
||||
}
|
||||
if changed {
|
||||
// Mark old reviews as stale if diff to mergebase has changed
|
||||
if err := issues_model.MarkReviewsAsStale(pr.IssueID); err != nil {
|
||||
if err := issues_model.MarkReviewsAsStale(ctx, pr.IssueID); err != nil {
|
||||
log.Error("MarkReviewsAsStale: %v", err)
|
||||
}
|
||||
|
||||
@@ -351,7 +351,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := issues_model.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil {
|
||||
if err := issues_model.MarkReviewsAsNotStale(ctx, pr.IssueID, newCommitID); err != nil {
|
||||
log.Error("MarkReviewsAsNotStale: %v", err)
|
||||
}
|
||||
divergence, err := GetDiverging(ctx, pr)
|
||||
|
@@ -84,7 +84,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
|
||||
if !pendingReview && replyReviewID != 0 {
|
||||
// It's not part of a review; maybe a reply to a review comment or a single comment.
|
||||
// Check if there are reviews for that line already; if there are, this is a reply
|
||||
if existsReview, err = issues_model.ReviewExists(issue, treePath, line); err != nil {
|
||||
if existsReview, err = issues_model.ReviewExists(ctx, issue, treePath, line); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -288,7 +288,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos
|
||||
}
|
||||
}
|
||||
|
||||
review, comm, err := issues_model.SubmitReview(doer, issue, reviewType, content, commitID, stale, attachmentUUIDs)
|
||||
review, comm, err := issues_model.SubmitReview(ctx, doer, issue, reviewType, content, commitID, stale, attachmentUUIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@@ -52,7 +52,7 @@ func NewNotifier() notify_service.Notifier {
|
||||
|
||||
func handler(items ...issueNotificationOpts) []issueNotificationOpts {
|
||||
for _, opts := range items {
|
||||
if err := activities_model.CreateOrUpdateIssueNotifications(opts.IssueID, opts.CommentID, opts.NotificationAuthorID, opts.ReceiverID); err != nil {
|
||||
if err := activities_model.CreateOrUpdateIssueNotifications(db.DefaultContext, opts.IssueID, opts.CommentID, opts.NotificationAuthorID, opts.ReceiverID); err != nil {
|
||||
log.Error("Was unable to create issue notification: %v", err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user