Browse Source

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 <dev.lh@web.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
pull/21882/head
KN4CK3R 2 weeks ago committed by GitHub
parent
commit
044c754ea5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/admin.go
  2. 18
      models/activities/action.go
  3. 10
      models/activities/action_test.go
  4. 96
      models/activities/notification.go
  5. 8
      models/activities/notification_test.go
  6. 15
      models/db/consistency.go
  7. 4
      models/db/engine_test.go
  8. 5
      models/db/sequence.go
  9. 2
      models/fixture_generation.go
  10. 17
      models/git/protected_tag.go
  11. 19
      models/git/protected_tag_test.go
  12. 9
      models/issues/assignees.go
  13. 10
      models/issues/assignees_test.go
  14. 63
      models/issues/comment.go
  15. 30
      models/issues/comment_list.go
  16. 114
      models/issues/issue.go
  17. 38
      models/issues/issue_list.go
  18. 3
      models/issues/issue_list_test.go
  19. 12
      models/issues/issue_project.go
  20. 4
      models/issues/issue_test.go
  21. 2
      models/issues/issue_xref.go
  22. 44
      models/issues/label.go
  23. 4
      models/issues/label_test.go
  24. 66
      models/issues/pull.go
  25. 2
      models/issues/pull_list.go
  26. 30
      models/issues/pull_test.go
  27. 20
      models/issues/review.go
  28. 2
      models/issues/review_test.go
  29. 2
      models/org_team_test.go
  30. 14
      models/perm/access/access_test.go
  31. 12
      models/perm/access/repo_permission.go
  32. 12
      models/repo/attachment.go
  33. 4
      models/repo/pushmirror.go
  34. 16
      models/repo/release.go
  35. 24
      models/repo/repo.go
  36. 11
      models/repo/repo_list.go
  37. 18
      models/repo/repo_list_test.go
  38. 10
      models/repo/user_repo.go
  39. 12
      models/user/user.go
  40. 8
      models/user/user_test.go
  41. 29
      modules/convert/issue.go
  42. 27
      modules/convert/issue_comment.go
  43. 6
      modules/convert/pull.go
  44. 9
      modules/convert/pull_test.go
  45. 22
      modules/doctor/dbconsistency.go
  46. 18
      modules/indexer/issues/indexer.go
  47. 117
      modules/notification/action/action.go
  48. 3
      modules/notification/action/action_test.go
  49. 90
      modules/notification/base/notifier.go
  50. 92
      modules/notification/base/null.go
  51. 44
      modules/notification/indexer/indexer.go
  52. 91
      modules/notification/mail/mail.go
  53. 14
      modules/notification/mirror/mirror.go
  54. 180
      modules/notification/notification.go
  55. 47
      modules/notification/ui/ui.go
  56. 321
      modules/notification/webhook/webhook.go
  57. 2
      modules/repository/repo.go
  58. 2
      routers/api/packages/conan/conan.go
  59. 2
      routers/api/v1/misc/nodeinfo.go
  60. 8
      routers/api/v1/notify/repo.go
  61. 8
      routers/api/v1/notify/threads.go
  62. 8
      routers/api/v1/notify/user.go
  63. 8
      routers/api/v1/org/team.go
  64. 12
      routers/api/v1/repo/branch.go
  65. 2
      routers/api/v1/repo/fork.go
  66. 28
      routers/api/v1/repo/issue.go
  67. 24
      routers/api/v1/repo/issue_comment.go
  68. 4
      routers/api/v1/repo/issue_reaction.go
  69. 10
      routers/api/v1/repo/issue_tracked_time.go
  70. 2
      routers/api/v1/repo/migrate.go
  71. 52
      routers/api/v1/repo/pull.go
  72. 4
      routers/api/v1/repo/pull_review.go
  73. 8
      routers/api/v1/repo/release.go
  74. 2
      routers/api/v1/repo/release_attachment.go
  75. 2
      routers/api/v1/repo/release_tags.go
  76. 4
      routers/api/v1/repo/repo.go
  77. 6
      routers/api/v1/repo/wiki.go
  78. 6
      routers/api/v1/user/repo.go
  79. 11
      routers/api/v1/user/star.go
  80. 11
      routers/api/v1/user/watch.go
  81. 2
      routers/private/hook_post_receive.go
  82. 4
      routers/private/hook_pre_receive.go
  83. 2
      routers/web/explore/repo.go
  84. 2
      routers/web/home.go
  85. 2
      routers/web/org/home.go
  86. 8
      routers/web/repo/branch.go
  87. 4
      routers/web/repo/compare.go
  88. 60
      routers/web/repo/issue.go
  89. 2
      routers/web/repo/issue_label.go
  90. 4
      routers/web/repo/projects.go
  91. 30
      routers/web/repo/pull.go
  92. 4
      routers/web/repo/pull_review.go
  93. 8
      routers/web/repo/release.go
  94. 2
      routers/web/repo/repo.go
  95. 4
      routers/web/repo/tag.go
  96. 6
      routers/web/repo/wiki.go
  97. 6
      routers/web/user/home.go
  98. 220
      routers/web/user/notification.go
  99. 6
      routers/web/user/profile.go
  100. 10
      services/agit/agit.go
  101. Some files were not shown because too many files have changed in this diff Show More

2
cmd/admin.go

