mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Even more db.DefaultContext
refactor (#27352)
Part of #27065 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
@ -18,7 +18,7 @@ func TestUpdateAssignee(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
// Fake issue with assignees
|
||||
issue, err := issues_model.GetIssueWithAttrsByID(1)
|
||||
issue, err := issues_model.GetIssueWithAttrsByID(db.DefaultContext, 1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Assign multiple users
|
||||
|
@ -655,12 +655,12 @@ func (c *Comment) LoadDepIssueDetails(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
// LoadTime loads the associated time for a CommentTypeAddTimeManual
|
||||
func (c *Comment) LoadTime() error {
|
||||
func (c *Comment) LoadTime(ctx context.Context) error {
|
||||
if c.Time != nil || c.TimeID == 0 {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
c.Time, err = GetTrackedTimeByID(c.TimeID)
|
||||
c.Time, err = GetTrackedTimeByID(ctx, c.TimeID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -201,12 +201,12 @@ func (issue *Issue) IsTimetrackerEnabled(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
// GetPullRequest returns the issue pull request
|
||||
func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
|
||||
func (issue *Issue) GetPullRequest(ctx context.Context) (pr *PullRequest, err error) {
|
||||
if !issue.IsPull {
|
||||
return nil, fmt.Errorf("Issue is not a pull request")
|
||||
}
|
||||
|
||||
pr, err = GetPullRequestByIssueID(db.DefaultContext, issue.ID)
|
||||
pr, err = GetPullRequestByIssueID(ctx, issue.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -369,9 +369,9 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
// GetIsRead load the `IsRead` field of the issue
|
||||
func (issue *Issue) GetIsRead(userID int64) error {
|
||||
func (issue *Issue) GetIsRead(ctx context.Context, userID int64) error {
|
||||
issueUser := &IssueUser{IssueID: issue.ID, UID: userID}
|
||||
if has, err := db.GetEngine(db.DefaultContext).Get(issueUser); err != nil {
|
||||
if has, err := db.GetEngine(ctx).Get(issueUser); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
issue.IsRead = false
|
||||
@ -382,9 +382,9 @@ func (issue *Issue) GetIsRead(userID int64) error {
|
||||
}
|
||||
|
||||
// APIURL returns the absolute APIURL to this issue.
|
||||
func (issue *Issue) APIURL() string {
|
||||
func (issue *Issue) APIURL(ctx context.Context) string {
|
||||
if issue.Repo == nil {
|
||||
err := issue.LoadRepo(db.DefaultContext)
|
||||
err := issue.LoadRepo(ctx)
|
||||
if err != nil {
|
||||
log.Error("Issue[%d].APIURL(): %v", issue.ID, err)
|
||||
return ""
|
||||
@ -479,9 +479,9 @@ func (issue *Issue) GetLastEventLabel() string {
|
||||
}
|
||||
|
||||
// GetLastComment return last comment for the current issue.
|
||||
func (issue *Issue) GetLastComment() (*Comment, error) {
|
||||
func (issue *Issue) GetLastComment(ctx context.Context) (*Comment, error) {
|
||||
var c Comment
|
||||
exist, err := db.GetEngine(db.DefaultContext).Where("type = ?", CommentTypeComment).
|
||||
exist, err := db.GetEngine(ctx).Where("type = ?", CommentTypeComment).
|
||||
And("issue_id = ?", issue.ID).Desc("created_unix").Get(&c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -543,12 +543,12 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
|
||||
}
|
||||
|
||||
// GetIssueWithAttrsByID returns an issue with attributes by given ID.
|
||||
func GetIssueWithAttrsByID(id int64) (*Issue, error) {
|
||||
issue, err := GetIssueByID(db.DefaultContext, id)
|
||||
func GetIssueWithAttrsByID(ctx context.Context, id int64) (*Issue, error) {
|
||||
issue, err := GetIssueByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return issue, issue.LoadAttributes(db.DefaultContext)
|
||||
return issue, issue.LoadAttributes(ctx)
|
||||
}
|
||||
|
||||
// GetIssuesByIDs return issues with the given IDs.
|
||||
@ -600,8 +600,8 @@ func GetParticipantsIDsByIssueID(ctx context.Context, issueID int64) ([]int64, e
|
||||
}
|
||||
|
||||
// IsUserParticipantsOfIssue return true if user is participants of an issue
|
||||
func IsUserParticipantsOfIssue(user *user_model.User, issue *Issue) bool {
|
||||
userIDs, err := issue.GetParticipantIDsByIssue(db.DefaultContext)
|
||||
func IsUserParticipantsOfIssue(ctx context.Context, user *user_model.User, issue *Issue) bool {
|
||||
userIDs, err := issue.GetParticipantIDsByIssue(ctx)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return false
|
||||
@ -894,8 +894,8 @@ func IsErrIssueMaxPinReached(err error) bool {
|
||||
}
|
||||
|
||||
// InsertIssues insert issues to database
|
||||
func InsertIssues(issues ...*Issue) error {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
func InsertIssues(ctx context.Context, issues ...*Issue) error {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ func TestIssueAPIURL(t *testing.T) {
|
||||
err := issue.LoadAttributes(db.DefaultContext)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL(db.DefaultContext))
|
||||
}
|
||||
|
||||
func TestGetIssuesByIDs(t *testing.T) {
|
||||
@ -477,7 +477,7 @@ func assertCreateIssues(t *testing.T, isPull bool) {
|
||||
Labels: []*issues_model.Label{label},
|
||||
Reactions: []*issues_model.Reaction{reaction},
|
||||
}
|
||||
err := issues_model.InsertIssues(is)
|
||||
err := issues_model.InsertIssues(db.DefaultContext, is)
|
||||
assert.NoError(t, err)
|
||||
|
||||
i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: title})
|
||||
|
@ -81,7 +81,7 @@ func CheckIssueWatch(ctx context.Context, user *user_model.User, issue *Issue) (
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return repo_model.IsWatchMode(w.Mode) || IsUserParticipantsOfIssue(user, issue), nil
|
||||
return repo_model.IsWatchMode(w.Mode) || IsUserParticipantsOfIssue(ctx, user, issue), nil
|
||||
}
|
||||
|
||||
// GetIssueWatchersIDs returns IDs of subscribers or explicit unsubscribers to a given issue id
|
||||
|
@ -1040,7 +1040,7 @@ func ParseCodeOwnersLine(ctx context.Context, tokens []string) (*CodeOwnerRule,
|
||||
warnings = append(warnings, fmt.Sprintf("incorrect codeowner organization: %s", user))
|
||||
continue
|
||||
}
|
||||
teams, err := org.LoadTeams()
|
||||
teams, err := org.LoadTeams(ctx)
|
||||
if err != nil {
|
||||
warnings = append(warnings, fmt.Sprintf("incorrect codeowner team: %s", user))
|
||||
continue
|
||||
|
@ -199,8 +199,8 @@ func addTime(ctx context.Context, user *user_model.User, issue *Issue, amount in
|
||||
}
|
||||
|
||||
// TotalTimesForEachUser returns the spent time in seconds for each user by an issue
|
||||
func TotalTimesForEachUser(options *FindTrackedTimesOptions) (map[*user_model.User]int64, error) {
|
||||
trackedTimes, err := GetTrackedTimes(db.DefaultContext, options)
|
||||
func TotalTimesForEachUser(ctx context.Context, options *FindTrackedTimesOptions) (map[*user_model.User]int64, error) {
|
||||
trackedTimes, err := GetTrackedTimes(ctx, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -213,7 +213,7 @@ func TotalTimesForEachUser(options *FindTrackedTimesOptions) (map[*user_model.Us
|
||||
totalTimes := make(map[*user_model.User]int64)
|
||||
// Fetching User and making time human readable
|
||||
for userID, total := range totalTimesByUser {
|
||||
user, err := user_model.GetUserByID(db.DefaultContext, userID)
|
||||
user, err := user_model.GetUserByID(ctx, userID)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
continue
|
||||
@ -226,8 +226,8 @@ func TotalTimesForEachUser(options *FindTrackedTimesOptions) (map[*user_model.Us
|
||||
}
|
||||
|
||||
// DeleteIssueUserTimes deletes times for issue
|
||||
func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
func DeleteIssueUserTimes(ctx context.Context, issue *Issue, user *user_model.User) error {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -265,8 +265,8 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
|
||||
}
|
||||
|
||||
// DeleteTime delete a specific Time
|
||||
func DeleteTime(t *TrackedTime) error {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
func DeleteTime(ctx context.Context, t *TrackedTime) error {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -315,9 +315,9 @@ func deleteTime(ctx context.Context, t *TrackedTime) error {
|
||||
}
|
||||
|
||||
// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
|
||||
func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
|
||||
func GetTrackedTimeByID(ctx context.Context, id int64) (*TrackedTime, error) {
|
||||
time := new(TrackedTime)
|
||||
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(time)
|
||||
has, err := db.GetEngine(ctx).ID(id).Get(time)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
|
@ -82,7 +82,7 @@ func TestGetTrackedTimes(t *testing.T) {
|
||||
func TestTotalTimesForEachUser(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
total, err := issues_model.TotalTimesForEachUser(&issues_model.FindTrackedTimesOptions{IssueID: 1})
|
||||
total, err := issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, total, 1)
|
||||
for user, time := range total {
|
||||
@ -90,7 +90,7 @@ func TestTotalTimesForEachUser(t *testing.T) {
|
||||
assert.EqualValues(t, 400, time)
|
||||
}
|
||||
|
||||
total, err = issues_model.TotalTimesForEachUser(&issues_model.FindTrackedTimesOptions{IssueID: 2})
|
||||
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, total, 2)
|
||||
for user, time := range total {
|
||||
@ -103,7 +103,7 @@ func TestTotalTimesForEachUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
total, err = issues_model.TotalTimesForEachUser(&issues_model.FindTrackedTimesOptions{IssueID: 5})
|
||||
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 5})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, total, 1)
|
||||
for user, time := range total {
|
||||
@ -111,7 +111,7 @@ func TestTotalTimesForEachUser(t *testing.T) {
|
||||
assert.EqualValues(t, 1, time)
|
||||
}
|
||||
|
||||
total, err = issues_model.TotalTimesForEachUser(&issues_model.FindTrackedTimesOptions{IssueID: 4})
|
||||
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 4})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, total, 2)
|
||||
}
|
||||
|
Reference in New Issue
Block a user