Skip to content

Commit cc638c6

Browse files
JakobDevGiteaBot
authored andcommitted
More db.DefaultContext refactor (go-gitea#27265)
Part of go-gitea#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.
1 parent 84ee02f commit cc638c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+455
-456
lines changed

models/activities/action.go

+46-47
Original file line numberDiff line numberDiff line change
@@ -208,91 +208,91 @@ func (a *Action) loadRepo(ctx context.Context) {
208208
}
209209

210210
// GetActFullName gets the action's user full name.
211-
func (a *Action) GetActFullName() string {
212-
a.LoadActUser(db.DefaultContext)
211+
func (a *Action) GetActFullName(ctx context.Context) string {
212+
a.LoadActUser(ctx)
213213
return a.ActUser.FullName
214214
}
215215

216216
// GetActUserName gets the action's user name.
217-
func (a *Action) GetActUserName() string {
218-
a.LoadActUser(db.DefaultContext)
217+
func (a *Action) GetActUserName(ctx context.Context) string {
218+
a.LoadActUser(ctx)
219219
return a.ActUser.Name
220220
}
221221

222222
// ShortActUserName gets the action's user name trimmed to max 20
223223
// chars.
224-
func (a *Action) ShortActUserName() string {
225-
return base.EllipsisString(a.GetActUserName(), 20)
224+
func (a *Action) ShortActUserName(ctx context.Context) string {
225+
return base.EllipsisString(a.GetActUserName(ctx), 20)
226226
}
227227

228228
// GetDisplayName gets the action's display name based on DEFAULT_SHOW_FULL_NAME, or falls back to the username if it is blank.
229-
func (a *Action) GetDisplayName() string {
229+
func (a *Action) GetDisplayName(ctx context.Context) string {
230230
if setting.UI.DefaultShowFullName {
231-
trimmedFullName := strings.TrimSpace(a.GetActFullName())
231+
trimmedFullName := strings.TrimSpace(a.GetActFullName(ctx))
232232
if len(trimmedFullName) > 0 {
233233
return trimmedFullName
234234
}
235235
}
236-
return a.ShortActUserName()
236+
return a.ShortActUserName(ctx)
237237
}
238238

239239
// GetDisplayNameTitle gets the action's display name used for the title (tooltip) based on DEFAULT_SHOW_FULL_NAME
240-
func (a *Action) GetDisplayNameTitle() string {
240+
func (a *Action) GetDisplayNameTitle(ctx context.Context) string {
241241
if setting.UI.DefaultShowFullName {
242-
return a.ShortActUserName()
242+
return a.ShortActUserName(ctx)
243243
}
244-
return a.GetActFullName()
244+
return a.GetActFullName(ctx)
245245
}
246246

247247
// GetRepoUserName returns the name of the action repository owner.
248-
func (a *Action) GetRepoUserName() string {
249-
a.loadRepo(db.DefaultContext)
248+
func (a *Action) GetRepoUserName(ctx context.Context) string {
249+
a.loadRepo(ctx)
250250
return a.Repo.OwnerName
251251
}
252252

253253
// ShortRepoUserName returns the name of the action repository owner
254254
// trimmed to max 20 chars.
255-
func (a *Action) ShortRepoUserName() string {
256-
return base.EllipsisString(a.GetRepoUserName(), 20)
255+
func (a *Action) ShortRepoUserName(ctx context.Context) string {
256+
return base.EllipsisString(a.GetRepoUserName(ctx), 20)
257257
}
258258

259259
// GetRepoName returns the name of the action repository.
260-
func (a *Action) GetRepoName() string {
261-
a.loadRepo(db.DefaultContext)
260+
func (a *Action) GetRepoName(ctx context.Context) string {
261+
a.loadRepo(ctx)
262262
return a.Repo.Name
263263
}
264264

265265
// ShortRepoName returns the name of the action repository
266266
// trimmed to max 33 chars.
267-
func (a *Action) ShortRepoName() string {
268-
return base.EllipsisString(a.GetRepoName(), 33)
267+
func (a *Action) ShortRepoName(ctx context.Context) string {
268+
return base.EllipsisString(a.GetRepoName(ctx), 33)
269269
}
270270

271271
// GetRepoPath returns the virtual path to the action repository.
272-
func (a *Action) GetRepoPath() string {
273-
return path.Join(a.GetRepoUserName(), a.GetRepoName())
272+
func (a *Action) GetRepoPath(ctx context.Context) string {
273+
return path.Join(a.GetRepoUserName(ctx), a.GetRepoName(ctx))
274274
}
275275

276276
// ShortRepoPath returns the virtual path to the action repository
277277
// trimmed to max 20 + 1 + 33 chars.
278-
func (a *Action) ShortRepoPath() string {
279-
return path.Join(a.ShortRepoUserName(), a.ShortRepoName())
278+
func (a *Action) ShortRepoPath(ctx context.Context) string {
279+
return path.Join(a.ShortRepoUserName(ctx), a.ShortRepoName(ctx))
280280
}
281281

282282
// GetRepoLink returns relative link to action repository.
283-
func (a *Action) GetRepoLink() string {
283+
func (a *Action) GetRepoLink(ctx context.Context) string {
284284
// path.Join will skip empty strings
285-
return path.Join(setting.AppSubURL, "/", url.PathEscape(a.GetRepoUserName()), url.PathEscape(a.GetRepoName()))
285+
return path.Join(setting.AppSubURL, "/", url.PathEscape(a.GetRepoUserName(ctx)), url.PathEscape(a.GetRepoName(ctx)))
286286
}
287287

288288
// GetRepoAbsoluteLink returns the absolute link to action repository.
289-
func (a *Action) GetRepoAbsoluteLink() string {
290-
return setting.AppURL + url.PathEscape(a.GetRepoUserName()) + "/" + url.PathEscape(a.GetRepoName())
289+
func (a *Action) GetRepoAbsoluteLink(ctx context.Context) string {
290+
return setting.AppURL + url.PathEscape(a.GetRepoUserName(ctx)) + "/" + url.PathEscape(a.GetRepoName(ctx))
291291
}
292292

293293
// GetCommentHTMLURL returns link to action comment.
294-
func (a *Action) GetCommentHTMLURL() string {
295-
return a.getCommentHTMLURL(db.DefaultContext)
294+
func (a *Action) GetCommentHTMLURL(ctx context.Context) string {
295+
return a.getCommentHTMLURL(ctx)
296296
}
297297

298298
func (a *Action) loadComment(ctx context.Context) (err error) {
@@ -309,7 +309,7 @@ func (a *Action) getCommentHTMLURL(ctx context.Context) string {
309309
}
310310
_ = a.loadComment(ctx)
311311
if a.Comment != nil {
312-
return a.Comment.HTMLURL()
312+
return a.Comment.HTMLURL(ctx)
313313
}
314314
if len(a.GetIssueInfos()) == 0 {
315315
return "#"
@@ -334,8 +334,8 @@ func (a *Action) getCommentHTMLURL(ctx context.Context) string {
334334
}
335335

336336
// GetCommentLink returns link to action comment.
337-
func (a *Action) GetCommentLink() string {
338-
return a.getCommentLink(db.DefaultContext)
337+
func (a *Action) GetCommentLink(ctx context.Context) string {
338+
return a.getCommentLink(ctx)
339339
}
340340

341341
func (a *Action) getCommentLink(ctx context.Context) string {
@@ -344,7 +344,7 @@ func (a *Action) getCommentLink(ctx context.Context) string {
344344
}
345345
_ = a.loadComment(ctx)
346346
if a.Comment != nil {
347-
return a.Comment.Link()
347+
return a.Comment.Link(ctx)
348348
}
349349
if len(a.GetIssueInfos()) == 0 {
350350
return "#"
@@ -374,8 +374,8 @@ func (a *Action) GetBranch() string {
374374
}
375375

376376
// GetRefLink returns the action's ref link.
377-
func (a *Action) GetRefLink() string {
378-
return git.RefURL(a.GetRepoLink(), a.RefName)
377+
func (a *Action) GetRefLink(ctx context.Context) string {
378+
return git.RefURL(a.GetRepoLink(ctx), a.RefName)
379379
}
380380

381381
// GetTag returns the action's repository tag.
@@ -399,11 +399,10 @@ func (a *Action) GetIssueInfos() []string {
399399
return strings.SplitN(a.Content, "|", 3)
400400
}
401401

402-
// GetIssueTitle returns the title of first issue associated
403-
// with the action. This function will be invoked in template so keep db.DefaultContext here
404-
func (a *Action) GetIssueTitle() string {
402+
// GetIssueTitle returns the title of first issue associated with the action.
403+
func (a *Action) GetIssueTitle(ctx context.Context) string {
405404
index, _ := strconv.ParseInt(a.GetIssueInfos()[0], 10, 64)
406-
issue, err := issues_model.GetIssueByIndex(db.DefaultContext, a.RepoID, index)
405+
issue, err := issues_model.GetIssueByIndex(ctx, a.RepoID, index)
407406
if err != nil {
408407
log.Error("GetIssueByIndex: %v", err)
409408
return "500 when get issue"
@@ -442,7 +441,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
442441
return nil, 0, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
443442
}
444443

445-
cond, err := activityQueryCondition(opts)
444+
cond, err := activityQueryCondition(ctx, opts)
446445
if err != nil {
447446
return nil, 0, err
448447
}
@@ -473,11 +472,11 @@ func ActivityReadable(user, doer *user_model.User) bool {
473472
doer != nil && (doer.IsAdmin || user.ID == doer.ID)
474473
}
475474

476-
func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
475+
func activityQueryCondition(ctx context.Context, opts GetFeedsOptions) (builder.Cond, error) {
477476
cond := builder.NewCond()
478477

479478
if opts.RequestedTeam != nil && opts.RequestedUser == nil {
480-
org, err := user_model.GetUserByID(db.DefaultContext, opts.RequestedTeam.OrgID)
479+
org, err := user_model.GetUserByID(ctx, opts.RequestedTeam.OrgID)
481480
if err != nil {
482481
return nil, err
483482
}
@@ -564,12 +563,12 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
564563
}
565564

566565
// DeleteOldActions deletes all old actions from database.
567-
func DeleteOldActions(olderThan time.Duration) (err error) {
566+
func DeleteOldActions(ctx context.Context, olderThan time.Duration) (err error) {
568567
if olderThan <= 0 {
569568
return nil
570569
}
571570

572-
_, err = db.GetEngine(db.DefaultContext).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Action{})
571+
_, err = db.GetEngine(ctx).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Action{})
573572
return err
574573
}
575574

@@ -679,8 +678,8 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
679678
}
680679

681680
// NotifyWatchersActions creates batch of actions for every watcher.
682-
func NotifyWatchersActions(acts []*Action) error {
683-
ctx, committer, err := db.TxContext(db.DefaultContext)
681+
func NotifyWatchersActions(ctx context.Context, acts []*Action) error {
682+
ctx, committer, err := db.TxContext(ctx)
684683
if err != nil {
685684
return err
686685
}

models/activities/action_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestAction_GetRepoPath(t *testing.T) {
2424
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2525
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
2626
action := &activities_model.Action{RepoID: repo.ID}
27-
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
27+
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath(db.DefaultContext))
2828
}
2929

3030
func TestAction_GetRepoLink(t *testing.T) {
@@ -35,9 +35,9 @@ func TestAction_GetRepoLink(t *testing.T) {
3535
action := &activities_model.Action{RepoID: repo.ID, CommentID: comment.ID}
3636
setting.AppSubURL = "/suburl"
3737
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
38-
assert.Equal(t, expected, action.GetRepoLink())
39-
assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink())
40-
assert.Equal(t, comment.HTMLURL(), action.GetCommentHTMLURL())
38+
assert.Equal(t, expected, action.GetRepoLink(db.DefaultContext))
39+
assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink(db.DefaultContext))
40+
assert.Equal(t, comment.HTMLURL(db.DefaultContext), action.GetCommentHTMLURL(db.DefaultContext))
4141
}
4242

4343
func TestGetFeeds(t *testing.T) {

models/activities/notification.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_mo
175175
// CreateOrUpdateIssueNotifications creates an issue notification
176176
// for each watcher, or updates it if already exists
177177
// receiverID > 0 just send to receiver, else send to all watcher
178-
func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error {
179-
ctx, committer, err := db.TxContext(db.DefaultContext)
178+
func CreateOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error {
179+
ctx, committer, err := db.TxContext(ctx)
180180
if err != nil {
181181
return err
182182
}
@@ -435,21 +435,21 @@ func (n *Notification) loadUser(ctx context.Context) (err error) {
435435
}
436436

437437
// GetRepo returns the repo of the notification
438-
func (n *Notification) GetRepo() (*repo_model.Repository, error) {
439-
return n.Repository, n.loadRepo(db.DefaultContext)
438+
func (n *Notification) GetRepo(ctx context.Context) (*repo_model.Repository, error) {
439+
return n.Repository, n.loadRepo(ctx)
440440
}
441441

442442
// GetIssue returns the issue of the notification
443-
func (n *Notification) GetIssue() (*issues_model.Issue, error) {
444-
return n.Issue, n.loadIssue(db.DefaultContext)
443+
func (n *Notification) GetIssue(ctx context.Context) (*issues_model.Issue, error) {
444+
return n.Issue, n.loadIssue(ctx)
445445
}
446446

447447
// HTMLURL formats a URL-string to the notification
448-
func (n *Notification) HTMLURL() string {
448+
func (n *Notification) HTMLURL(ctx context.Context) string {
449449
switch n.Source {
450450
case NotificationSourceIssue, NotificationSourcePullRequest:
451451
if n.Comment != nil {
452-
return n.Comment.HTMLURL()
452+
return n.Comment.HTMLURL(ctx)
453453
}
454454
return n.Issue.HTMLURL()
455455
case NotificationSourceCommit:
@@ -461,11 +461,11 @@ func (n *Notification) HTMLURL() string {
461461
}
462462

463463
// Link formats a relative URL-string to the notification
464-
func (n *Notification) Link() string {
464+
func (n *Notification) Link(ctx context.Context) string {
465465
switch n.Source {
466466
case NotificationSourceIssue, NotificationSourcePullRequest:
467467
if n.Comment != nil {
468-
return n.Comment.Link()
468+
return n.Comment.Link(ctx)
469469
}
470470
return n.Issue.Link()
471471
case NotificationSourceCommit:
@@ -733,12 +733,12 @@ type UserIDCount struct {
733733
}
734734

735735
// GetUIDsAndNotificationCounts between the two provided times
736-
func GetUIDsAndNotificationCounts(since, until timeutil.TimeStamp) ([]UserIDCount, error) {
736+
func GetUIDsAndNotificationCounts(ctx context.Context, since, until timeutil.TimeStamp) ([]UserIDCount, error) {
737737
sql := `SELECT user_id, count(*) AS count FROM notification ` +
738738
`WHERE user_id IN (SELECT user_id FROM notification WHERE updated_unix >= ? AND ` +
739739
`updated_unix < ?) AND status = ? GROUP BY user_id`
740740
var res []UserIDCount
741-
return res, db.GetEngine(db.DefaultContext).SQL(sql, since, until, NotificationStatusUnread).Find(&res)
741+
return res, db.GetEngine(ctx).SQL(sql, since, until, NotificationStatusUnread).Find(&res)
742742
}
743743

744744
// SetIssueReadBy sets issue to be read by given user.

models/activities/notification_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestCreateOrUpdateIssueNotifications(t *testing.T) {
2020
assert.NoError(t, unittest.PrepareTestDatabase())
2121
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
2222

23-
assert.NoError(t, activities_model.CreateOrUpdateIssueNotifications(issue.ID, 0, 2, 0))
23+
assert.NoError(t, activities_model.CreateOrUpdateIssueNotifications(db.DefaultContext, issue.ID, 0, 2, 0))
2424

2525
// User 9 is inactive, thus notifications for user 1 and 4 are created
2626
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{UserID: 1, IssueID: issue.ID})
@@ -50,7 +50,7 @@ func TestNotificationsForUser(t *testing.T) {
5050
func TestNotification_GetRepo(t *testing.T) {
5151
assert.NoError(t, unittest.PrepareTestDatabase())
5252
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{RepoID: 1})
53-
repo, err := notf.GetRepo()
53+
repo, err := notf.GetRepo(db.DefaultContext)
5454
assert.NoError(t, err)
5555
assert.Equal(t, repo, notf.Repository)
5656
assert.EqualValues(t, notf.RepoID, repo.ID)
@@ -59,7 +59,7 @@ func TestNotification_GetRepo(t *testing.T) {
5959
func TestNotification_GetIssue(t *testing.T) {
6060
assert.NoError(t, unittest.PrepareTestDatabase())
6161
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{RepoID: 1})
62-
issue, err := notf.GetIssue()
62+
issue, err := notf.GetIssue(db.DefaultContext)
6363
assert.NoError(t, err)
6464
assert.Equal(t, issue, notf.Issue)
6565
assert.EqualValues(t, notf.IssueID, issue.ID)

models/activities/user_heatmap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organi
4747
groupByName = groupBy
4848
}
4949

50-
cond, err := activityQueryCondition(GetFeedsOptions{
50+
cond, err := activityQueryCondition(ctx, GetFeedsOptions{
5151
RequestedUser: user,
5252
RequestedTeam: team,
5353
Actor: doer,

models/git/commit_status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ func ConvertFromGitCommit(ctx context.Context, commits []*git.Commit, repo *repo
514514
user_model.ValidateCommitsWithEmails(ctx, commits),
515515
repo.GetTrustModel(),
516516
func(user *user_model.User) (bool, error) {
517-
return repo_model.IsOwnerMemberCollaborator(repo, user.ID)
517+
return repo_model.IsOwnerMemberCollaborator(ctx, repo, user.ID)
518518
},
519519
),
520520
repo,

0 commit comments

Comments
 (0)