@ -727,7 +727,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
log.Trace("Synchronizing repository releases (this may take a while)")
for page := 1; ; page++ {
repos, count, err := repo_model.SearchRepositoryByName(&repo_model.SearchRepoOptions{
repos, count, err := repo_model.SearchRepositoryByName(ctx, &repo_model.SearchRepoOptions{
ListOptions: db.ListOptions{
PageSize: repo_model.RepositoryListDefaultPageSize,
Page: page,

18
models/activities/action.go

@ -461,7 +461,8 @@ func DeleteOldActions(olderThan time.Duration) (err error) {
return err
}
func notifyWatchers(ctx context.Context, actions ...*Action) error {
// NotifyWatchers creates batch of actions for every watcher.
func NotifyWatchers(ctx context.Context, actions ...*Action) error {
var watchers []*repo_model.Watch
var repo *repo_model.Repository
var err error
@ -565,11 +566,6 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
return nil
}
// NotifyWatchers creates batch of actions for every watcher.
func NotifyWatchers(actions ...*Action) error {
return notifyWatchers(db.DefaultContext, actions...)
}
// NotifyWatchersActions creates batch of actions for every watcher.
func NotifyWatchersActions(acts []*Action) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
@ -578,7 +574,7 @@ func NotifyWatchersActions(acts []*Action) error {
}
defer committer.Close()
for _, act := range acts {
if err := notifyWatchers(ctx, act); err != nil {
if err := NotifyWatchers(ctx, act); err != nil {
return err
}
}
@ -603,17 +599,17 @@ func DeleteIssueActions(ctx context.Context, repoID, issueID int64) error {
}
// CountActionCreatedUnixString count actions where created_unix is an empty string
func CountActionCreatedUnixString() (int64, error) {
func CountActionCreatedUnixString(ctx context.Context) (int64, error) {
if setting.Database.UseSQLite3 {
return db.GetEngine(db.DefaultContext).Where(`created_unix = ""`).Count(new(Action))
return db.GetEngine(ctx).Where(`created_unix = ""`).Count(new(Action))
}
return 0, nil
}
// FixActionCreatedUnixString set created_unix to zero if it is an empty string
func FixActionCreatedUnixString() (int64, error) {
func FixActionCreatedUnixString(ctx context.Context) (int64, error) {
if setting.Database.UseSQLite3 {
res, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
res, err := db.GetEngine(ctx).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
if err != nil {
return 0, err
}

10
models/activities/action_test.go

@ -188,7 +188,7 @@ func TestNotifyWatchers(t *testing.T) {
RepoID: 1,
OpType: activities_model.ActionStarRepo,
}
assert.NoError(t, activities_model.NotifyWatchers(action))
assert.NoError(t, activities_model.NotifyWatchers(db.DefaultContext, action))
// One watchers are inactive, thus action is only created for user 8, 1, 4, 11
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
@ -256,17 +256,17 @@ func TestConsistencyUpdateAction(t *testing.T) {
//
// Get rid of incorrectly set created_unix
//
count, err := activities_model.CountActionCreatedUnixString()
count, err := activities_model.CountActionCreatedUnixString(db.DefaultContext)
assert.NoError(t, err)
assert.EqualValues(t, 1, count)
count, err = activities_model.FixActionCreatedUnixString()
count, err = activities_model.FixActionCreatedUnixString(db.DefaultContext)
assert.NoError(t, err)
assert.EqualValues(t, 1, count)
count, err = activities_model.CountActionCreatedUnixString()
count, err = activities_model.CountActionCreatedUnixString(db.DefaultContext)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
count, err = activities_model.FixActionCreatedUnixString()
count, err = activities_model.FixActionCreatedUnixString(db.DefaultContext)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)

96
models/activities/notification.go

@ -136,49 +136,41 @@ func GetNotifications(ctx context.Context, options *FindNotificationOptions) (nl
}
// CountNotifications count all notifications that fit to the given options and ignore pagination.
func CountNotifications(opts *FindNotificationOptions) (int64, error) {
return db.GetEngine(db.DefaultContext).Where(opts.ToCond()).Count(&Notification{})
func CountNotifications(ctx context.Context, opts *FindNotificationOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.ToCond()).Count(&Notification{})
}
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
defer committer.Close()
var notify []*Notification
func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) error {
return db.AutoTx(ctx, func(ctx context.Context) error {
var notify []*Notification
if newOwner.IsOrganization() {
users, err := organization.GetUsersWhoCanCreateOrgRepo(ctx, newOwner.ID)
if err != nil || len(users) == 0 {
return err
}
for i := range users {
notify = append(notify, &Notification{
UserID: users[i].ID,
if newOwner.IsOrganization() {
users, err := organization.GetUsersWhoCanCreateOrgRepo(ctx, newOwner.ID)
if err != nil || len(users) == 0 {
return err
}
for i := range users {
notify = append(notify, &Notification{
UserID: users[i].ID,
RepoID: repo.ID,
Status: NotificationStatusUnread,
UpdatedBy: doer.ID,
Source: NotificationSourceRepository,
})
}
} else {
notify = []*Notification{{
UserID: newOwner.ID,
RepoID: repo.ID,
Status: NotificationStatusUnread,
UpdatedBy: doer.ID,
Source: NotificationSourceRepository,
})
}}
}
} else {
notify = []*Notification{{
UserID: newOwner.ID,
RepoID: repo.ID,
Status: NotificationStatusUnread,
UpdatedBy: doer.ID,
Source: NotificationSourceRepository,
}}
}
if err := db.Insert(ctx, notify); err != nil {
return err
}
return committer.Commit()
return db.Insert(ctx, notify)
})
}
// CreateOrUpdateIssueNotifications creates an issue notification
@ -379,11 +371,7 @@ func CountUnread(ctx context.Context, userID int64) int64 {
}
// LoadAttributes load Repo Issue User and Comment if not loaded
func (n *Notification) LoadAttributes() (err error) {
return n.loadAttributes(db.DefaultContext)
}
func (n *Notification) loadAttributes(ctx context.Context) (err error) {
func (n *Notification) LoadAttributes(ctx context.Context) (err error) {
if err = n.loadRepo(ctx); err != nil {
return
}
@ -481,10 +469,10 @@ func (n *Notification) APIURL() string {
type NotificationList []*Notification
// LoadAttributes load Repo Issue User and Comment if not loaded
func (nl NotificationList) LoadAttributes() error {
func (nl NotificationList) LoadAttributes(ctx context.Context) error {
var err error
for i := 0; i < len(nl); i++ {
err = nl[i].LoadAttributes()
err = nl[i].LoadAttributes(ctx)
if err != nil && !issues_model.IsErrCommentNotExist(err) {
return err
}
@ -504,7 +492,7 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
}
// LoadRepos loads repositories from database
func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error) {
func (nl NotificationList) LoadRepos(ctx context.Context) (repo_model.RepositoryList, []int, error) {
if len(nl) == 0 {
return repo_model.RepositoryList{}, []int{}, nil
}
@ -517,7 +505,7 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
if left < limit {
limit = left
}
rows, err := db.GetEngine(db.DefaultContext).
rows, err := db.GetEngine(ctx).
In("id", repoIDs[:limit]).
Rows(new(repo_model.Repository))
if err != nil {
@ -578,7 +566,7 @@ func (nl NotificationList) getPendingIssueIDs() []int64 {
}
// LoadIssues loads issues from database
func (nl NotificationList) LoadIssues() ([]int, error) {
func (nl NotificationList) LoadIssues(ctx context.Context) ([]int, error) {
if len(nl) == 0 {
return []int{}, nil
}
@ -591,7 +579,7 @@ func (nl NotificationList) LoadIssues() ([]int, error) {
if left < limit {
limit = left
}
rows, err := db.GetEngine(db.DefaultContext).
rows, err := db.GetEngine(ctx).
In("id", issueIDs[:limit]).
Rows(new(issues_model.Issue))
if err != nil {
@ -662,7 +650,7 @@ func (nl NotificationList) getPendingCommentIDs() []int64 {
}
// LoadComments loads comments from database
func (nl NotificationList) LoadComments() ([]int, error) {
func (nl NotificationList) LoadComments(ctx context.Context) ([]int, error) {
if len(nl) == 0 {
return []int{}, nil
}
@ -675,7 +663,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
if left < limit {
limit = left
}
rows, err := db.GetEngine(db.DefaultContext).
rows, err := db.GetEngine(ctx).
In("id", commentIDs[:limit]).
Rows(new(issues_model.Comment))
if err != nil {
@ -775,8 +763,8 @@ func SetRepoReadBy(ctx context.Context, userID, repoID int64) error {
}
// SetNotificationStatus change the notification status
func SetNotificationStatus(notificationID int64, user *user_model.User, status NotificationStatus) (*Notification, error) {
notification, err := getNotificationByID(db.DefaultContext, notificationID)
func SetNotificationStatus(ctx context.Context, notificationID int64, user *user_model.User, status NotificationStatus) (*Notification, error) {
notification, err := GetNotificationByID(ctx, notificationID)
if err != nil {
return notification, err
}
@ -787,16 +775,12 @@ func SetNotificationStatus(notificationID int64, user *user_model.User, status N
notification.Status = status
_, err = db.GetEngine(db.DefaultContext).ID(notificationID).Update(notification)
_, err = db.GetEngine(ctx).ID(notificationID).Update(notification)
return notification, err
}
// GetNotificationByID return notification by ID
func GetNotificationByID(notificationID int64) (*Notification, error) {
return getNotificationByID(db.DefaultContext, notificationID)
}
func getNotificationByID(ctx context.Context, notificationID int64) (*Notification, error) {
func GetNotificationByID(ctx context.Context, notificationID int64) (*Notification, error) {
notification := new(Notification)
ok, err := db.GetEngine(ctx).
Where("id = ?", notificationID).
@ -813,9 +797,9 @@ func getNotificationByID(ctx context.Context, notificationID int64) (*Notificati
}
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
func UpdateNotificationStatuses(user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
func UpdateNotificationStatuses(ctx context.Context, user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
_, err := db.GetEngine(db.DefaultContext).
_, err := db.GetEngine(ctx).
Where("user_id = ? AND status = ?", user.ID, currentStatus).
Cols("status", "updated_by", "updated_unix").
Update(n)

8
models/activities/notification_test.go

@ -82,14 +82,14 @@ func TestSetNotificationStatus(t *testing.T) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
notf := unittest.AssertExistsAndLoadBean(t,
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
_, err := activities_model.SetNotificationStatus(notf.ID, user, activities_model.NotificationStatusPinned)
_, err := activities_model.SetNotificationStatus(db.DefaultContext, notf.ID, user, activities_model.NotificationStatusPinned)
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t,
&activities_model.Notification{ID: notf.ID, Status: activities_model.NotificationStatusPinned})
_, err = activities_model.SetNotificationStatus(1, user, activities_model.NotificationStatusRead)
_, err = activities_model.SetNotificationStatus(db.DefaultContext, 1, user, activities_model.NotificationStatusRead)
assert.Error(t, err)
_, err = activities_model.SetNotificationStatus(unittest.NonexistentID, user, activities_model.NotificationStatusRead)
_, err = activities_model.SetNotificationStatus(db.DefaultContext, unittest.NonexistentID, user, activities_model.NotificationStatusRead)
assert.Error(t, err)
}
@ -102,7 +102,7 @@ func TestUpdateNotificationStatuses(t *testing.T) {
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
notfPinned := unittest.AssertExistsAndLoadBean(t,
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusPinned})
assert.NoError(t, activities_model.UpdateNotificationStatuses(user, activities_model.NotificationStatusUnread, activities_model.NotificationStatusRead))
assert.NoError(t, activities_model.UpdateNotificationStatuses(db.DefaultContext, user, activities_model.NotificationStatusUnread, activities_model.NotificationStatusRead))
unittest.AssertExistsAndLoadBean(t,
&activities_model.Notification{ID: notfUnread.ID, Status: activities_model.NotificationStatusRead})
unittest.AssertExistsAndLoadBean(t,

15
models/db/consistency.go

@ -4,11 +4,16 @@
package db
import "xorm.io/builder"
import (
"context"
"xorm.io/builder"
)
// CountOrphanedObjects count subjects with have no existing refobject anymore
func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
return GetEngine(DefaultContext).Table("`"+subject+"`").
func CountOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) (int64, error) {
return GetEngine(ctx).
Table("`"+subject+"`").
Join("LEFT", "`"+refobject+"`", joinCond).
Where(builder.IsNull{"`" + refobject + "`.id"}).
Select("COUNT(`" + subject + "`.`id`)").
@ -16,12 +21,12 @@ func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
}
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
func DeleteOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) error {
subQuery := builder.Select("`"+subject+"`.id").
From("`"+subject+"`").
Join("LEFT", "`"+refobject+"`", joinCond).
Where(builder.IsNull{"`" + refobject + "`.id"})
b := builder.Delete(builder.In("id", subQuery)).From("`" + subject + "`")
_, err := GetEngine(DefaultContext).Exec(b)
_, err := GetEngine(ctx).Exec(b)
return err
}

4
models/db/engine_test.go

@ -41,11 +41,11 @@ func TestDeleteOrphanedObjects(t *testing.T) {
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
assert.NoError(t, err)
orphaned, err := db.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
assert.NoError(t, err)
assert.EqualValues(t, 3, orphaned)
err = db.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
assert.NoError(t, err)
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})

5
models/db/sequence.go

@ -5,6 +5,7 @@
package db
import (
"context"
"fmt"
"regexp"
@ -12,7 +13,7 @@ import (
)
// CountBadSequences looks for broken sequences from recreate-table mistakes
func CountBadSequences() (int64, error) {
func CountBadSequences(_ context.Context) (int64, error) {
if !setting.Database.UsePostgreSQL {
return 0, nil
}
@ -33,7 +34,7 @@ func CountBadSequences() (int64, error) {
}
// FixBadSequences fixes for broken sequences from recreate-table mistakes
func FixBadSequences() error {
func FixBadSequences(_ context.Context) error {
if !setting.Database.UsePostgreSQL {
return nil
}

2
models/fixture_generation.go

@ -22,7 +22,7 @@ func GetYamlFixturesAccess() (string, error) {
}
for _, repo := range repos {
repo.MustOwner()
repo.MustOwner(db.DefaultContext)
if err := access_model.RecalculateAccesses(db.DefaultContext, repo); err != nil {
return "", err
}

17
models/git/protected_tag.go

@ -5,6 +5,7 @@
package git
import (
"context"
"regexp"
"strings"
@ -69,13 +70,13 @@ func UpdateProtectedTag(pt *ProtectedTag) error {
}
// DeleteProtectedTag deletes a protected tag by ID
func DeleteProtectedTag(pt *ProtectedTag) error {
_, err := db.GetEngine(db.DefaultContext).ID(pt.ID).Delete(&ProtectedTag{})
func DeleteProtectedTag(ctx context.Context, pt *ProtectedTag) error {
_, err := db.GetEngine(ctx).ID(pt.ID).Delete(&ProtectedTag{})
return err
}
// IsUserAllowedModifyTag returns true if the user is allowed to modify the tag
func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) {
func IsUserAllowedModifyTag(ctx context.Context, pt *ProtectedTag, userID int64) (bool, error) {
if base.Int64sContains(pt.AllowlistUserIDs, userID) {
return true, nil
}
@ -84,7 +85,7 @@ func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) {
return false, nil
}
in, err := organization.IsUserInTeams(db.DefaultContext, userID, pt.AllowlistTeamIDs)
in, err := organization.IsUserInTeams(ctx, userID, pt.AllowlistTeamIDs)
if err != nil {
return false, err
}
@ -92,9 +93,9 @@ func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) {
}
// GetProtectedTags gets all protected tags of the repository
func GetProtectedTags(repoID int64) ([]*ProtectedTag, error) {
func GetProtectedTags(ctx context.Context, repoID int64) ([]*ProtectedTag, error) {
tags := make([]*ProtectedTag, 0)
return tags, db.GetEngine(db.DefaultContext).Find(&tags, &ProtectedTag{RepoID: repoID})
return tags, db.GetEngine(ctx).Find(&tags, &ProtectedTag{RepoID: repoID})
}
// GetProtectedTagByID gets the protected tag with the specific id
@ -112,7 +113,7 @@ func GetProtectedTagByID(id int64) (*ProtectedTag, error) {
// IsUserAllowedToControlTag checks if a user can control the specific tag.
// It returns true if the tag name is not protected or the user is allowed to control it.
func IsUserAllowedToControlTag(tags []*ProtectedTag, tagName string, userID int64) (bool, error) {
func IsUserAllowedToControlTag(ctx context.Context, tags []*ProtectedTag, tagName string, userID int64) (bool, error) {
isAllowed := true
for _, tag := range tags {
err := tag.EnsureCompiledPattern()
@ -124,7 +125,7 @@ func IsUserAllowedToControlTag(tags []*ProtectedTag, tagName string, userID int6
continue
}
isAllowed, err = IsUserAllowedModifyTag(tag, userID)
isAllowed, err = IsUserAllowedModifyTag(ctx, tag, userID)
if err != nil {
return false, err
}

19
models/git/protected_tag_test.go

@ -7,6 +7,7 @@ package git_test
import (
"testing"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unittest"
@ -17,29 +18,29 @@ func TestIsUserAllowed(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
pt := &git_model.ProtectedTag{}
allowed, err := git_model.IsUserAllowedModifyTag(pt, 1)
allowed, err := git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1)
assert.NoError(t, err)
assert.False(t, allowed)
pt = &git_model.ProtectedTag{
AllowlistUserIDs: []int64{1},
}
allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1)
assert.NoError(t, err)
assert.True(t, allowed)
allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2)
assert.NoError(t, err)
assert.False(t, allowed)
pt = &git_model.ProtectedTag{
AllowlistTeamIDs: []int64{1},
}
allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1)
assert.NoError(t, err)
assert.False(t, allowed)
allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2)
assert.NoError(t, err)
assert.True(t, allowed)
@ -47,11 +48,11 @@ func TestIsUserAllowed(t *testing.T) {
AllowlistUserIDs: []int64{1},
AllowlistTeamIDs: []int64{1},
}
allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1)
assert.NoError(t, err)
assert.True(t, allowed)
allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2)
assert.NoError(t, err)
assert.True(t, allowed)
}
@ -135,7 +136,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) {
}
for n, c := range cases {
isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid)
isAllowed, err := git_model.IsUserAllowedToControlTag(db.DefaultContext, protectedTags, c.name, c.userid)
assert.NoError(t, err)
assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n)
}
@ -157,7 +158,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) {
}
for n, c := range cases {
isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid)
isAllowed, err := git_model.IsUserAllowedToControlTag(db.DefaultContext, protectedTags, c.name, c.userid)
assert.NoError(t, err)
assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n)
}

9
models/issues/assignees.go

@ -48,9 +48,10 @@ func (issue *Issue) LoadAssignees(ctx context.Context) (err error) {
// GetAssigneeIDsByIssue returns the IDs of users assigned to an issue
// but skips joining with `user` for performance reasons.
// User permissions must be verified elsewhere if required.
func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) {
func GetAssigneeIDsByIssue(ctx context.Context, issueID int64) ([]int64, error) {
userIDs := make([]int64, 0, 5)
return userIDs, db.GetEngine(db.DefaultContext).Table("issue_assignees").
return userIDs, db.GetEngine(ctx).
Table("issue_assignees").
Cols("assignee_id").
Where("issue_id = ?", issueID).
Distinct("assignee_id").
@ -151,7 +152,7 @@ func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (re
}
// MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
func MakeIDsFromAPIAssigneesToAdd(ctx context.Context, oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
var requestAssignees []string
// Keeping the old assigning method for compatibility reasons
@ -165,7 +166,7 @@ func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string
}
// Get the IDs of all assignees
assigneeIDs, err = user_model.GetUserIDsByNames(requestAssignees, false)
assigneeIDs, err = user_model.GetUserIDsByNames(ctx, requestAssignees, false)
return assigneeIDs, err
}

10
models/issues/assignees_test.go

@ -71,22 +71,22 @@ func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) {
_ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
_ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
IDs, err := issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{""})
IDs, err := issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{""})
assert.NoError(t, err)
assert.Equal(t, []int64{}, IDs)
_, err = issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{"none_existing_user"})
_, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{"none_existing_user"})
assert.Error(t, err)
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("user1", []string{"user1"})
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "user1", []string{"user1"})
assert.NoError(t, err)
assert.Equal(t, []int64{1}, IDs)
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("user2", []string{""})
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "user2", []string{""})
assert.NoError(t, err)
assert.Equal(t, []int64{2}, IDs)
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{"user1", "user2"})
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{"user1", "user2"})
assert.NoError(t, err)
assert.Equal(t, []int64{1, 2}, IDs)
}

63
models/issues/comment.go

@ -309,13 +309,8 @@ type PushActionContent struct {
CommitIDs []string `json:"commit_ids"`
}
// LoadIssue loads issue from database
func (c *Comment) LoadIssue() (err error) {
return c.LoadIssueCtx(db.DefaultContext)
}
// LoadIssueCtx loads issue from database
func (c *Comment) LoadIssueCtx(ctx context.Context) (err error) {
// LoadIssue loads the issue reference for the comment
func (c *Comment) LoadIssue(ctx context.Context) (err error) {
if c.Issue != nil {
return nil
}
@ -350,7 +345,8 @@ func (c *Comment) AfterLoad(session *xorm.Session) {
}
}
func (c *Comment) loadPoster(ctx context.Context) (err error) {
// LoadPoster loads comment poster
func (c *Comment) LoadPoster(ctx context.Context) (err error) {
if c.PosterID <= 0 || c.Poster != nil {
return nil
}
@ -381,7 +377,7 @@ func (c *Comment) AfterDelete() {
// HTMLURL formats a URL-string to the issue-comment
func (c *Comment) HTMLURL() string {
err := c.LoadIssue()
err := c.LoadIssue(db.DefaultContext)
if err != nil { // Silently dropping errors :unamused:
log.Error("LoadIssue(%d): %v", c.IssueID, err)
return ""
@ -410,7 +406,7 @@ func (c *Comment) HTMLURL() string {
// APIURL formats a API-string to the issue-comment
func (c *Comment) APIURL() string {
err := c.LoadIssue()
err := c.LoadIssue(db.DefaultContext)
if err != nil { // Silently dropping errors :unamused:
log.Error("LoadIssue(%d): %v", c.IssueID, err)
return ""
@ -426,7 +422,7 @@ func (c *Comment) APIURL() string {
// IssueURL formats a URL-string to the issue
func (c *Comment) IssueURL() string {
err := c.LoadIssue()
err := c.LoadIssue(db.DefaultContext)
if err != nil { // Silently dropping errors :unamused:
log.Error("LoadIssue(%d): %v", c.IssueID, err)
return ""
@ -446,7 +442,7 @@ func (c *Comment) IssueURL() string {
// PRURL formats a URL-string to the pull-request
func (c *Comment) PRURL() string {
err := c.LoadIssue()
err := c.LoadIssue(db.DefaultContext)
if err != nil { // Silently dropping errors :unamused:
log.Error("LoadIssue(%d): %v", c.IssueID, err)
return ""
@ -521,10 +517,10 @@ func (c *Comment) LoadProject() error {
}
// LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
func (c *Comment) LoadMilestone() error {
func (c *Comment) LoadMilestone(ctx context.Context) error {
if c.OldMilestoneID > 0 {
var oldMilestone Milestone
has, err := db.GetEngine(db.DefaultContext).ID(c.OldMilestoneID).Get(&oldMilestone)
has, err := db.GetEngine(ctx).ID(c.OldMilestoneID).Get(&oldMilestone)
if err != nil {
return err
} else if has {
@ -534,7 +530,7 @@ func (c *Comment) LoadMilestone() error {
if c.MilestoneID > 0 {
var milestone Milestone
has, err := db.GetEngine(db.DefaultContext).ID(c.MilestoneID).Get(&milestone)
has, err := db.GetEngine(ctx).ID(c.MilestoneID).Get(&milestone)
if err != nil {
return err
} else if has {
@ -544,19 +540,14 @@ func (c *Comment) LoadMilestone() error {
return nil
}
// LoadPoster loads comment poster
func (c *Comment) LoadPoster() error {
return c.loadPoster(db.DefaultContext)
}
// LoadAttachments loads attachments (it never returns error, the error during `GetAttachmentsByCommentIDCtx` is ignored)
func (c *Comment) LoadAttachments() error {
func (c *Comment) LoadAttachments(ctx context.Context) error {
if len(c.Attachments) > 0 {
return nil
}
var err error
c.Attachments, err = repo_model.GetAttachmentsByCommentID(db.DefaultContext, c.ID)
c.Attachments, err = repo_model.GetAttachmentsByCommentID(ctx, c.ID)
if err != nil {
log.Error("getAttachmentsByCommentID[%d]: %v", c.ID, err)
}
@ -598,7 +589,7 @@ func (c *Comment) LoadAssigneeUserAndTeam() error {
c.Assignee = user_model.NewGhostUser()
}
} else if c.AssigneeTeamID > 0 && c.AssigneeTeam == nil {
if err = c.LoadIssue(); err != nil {
if err = c.LoadIssue(db.DefaultContext); err != nil {
return err
}
@ -740,7 +731,7 @@ func (c *Comment) UnsignedLine() uint64 {
// CodeCommentURL returns the url to a comment in code
func (c *Comment) CodeCommentURL() string {
err := c.LoadIssue()
err := c.LoadIssue(db.DefaultContext)
if err != nil { // Silently dropping errors :unamused:
log.Error("LoadIssue(%d): %v", c.IssueID, err)
return ""
@ -1145,7 +1136,7 @@ func UpdateComment(c *Comment, doer *user_model.User) error {
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
return err
}
if err := c.LoadIssueCtx(ctx); err != nil {
if err := c.LoadIssue(ctx); err != nil {
return err
}
if err := c.AddCrossReferences(ctx, doer, true); err != nil {
@ -1245,7 +1236,7 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
return nil, err
}
if err := CommentList(comments).loadPosters(ctx); err != nil {
if err := CommentList(comments).LoadPosters(ctx); err != nil {
return nil, err
}
@ -1363,11 +1354,11 @@ func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullReques
if typ != CommentTypePRScheduledToAutoMerge && typ != CommentTypePRUnScheduledToAutoMerge {
return nil, fmt.Errorf("comment type %d cannot be used to create an auto merge comment", typ)
}
if err = pr.LoadIssueCtx(ctx); err != nil {
if err = pr.LoadIssue(ctx); err != nil {
return
}
if err = pr.LoadBaseRepoCtx(ctx); err != nil {
if err = pr.LoadBaseRepo(ctx); err != nil {
return
}
@ -1512,18 +1503,18 @@ func (c *Comment) GetExternalName() string { return c.OriginalAuthor }
func (c *Comment) GetExternalID() int64 { return c.OriginalAuthorID }
// CountCommentTypeLabelWithEmptyLabel count label comments with empty label
func CountCommentTypeLabelWithEmptyLabel() (int64, error) {
return db.GetEngine(db.DefaultContext).Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Count(new(Comment))
func CountCommentTypeLabelWithEmptyLabel(ctx context.Context) (int64, error) {
return db.GetEngine(ctx).Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Count(new(Comment))
}
// FixCommentTypeLabelWithEmptyLabel count label comments with empty label
func FixCommentTypeLabelWithEmptyLabel() (int64, error) {
return db.GetEngine(db.DefaultContext).Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Delete(new(Comment))
func FixCommentTypeLabelWithEmptyLabel(ctx context.Context) (int64, error) {
return db.GetEngine(ctx).Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Delete(new(Comment))
}
// CountCommentTypeLabelWithOutsideLabels count label comments with outside label
func CountCommentTypeLabelWithOutsideLabels() (int64, error) {
return db.GetEngine(db.DefaultContext).Where("comment.type = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id))", CommentTypeLabel).
func CountCommentTypeLabelWithOutsideLabels(ctx context.Context) (int64, error) {
return db.GetEngine(ctx).Where("comment.type = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id))", CommentTypeLabel).
Table("comment").
Join("inner", "label", "label.id = comment.label_id").
Join("inner", "issue", "issue.id = comment.issue_id ").
@ -1532,8 +1523,8 @@ func CountCommentTypeLabelWithOutsideLabels() (int64, error) {
}
// FixCommentTypeLabelWithOutsideLabels count label comments with outside label
func FixCommentTypeLabelWithOutsideLabels() (int64, error) {
res, err := db.GetEngine(db.DefaultContext).Exec(`DELETE FROM comment WHERE comment.id IN (
func FixCommentTypeLabelWithOutsideLabels(ctx context.Context) (int64, error) {
res, err := db.GetEngine(ctx).Exec(`DELETE FROM comment WHERE comment.id IN (
SELECT il_too.id FROM (
SELECT com.id
FROM comment AS com

30
models/issues/comment_list.go

@ -24,7 +24,8 @@ func (comments CommentList) getPosterIDs() []int64 {
return posterIDs.Values()
}
func (comments CommentList) loadPosters(ctx context.Context) error {
// LoadPosters loads posters
func (comments CommentList) LoadPosters(ctx context.Context) error {
if len(comments) == 0 {
return nil
}
@ -277,7 +278,8 @@ func (comments CommentList) Issues() IssueList {
return issueList
}
func (comments CommentList) loadIssues(ctx context.Context) error {
// LoadIssues loads issues of comments
func (comments CommentList) LoadIssues(ctx context.Context) error {
if len(comments) == 0 {
return nil
}
@ -382,7 +384,8 @@ func (comments CommentList) loadDependentIssues(ctx context.Context) error {
return nil
}
func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
// LoadAttachments loads attachments
func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
if len(comments) == 0 {
return nil
}
@ -476,7 +479,7 @@ func (comments CommentList) loadReviews(ctx context.Context) error { //nolint
// loadAttributes loads all attributes
func (comments CommentList) loadAttributes(ctx context.Context) (err error) {
if err = comments.loadPosters(ctx); err != nil {
if err = comments.LoadPosters(ctx); err != nil {
return
}
@ -496,7 +499,7 @@ func (comments CommentList) loadAttributes(ctx context.Context) (err error) {
return
}
if err = comments.loadAttachments(ctx); err != nil {
if err = comments.LoadAttachments(ctx); err != nil {
return
}
@ -504,7 +507,7 @@ func (comments CommentList) loadAttributes(ctx context.Context) (err error) {
return
}
if err = comments.loadIssues(ctx); err != nil {
if err = comments.LoadIssues(ctx); err != nil {
return
}
@ -520,18 +523,3 @@ func (comments CommentList) loadAttributes(ctx context.Context) (err error) {
func (comments CommentList) LoadAttributes() error {
return comments.loadAttributes(db.DefaultContext)
}
// LoadAttachments loads attachments
func (comments CommentList) LoadAttachments() error {
return comments.loadAttachments(db.DefaultContext)
}
// LoadPosters loads posters
func (comments CommentList) LoadPosters() error {
return comments.loadPosters(db.DefaultContext)
}
// LoadIssues loads issues of comments
func (comments CommentList) LoadIssues() error {
return comments.loadIssues(db.DefaultContext)
}

114
models/issues/issue.go

@ -241,11 +241,7 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
}
// LoadPoster loads poster
func (issue *Issue) LoadPoster() error {
return issue.loadPoster(db.DefaultContext)
}
func (issue *Issue) loadPoster(ctx context.Context) (err error) {
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
if issue.Poster == nil {
issue.Poster, err = user_model.GetUserByIDCtx(ctx, issue.PosterID)
if err != nil {
@ -261,7 +257,8 @@ func (issue *Issue) loadPoster(ctx context.Context) (err error) {
return err
}
func (issue *Issue) loadPullRequest(ctx context.Context) (err error) {
// LoadPullRequest loads pull request info
func (issue *Issue) LoadPullRequest(ctx context.Context) (err error) {
if issue.IsPull && issue.PullRequest == nil {
issue.PullRequest, err = GetPullRequestByIssueID(ctx, issue.ID)
if err != nil {
@ -275,18 +272,13 @@ func (issue *Issue) loadPullRequest(ctx context.Context) (err error) {
return nil
}
// LoadPullRequest loads pull request info
func (issue *Issue) LoadPullRequest() error {
return issue.loadPullRequest(db.DefaultContext)
}
func (issue *Issue) loadComments(ctx context.Context) (err error) {
return issue.loadCommentsByType(ctx, CommentTypeUnknown)
}
// LoadDiscussComments loads discuss comments
func (issue *Issue) LoadDiscussComments() error {
return issue.loadCommentsByType(db.DefaultContext, CommentTypeComment)
func (issue *Issue) LoadDiscussComments(ctx context.Context) error {
return issue.loadCommentsByType(ctx, CommentTypeComment)
}
func (issue *Issue) loadCommentsByType(ctx context.Context, tp CommentType) (err error) {
@ -357,7 +349,8 @@ func (issue *Issue) loadForeignReference(ctx context.Context) (err error) {
return nil
}
func (issue *Issue) loadMilestone(ctx context.Context) (err error) {
// LoadMilestone load milestone of this issue.
func (issue *Issue) LoadMilestone(ctx context.Context) (err error) {
if (issue.Milestone == nil || issue.Milestone.ID != issue.MilestoneID) && issue.MilestoneID > 0 {
issue.Milestone, err = GetMilestoneByRepoID(ctx, issue.RepoID, issue.MilestoneID)
if err != nil && !IsErrMilestoneNotExist(err) {
@ -373,7 +366,7 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
return
}
if err = issue.loadPoster(ctx); err != nil {
if err = issue.LoadPoster(ctx); err != nil {
return
}
@ -381,7 +374,7 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
return
}
if err = issue.loadMilestone(ctx); err != nil {
if err = issue.LoadMilestone(ctx); err != nil {
return
}
@ -393,7 +386,7 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
return
}
if err = issue.loadPullRequest(ctx); err != nil && !IsErrPullRequestNotExist(err) {
if err = issue.LoadPullRequest(ctx); err != nil && !IsErrPullRequestNotExist(err) {
// It is possible pull request is not yet created.
return err
}
@ -425,11 +418,6 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
return issue.loadReactions(ctx)
}
// LoadMilestone load milestone of this issue.
func (issue *Issue) LoadMilestone() error {
return issue.loadMilestone(db.DefaultContext)
}
// GetIsRead load the `IsRead` field of the issue
func (issue *Issue) GetIsRead(userID int64) error {
issueUser := &IssueUser{IssueID: issue.ID, UID: userID}
@ -548,7 +536,7 @@ func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) {
if err := issue.LoadRepo(ctx); err != nil {
return err
} else if err = issue.loadPullRequest(ctx); err != nil {
} else if err = issue.LoadPullRequest(ctx); err != nil {
return err
}
@ -751,7 +739,7 @@ func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User,
if err := issue.LoadRepo(ctx); err != nil {
return nil, err
}
if err := issue.loadPoster(ctx); err != nil {
if err := issue.LoadPoster(ctx); err != nil {
return nil, err
}
@ -1027,7 +1015,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
return fmt.Errorf("find all labels [label_ids: %v]: %w", opts.LabelIDs, err)
}
if err = opts.Issue.loadPoster(ctx); err != nil {
if err = opts.Issue.LoadPoster(ctx); err != nil {
return err
}
@ -1505,10 +1493,9 @@ func applySubscribedCondition(sess *xorm.Session, subscriberID int64) *xorm.Sess
}
// CountIssuesByRepo map from repoID to number of issues matching the options
func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
e := db.GetEngine(db.DefaultContext)
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
func CountIssuesByRepo(ctx context.Context, opts *IssuesOptions) (map[int64]int64, error) {
sess := db.GetEngine(ctx).
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSessionNoLimit(sess)
@ -1551,10 +1538,9 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
}
// Issues returns a list of issues by given conditions.
func Issues(opts *IssuesOptions) ([]*Issue, error) {
e := db.GetEngine(db.DefaultContext)
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
func Issues(ctx context.Context, opts *IssuesOptions) ([]*Issue, error) {
sess := db.GetEngine(ctx).
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSessionWithLimit(sess)
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
@ -1572,11 +1558,11 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
}
// CountIssues number return of issues by given conditions.
func CountIssues(opts *IssuesOptions) (int64, error) {
e := db.GetEngine(db.DefaultContext)
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
func CountIssues(ctx context.Context, opts *IssuesOptions) (int64, error) {
sess := db.GetEngine(ctx).
Select("COUNT(issue.id) AS count").
Table("issue").
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSessionNoLimit(sess)
return sess.Count()
@ -1585,9 +1571,10 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
// GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue,
// but skips joining with `user` for performance reasons.
// User permissions must be verified elsewhere if required.
func GetParticipantsIDsByIssueID(issueID int64) ([]int64, error) {
func GetParticipantsIDsByIssueID(ctx context.Context, issueID int64) ([]int64, error) {
userIDs := make([]int64, 0, 5)
return userIDs, db.GetEngine(db.DefaultContext).Table("comment").
return userIDs, db.GetEngine(ctx).
Table("comment").
Cols("poster_id").
Where("issue_id = ?", issueID).
And("type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
@ -2426,8 +2413,9 @@ func (issue *Issue) GetExternalName() string { return issue.OriginalAuthor }
func (issue *Issue) GetExternalID() int64 { return issue.OriginalAuthorID }
// CountOrphanedIssues count issues without a repo
func CountOrphanedIssues() (int64, error) {
return db.GetEngine(db.DefaultContext).Table("issue").