Skip to content

Commit 044c754

Browse files
KN4CK3Rdelvhlunny
authored
Add context.Context to more methods (#21546)
This PR adds a context parameter to a bunch of methods. Some helper `xxxCtx()` methods got replaced with the normal name now. Co-authored-by: delvh <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent fefdb7f commit 044c754

File tree

148 files changed

+1408
-1561
lines changed

Some content is hidden

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

148 files changed

+1408
-1561
lines changed

Diff for: cmd/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
727727

728728
log.Trace("Synchronizing repository releases (this may take a while)")
729729
for page := 1; ; page++ {
730-
repos, count, err := repo_model.SearchRepositoryByName(&repo_model.SearchRepoOptions{
730+
repos, count, err := repo_model.SearchRepositoryByName(ctx, &repo_model.SearchRepoOptions{
731731
ListOptions: db.ListOptions{
732732
PageSize: repo_model.RepositoryListDefaultPageSize,
733733
Page: page,

Diff for: models/activities/action.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ func DeleteOldActions(olderThan time.Duration) (err error) {
461461
return err
462462
}
463463

464-
func notifyWatchers(ctx context.Context, actions ...*Action) error {
464+
// NotifyWatchers creates batch of actions for every watcher.
465+
func NotifyWatchers(ctx context.Context, actions ...*Action) error {
465466
var watchers []*repo_model.Watch
466467
var repo *repo_model.Repository
467468
var err error
@@ -565,11 +566,6 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
565566
return nil
566567
}
567568

568-
// NotifyWatchers creates batch of actions for every watcher.
569-
func NotifyWatchers(actions ...*Action) error {
570-
return notifyWatchers(db.DefaultContext, actions...)
571-
}
572-
573569
// NotifyWatchersActions creates batch of actions for every watcher.
574570
func NotifyWatchersActions(acts []*Action) error {
575571
ctx, committer, err := db.TxContext(db.DefaultContext)
@@ -578,7 +574,7 @@ func NotifyWatchersActions(acts []*Action) error {
578574
}
579575
defer committer.Close()
580576
for _, act := range acts {
581-
if err := notifyWatchers(ctx, act); err != nil {
577+
if err := NotifyWatchers(ctx, act); err != nil {
582578
return err
583579
}
584580
}
@@ -603,17 +599,17 @@ func DeleteIssueActions(ctx context.Context, repoID, issueID int64) error {
603599
}
604600

605601
// CountActionCreatedUnixString count actions where created_unix is an empty string
606-
func CountActionCreatedUnixString() (int64, error) {
602+
func CountActionCreatedUnixString(ctx context.Context) (int64, error) {
607603
if setting.Database.UseSQLite3 {
608-
return db.GetEngine(db.DefaultContext).Where(`created_unix = ""`).Count(new(Action))
604+
return db.GetEngine(ctx).Where(`created_unix = ""`).Count(new(Action))
609605
}
610606
return 0, nil
611607
}
612608

613609
// FixActionCreatedUnixString set created_unix to zero if it is an empty string
614-
func FixActionCreatedUnixString() (int64, error) {
610+
func FixActionCreatedUnixString(ctx context.Context) (int64, error) {
615611
if setting.Database.UseSQLite3 {
616-
res, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
612+
res, err := db.GetEngine(ctx).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
617613
if err != nil {
618614
return 0, err
619615
}

Diff for: models/activities/action_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestNotifyWatchers(t *testing.T) {
188188
RepoID: 1,
189189
OpType: activities_model.ActionStarRepo,
190190
}
191-
assert.NoError(t, activities_model.NotifyWatchers(action))
191+
assert.NoError(t, activities_model.NotifyWatchers(db.DefaultContext, action))
192192

193193
// One watchers are inactive, thus action is only created for user 8, 1, 4, 11
194194
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
@@ -256,17 +256,17 @@ func TestConsistencyUpdateAction(t *testing.T) {
256256
//
257257
// Get rid of incorrectly set created_unix
258258
//
259-
count, err := activities_model.CountActionCreatedUnixString()
259+
count, err := activities_model.CountActionCreatedUnixString(db.DefaultContext)
260260
assert.NoError(t, err)
261261
assert.EqualValues(t, 1, count)
262-
count, err = activities_model.FixActionCreatedUnixString()
262+
count, err = activities_model.FixActionCreatedUnixString(db.DefaultContext)
263263
assert.NoError(t, err)
264264
assert.EqualValues(t, 1, count)
265265

266-
count, err = activities_model.CountActionCreatedUnixString()
266+
count, err = activities_model.CountActionCreatedUnixString(db.DefaultContext)
267267
assert.NoError(t, err)
268268
assert.EqualValues(t, 0, count)
269-
count, err = activities_model.FixActionCreatedUnixString()
269+
count, err = activities_model.FixActionCreatedUnixString(db.DefaultContext)
270270
assert.NoError(t, err)
271271
assert.EqualValues(t, 0, count)
272272

Diff for: models/activities/notification.go

+40-56
Original file line numberDiff line numberDiff line change
@@ -136,49 +136,41 @@ func GetNotifications(ctx context.Context, options *FindNotificationOptions) (nl
136136
}
137137

138138
// CountNotifications count all notifications that fit to the given options and ignore pagination.
139-
func CountNotifications(opts *FindNotificationOptions) (int64, error) {
140-
return db.GetEngine(db.DefaultContext).Where(opts.ToCond()).Count(&Notification{})
139+
func CountNotifications(ctx context.Context, opts *FindNotificationOptions) (int64, error) {
140+
return db.GetEngine(ctx).Where(opts.ToCond()).Count(&Notification{})
141141
}
142142

143143
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
144-
func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error {
145-
ctx, committer, err := db.TxContext(db.DefaultContext)
146-
if err != nil {
147-
return err
148-
}
149-
defer committer.Close()
150-
151-
var notify []*Notification
144+
func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) error {
145+
return db.AutoTx(ctx, func(ctx context.Context) error {
146+
var notify []*Notification
152147

153-
if newOwner.IsOrganization() {
154-
users, err := organization.GetUsersWhoCanCreateOrgRepo(ctx, newOwner.ID)
155-
if err != nil || len(users) == 0 {
156-
return err
157-
}
158-
for i := range users {
159-
notify = append(notify, &Notification{
160-
UserID: users[i].ID,
148+
if newOwner.IsOrganization() {
149+
users, err := organization.GetUsersWhoCanCreateOrgRepo(ctx, newOwner.ID)
150+
if err != nil || len(users) == 0 {
151+
return err
152+
}
153+
for i := range users {
154+
notify = append(notify, &Notification{
155+
UserID: users[i].ID,
156+
RepoID: repo.ID,
157+
Status: NotificationStatusUnread,
158+
UpdatedBy: doer.ID,
159+
Source: NotificationSourceRepository,
160+
})
161+
}
162+
} else {
163+
notify = []*Notification{{
164+
UserID: newOwner.ID,
161165
RepoID: repo.ID,
162166
Status: NotificationStatusUnread,
163167
UpdatedBy: doer.ID,
164168
Source: NotificationSourceRepository,
165-
})
169+
}}
166170
}
167-
} else {
168-
notify = []*Notification{{
169-
UserID: newOwner.ID,
170-
RepoID: repo.ID,
171-
Status: NotificationStatusUnread,
172-
UpdatedBy: doer.ID,
173-
Source: NotificationSourceRepository,
174-
}}
175-
}
176-
177-
if err := db.Insert(ctx, notify); err != nil {
178-
return err
179-
}
180171

181-
return committer.Commit()
172+
return db.Insert(ctx, notify)
173+
})
182174
}
183175

184176
// CreateOrUpdateIssueNotifications creates an issue notification
@@ -379,11 +371,7 @@ func CountUnread(ctx context.Context, userID int64) int64 {
379371
}
380372

381373
// LoadAttributes load Repo Issue User and Comment if not loaded
382-
func (n *Notification) LoadAttributes() (err error) {
383-
return n.loadAttributes(db.DefaultContext)
384-
}
385-
386-
func (n *Notification) loadAttributes(ctx context.Context) (err error) {
374+
func (n *Notification) LoadAttributes(ctx context.Context) (err error) {
387375
if err = n.loadRepo(ctx); err != nil {
388376
return
389377
}
@@ -481,10 +469,10 @@ func (n *Notification) APIURL() string {
481469
type NotificationList []*Notification
482470

483471
// LoadAttributes load Repo Issue User and Comment if not loaded
484-
func (nl NotificationList) LoadAttributes() error {
472+
func (nl NotificationList) LoadAttributes(ctx context.Context) error {
485473
var err error
486474
for i := 0; i < len(nl); i++ {
487-
err = nl[i].LoadAttributes()
475+
err = nl[i].LoadAttributes(ctx)
488476
if err != nil && !issues_model.IsErrCommentNotExist(err) {
489477
return err
490478
}
@@ -504,7 +492,7 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
504492
}
505493

506494
// LoadRepos loads repositories from database
507-
func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error) {
495+
func (nl NotificationList) LoadRepos(ctx context.Context) (repo_model.RepositoryList, []int, error) {
508496
if len(nl) == 0 {
509497
return repo_model.RepositoryList{}, []int{}, nil
510498
}
@@ -517,7 +505,7 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
517505
if left < limit {
518506
limit = left
519507
}
520-
rows, err := db.GetEngine(db.DefaultContext).
508+
rows, err := db.GetEngine(ctx).
521509
In("id", repoIDs[:limit]).
522510
Rows(new(repo_model.Repository))
523511
if err != nil {
@@ -578,7 +566,7 @@ func (nl NotificationList) getPendingIssueIDs() []int64 {
578566
}
579567

580568
// LoadIssues loads issues from database
581-
func (nl NotificationList) LoadIssues() ([]int, error) {
569+
func (nl NotificationList) LoadIssues(ctx context.Context) ([]int, error) {
582570
if len(nl) == 0 {
583571
return []int{}, nil
584572
}
@@ -591,7 +579,7 @@ func (nl NotificationList) LoadIssues() ([]int, error) {
591579
if left < limit {
592580
limit = left
593581
}
594-
rows, err := db.GetEngine(db.DefaultContext).
582+
rows, err := db.GetEngine(ctx).
595583
In("id", issueIDs[:limit]).
596584
Rows(new(issues_model.Issue))
597585
if err != nil {
@@ -662,7 +650,7 @@ func (nl NotificationList) getPendingCommentIDs() []int64 {
662650
}
663651

664652
// LoadComments loads comments from database
665-
func (nl NotificationList) LoadComments() ([]int, error) {
653+
func (nl NotificationList) LoadComments(ctx context.Context) ([]int, error) {
666654
if len(nl) == 0 {
667655
return []int{}, nil
668656
}
@@ -675,7 +663,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
675663
if left < limit {
676664
limit = left
677665
}
678-
rows, err := db.GetEngine(db.DefaultContext).
666+
rows, err := db.GetEngine(ctx).
679667
In("id", commentIDs[:limit]).
680668
Rows(new(issues_model.Comment))
681669
if err != nil {
@@ -775,8 +763,8 @@ func SetRepoReadBy(ctx context.Context, userID, repoID int64) error {
775763
}
776764

777765
// SetNotificationStatus change the notification status
778-
func SetNotificationStatus(notificationID int64, user *user_model.User, status NotificationStatus) (*Notification, error) {
779-
notification, err := getNotificationByID(db.DefaultContext, notificationID)
766+
func SetNotificationStatus(ctx context.Context, notificationID int64, user *user_model.User, status NotificationStatus) (*Notification, error) {
767+
notification, err := GetNotificationByID(ctx, notificationID)
780768
if err != nil {
781769
return notification, err
782770
}
@@ -787,16 +775,12 @@ func SetNotificationStatus(notificationID int64, user *user_model.User, status N
787775

788776
notification.Status = status
789777

790-
_, err = db.GetEngine(db.DefaultContext).ID(notificationID).Update(notification)
778+
_, err = db.GetEngine(ctx).ID(notificationID).Update(notification)
791779
return notification, err
792780
}
793781

794782
// GetNotificationByID return notification by ID
795-
func GetNotificationByID(notificationID int64) (*Notification, error) {
796-
return getNotificationByID(db.DefaultContext, notificationID)
797-
}
798-
799-
func getNotificationByID(ctx context.Context, notificationID int64) (*Notification, error) {
783+
func GetNotificationByID(ctx context.Context, notificationID int64) (*Notification, error) {
800784
notification := new(Notification)
801785
ok, err := db.GetEngine(ctx).
802786
Where("id = ?", notificationID).
@@ -813,9 +797,9 @@ func getNotificationByID(ctx context.Context, notificationID int64) (*Notificati
813797
}
814798

815799
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
816-
func UpdateNotificationStatuses(user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
800+
func UpdateNotificationStatuses(ctx context.Context, user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
817801
n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
818-
_, err := db.GetEngine(db.DefaultContext).
802+
_, err := db.GetEngine(ctx).
819803
Where("user_id = ? AND status = ?", user.ID, currentStatus).
820804
Cols("status", "updated_by", "updated_unix").
821805
Update(n)

Diff for: models/activities/notification_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ func TestSetNotificationStatus(t *testing.T) {
8282
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
8383
notf := unittest.AssertExistsAndLoadBean(t,
8484
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
85-
_, err := activities_model.SetNotificationStatus(notf.ID, user, activities_model.NotificationStatusPinned)
85+
_, err := activities_model.SetNotificationStatus(db.DefaultContext, notf.ID, user, activities_model.NotificationStatusPinned)
8686
assert.NoError(t, err)
8787
unittest.AssertExistsAndLoadBean(t,
8888
&activities_model.Notification{ID: notf.ID, Status: activities_model.NotificationStatusPinned})
8989

90-
_, err = activities_model.SetNotificationStatus(1, user, activities_model.NotificationStatusRead)
90+
_, err = activities_model.SetNotificationStatus(db.DefaultContext, 1, user, activities_model.NotificationStatusRead)
9191
assert.Error(t, err)
92-
_, err = activities_model.SetNotificationStatus(unittest.NonexistentID, user, activities_model.NotificationStatusRead)
92+
_, err = activities_model.SetNotificationStatus(db.DefaultContext, unittest.NonexistentID, user, activities_model.NotificationStatusRead)
9393
assert.Error(t, err)
9494
}
9595

@@ -102,7 +102,7 @@ func TestUpdateNotificationStatuses(t *testing.T) {
102102
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
103103
notfPinned := unittest.AssertExistsAndLoadBean(t,
104104
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusPinned})
105-
assert.NoError(t, activities_model.UpdateNotificationStatuses(user, activities_model.NotificationStatusUnread, activities_model.NotificationStatusRead))
105+
assert.NoError(t, activities_model.UpdateNotificationStatuses(db.DefaultContext, user, activities_model.NotificationStatusUnread, activities_model.NotificationStatusRead))
106106
unittest.AssertExistsAndLoadBean(t,
107107
&activities_model.Notification{ID: notfUnread.ID, Status: activities_model.NotificationStatusRead})
108108
unittest.AssertExistsAndLoadBean(t,

Diff for: models/db/consistency.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
package db
66

7-
import "xorm.io/builder"
7+
import (
8+
"context"
9+
10+
"xorm.io/builder"
11+
)
812

913
// CountOrphanedObjects count subjects with have no existing refobject anymore
10-
func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
11-
return GetEngine(DefaultContext).Table("`"+subject+"`").
14+
func CountOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) (int64, error) {
15+
return GetEngine(ctx).
16+
Table("`"+subject+"`").
1217
Join("LEFT", "`"+refobject+"`", joinCond).
1318
Where(builder.IsNull{"`" + refobject + "`.id"}).
1419
Select("COUNT(`" + subject + "`.`id`)").
1520
Count()
1621
}
1722

1823
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
19-
func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
24+
func DeleteOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) error {
2025
subQuery := builder.Select("`"+subject+"`.id").
2126
From("`"+subject+"`").
2227
Join("LEFT", "`"+refobject+"`", joinCond).
2328
Where(builder.IsNull{"`" + refobject + "`.id"})
2429
b := builder.Delete(builder.In("id", subQuery)).From("`" + subject + "`")
25-
_, err := GetEngine(DefaultContext).Exec(b)
30+
_, err := GetEngine(ctx).Exec(b)
2631
return err
2732
}

Diff for: models/db/engine_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ func TestDeleteOrphanedObjects(t *testing.T) {
4141
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
4242
assert.NoError(t, err)
4343

44-
orphaned, err := db.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
44+
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
4545
assert.NoError(t, err)
4646
assert.EqualValues(t, 3, orphaned)
4747

48-
err = db.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
48+
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
4949
assert.NoError(t, err)
5050

5151
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})

Diff for: models/db/sequence.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
package db
66

77
import (
8+
"context"
89
"fmt"
910
"regexp"
1011

1112
"code.gitea.io/gitea/modules/setting"
1213
)
1314

1415
// CountBadSequences looks for broken sequences from recreate-table mistakes
15-
func CountBadSequences() (int64, error) {
16+
func CountBadSequences(_ context.Context) (int64, error) {
1617
if !setting.Database.UsePostgreSQL {
1718
return 0, nil
1819
}
@@ -33,7 +34,7 @@ func CountBadSequences() (int64, error) {
3334
}
3435

3536
// FixBadSequences fixes for broken sequences from recreate-table mistakes
36-
func FixBadSequences() error {
37+
func FixBadSequences(_ context.Context) error {
3738
if !setting.Database.UsePostgreSQL {
3839
return nil
3940
}

0 commit comments

Comments
 (0)