diff options
author | JakobDev <jakobdev@gmx.de> | 2023-09-25 15:17:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-25 13:17:37 +0000 |
commit | 7047df36d4c39fefa6464b713c66302aa0038123 (patch) | |
tree | a5c471a3e8abf5cabac8de42c0fd8a814df7abf1 /models/issues | |
parent | 93bd4351bfb4a2f5f37ecb80d50496d044240e00 (diff) | |
download | gitea-7047df36d4c39fefa6464b713c66302aa0038123.tar.gz gitea-7047df36d4c39fefa6464b713c66302aa0038123.zip |
Another round of `db.DefaultContext` refactor (#27103)
Part of #27065
---------
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Diffstat (limited to 'models/issues')
-rw-r--r-- | models/issues/issue_index.go | 10 | ||||
-rw-r--r-- | models/issues/issue_stats.go | 10 | ||||
-rw-r--r-- | models/issues/issue_test.go | 2 | ||||
-rw-r--r-- | models/issues/pull.go | 2 | ||||
-rw-r--r-- | models/issues/reaction.go | 32 | ||||
-rw-r--r-- | models/issues/reaction_test.go | 10 | ||||
-rw-r--r-- | models/issues/review_list.go | 12 | ||||
-rw-r--r-- | models/issues/review_test.go | 4 |
8 files changed, 43 insertions, 39 deletions
diff --git a/models/issues/issue_index.go b/models/issues/issue_index.go index b480cc683f..16274d0ef0 100644 --- a/models/issues/issue_index.go +++ b/models/issues/issue_index.go @@ -3,12 +3,16 @@ package issues -import "code.gitea.io/gitea/models/db" +import ( + "context" + + "code.gitea.io/gitea/models/db" +) // RecalculateIssueIndexForRepo create issue_index for repo if not exist and // update it based on highest index of existing issues assigned to a repo -func RecalculateIssueIndexForRepo(repoID int64) error { - ctx, committer, err := db.TxContext(db.DefaultContext) +func RecalculateIssueIndexForRepo(ctx context.Context, repoID int64) error { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } diff --git a/models/issues/issue_stats.go b/models/issues/issue_stats.go index d01ee44462..99ca19f804 100644 --- a/models/issues/issue_stats.go +++ b/models/issues/issue_stats.go @@ -80,9 +80,9 @@ func CountIssues(ctx context.Context, opts *IssuesOptions) (int64, error) { } // GetIssueStats returns issue statistic information by given conditions. -func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) { +func GetIssueStats(ctx context.Context, opts *IssuesOptions) (*IssueStats, error) { if len(opts.IssueIDs) <= MaxQueryParameters { - return getIssueStatsChunk(opts, opts.IssueIDs) + return getIssueStatsChunk(ctx, opts, opts.IssueIDs) } // If too long a list of IDs is provided, we get the statistics in @@ -95,7 +95,7 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) { if chunk > len(opts.IssueIDs) { chunk = len(opts.IssueIDs) } - stats, err := getIssueStatsChunk(opts, opts.IssueIDs[i:chunk]) + stats, err := getIssueStatsChunk(ctx, opts, opts.IssueIDs[i:chunk]) if err != nil { return nil, err } @@ -112,10 +112,10 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) { return accum, nil } -func getIssueStatsChunk(opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) { +func getIssueStatsChunk(ctx context.Context, opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) { stats := &IssueStats{} - sess := db.GetEngine(db.DefaultContext). + sess := db.GetEngine(ctx). Join("INNER", "repository", "`issue`.repo_id = `repository`.id") var err error diff --git a/models/issues/issue_test.go b/models/issues/issue_test.go index 513ae241bc..7301995524 100644 --- a/models/issues/issue_test.go +++ b/models/issues/issue_test.go @@ -369,7 +369,7 @@ func TestCorrectIssueStats(t *testing.T) { // Now we will call the GetIssueStats with these IDs and if working, // get the correct stats back. - issueStats, err := issues_model.GetIssueStats(&issues_model.IssuesOptions{ + issueStats, err := issues_model.GetIssueStats(db.DefaultContext, &issues_model.IssuesOptions{ RepoIDs: []int64{1}, IssueIDs: ids, }) diff --git a/models/issues/pull.go b/models/issues/pull.go index 1c163ecca4..3ca0847652 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -311,7 +311,7 @@ func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error { return nil } - reviews, err := GetReviewsByIssueID(pr.Issue.ID) + reviews, err := GetReviewsByIssueID(ctx, pr.Issue.ID) if err != nil { return err } diff --git a/models/issues/reaction.go b/models/issues/reaction.go index 28da696366..bb47cf24ca 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -71,11 +71,11 @@ type Reaction struct { } // LoadUser load user of reaction -func (r *Reaction) LoadUser() (*user_model.User, error) { +func (r *Reaction) LoadUser(ctx context.Context) (*user_model.User, error) { if r.User != nil { return r.User, nil } - user, err := user_model.GetUserByID(db.DefaultContext, r.UserID) + user, err := user_model.GetUserByID(ctx, r.UserID) if err != nil { return nil, err } @@ -141,16 +141,16 @@ func (opts *FindReactionsOptions) toConds() builder.Cond { } // FindCommentReactions returns a ReactionList of all reactions from an comment -func FindCommentReactions(issueID, commentID int64) (ReactionList, int64, error) { - return FindReactions(db.DefaultContext, FindReactionsOptions{ +func FindCommentReactions(ctx context.Context, issueID, commentID int64) (ReactionList, int64, error) { + return FindReactions(ctx, FindReactionsOptions{ IssueID: issueID, CommentID: commentID, }) } // FindIssueReactions returns a ReactionList of all reactions from an issue -func FindIssueReactions(issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) { - return FindReactions(db.DefaultContext, FindReactionsOptions{ +func FindIssueReactions(ctx context.Context, issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) { + return FindReactions(ctx, FindReactionsOptions{ ListOptions: listOptions, IssueID: issueID, CommentID: -1, @@ -218,12 +218,12 @@ type ReactionOptions struct { } // CreateReaction creates reaction for issue or comment. -func CreateReaction(opts *ReactionOptions) (*Reaction, error) { +func CreateReaction(ctx context.Context, opts *ReactionOptions) (*Reaction, error) { if !setting.UI.ReactionsLookup.Contains(opts.Type) { return nil, ErrForbiddenIssueReaction{opts.Type} } - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -241,8 +241,8 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) { } // CreateIssueReaction creates a reaction on issue. -func CreateIssueReaction(doerID, issueID int64, content string) (*Reaction, error) { - return CreateReaction(&ReactionOptions{ +func CreateIssueReaction(ctx context.Context, doerID, issueID int64, content string) (*Reaction, error) { + return CreateReaction(ctx, &ReactionOptions{ Type: content, DoerID: doerID, IssueID: issueID, @@ -250,8 +250,8 @@ func CreateIssueReaction(doerID, issueID int64, content string) (*Reaction, erro } // CreateCommentReaction creates a reaction on comment. -func CreateCommentReaction(doerID, issueID, commentID int64, content string) (*Reaction, error) { - return CreateReaction(&ReactionOptions{ +func CreateCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) (*Reaction, error) { + return CreateReaction(ctx, &ReactionOptions{ Type: content, DoerID: doerID, IssueID: issueID, @@ -279,8 +279,8 @@ func DeleteReaction(ctx context.Context, opts *ReactionOptions) error { } // DeleteIssueReaction deletes a reaction on issue. -func DeleteIssueReaction(doerID, issueID int64, content string) error { - return DeleteReaction(db.DefaultContext, &ReactionOptions{ +func DeleteIssueReaction(ctx context.Context, doerID, issueID int64, content string) error { + return DeleteReaction(ctx, &ReactionOptions{ Type: content, DoerID: doerID, IssueID: issueID, @@ -289,8 +289,8 @@ func DeleteIssueReaction(doerID, issueID int64, content string) error { } // DeleteCommentReaction deletes a reaction on comment. -func DeleteCommentReaction(doerID, issueID, commentID int64, content string) error { - return DeleteReaction(db.DefaultContext, &ReactionOptions{ +func DeleteCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) error { + return DeleteReaction(ctx, &ReactionOptions{ Type: content, DoerID: doerID, IssueID: issueID, diff --git a/models/issues/reaction_test.go b/models/issues/reaction_test.go index ceb7f2c2a6..5dc8e1a5f3 100644 --- a/models/issues/reaction_test.go +++ b/models/issues/reaction_test.go @@ -20,9 +20,9 @@ func addReaction(t *testing.T, doerID, issueID, commentID int64, content string) var reaction *issues_model.Reaction var err error if commentID == 0 { - reaction, err = issues_model.CreateIssueReaction(doerID, issueID, content) + reaction, err = issues_model.CreateIssueReaction(db.DefaultContext, doerID, issueID, content) } else { - reaction, err = issues_model.CreateCommentReaction(doerID, issueID, commentID, content) + reaction, err = issues_model.CreateCommentReaction(db.DefaultContext, doerID, issueID, commentID, content) } assert.NoError(t, err) assert.NotNil(t, reaction) @@ -49,7 +49,7 @@ func TestIssueAddDuplicateReaction(t *testing.T) { addReaction(t, user1.ID, issue1ID, 0, "heart") - reaction, err := issues_model.CreateReaction(&issues_model.ReactionOptions{ + reaction, err := issues_model.CreateReaction(db.DefaultContext, &issues_model.ReactionOptions{ DoerID: user1.ID, IssueID: issue1ID, Type: "heart", @@ -70,7 +70,7 @@ func TestIssueDeleteReaction(t *testing.T) { addReaction(t, user1.ID, issue1ID, 0, "heart") - err := issues_model.DeleteIssueReaction(user1.ID, issue1ID, "heart") + err := issues_model.DeleteIssueReaction(db.DefaultContext, user1.ID, issue1ID, "heart") assert.NoError(t, err) unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID}) @@ -168,7 +168,7 @@ func TestIssueCommentReactionCount(t *testing.T) { var comment1ID int64 = 1 addReaction(t, user1.ID, issue1ID, comment1ID, "heart") - assert.NoError(t, issues_model.DeleteCommentReaction(user1.ID, issue1ID, comment1ID, "heart")) + assert.NoError(t, issues_model.DeleteCommentReaction(db.DefaultContext, user1.ID, issue1ID, comment1ID, "heart")) unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID}) } diff --git a/models/issues/review_list.go b/models/issues/review_list.go index 9f50d8e09d..ed3d0bd028 100644 --- a/models/issues/review_list.go +++ b/models/issues/review_list.go @@ -126,16 +126,16 @@ func FindLatestReviews(ctx context.Context, opts FindReviewOptions) (ReviewList, } // CountReviews returns count of reviews passing FindReviewOptions -func CountReviews(opts FindReviewOptions) (int64, error) { - return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Review{}) +func CountReviews(ctx context.Context, opts FindReviewOptions) (int64, error) { + return db.GetEngine(ctx).Where(opts.toCond()).Count(&Review{}) } // GetReviewersFromOriginalAuthorsByIssueID gets the latest review of each original authors for a pull request -func GetReviewersFromOriginalAuthorsByIssueID(issueID int64) (ReviewList, error) { +func GetReviewersFromOriginalAuthorsByIssueID(ctx context.Context, issueID int64) (ReviewList, error) { reviews := make([]*Review, 0, 10) // Get latest review of each reviewer, sorted in order they were made - if err := db.GetEngine(db.DefaultContext).SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND original_author_id <> 0 GROUP BY issue_id, original_author_id) ORDER BY review.updated_unix ASC", + if err := db.GetEngine(ctx).SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND original_author_id <> 0 GROUP BY issue_id, original_author_id) ORDER BY review.updated_unix ASC", issueID, ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest). Find(&reviews); err != nil { return nil, err @@ -145,10 +145,10 @@ func GetReviewersFromOriginalAuthorsByIssueID(issueID int64) (ReviewList, error) } // GetReviewsByIssueID gets the latest review of each reviewer for a pull request -func GetReviewsByIssueID(issueID int64) (ReviewList, error) { +func GetReviewsByIssueID(ctx context.Context, issueID int64) (ReviewList, error) { reviews := make([]*Review, 0, 10) - sess := db.GetEngine(db.DefaultContext) + sess := db.GetEngine(ctx) // Get latest review of each reviewer, sorted in order they were made if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND dismissed = ? AND original_author_id = 0 GROUP BY issue_id, reviewer_id) ORDER BY review.updated_unix ASC", diff --git a/models/issues/review_test.go b/models/issues/review_test.go index 8f0e773f4c..cd5b221bcd 100644 --- a/models/issues/review_test.go +++ b/models/issues/review_test.go @@ -144,7 +144,7 @@ func TestGetReviewersByIssueID(t *testing.T) { UpdatedUnix: 946684814, }) - allReviews, err := issues_model.GetReviewsByIssueID(issue.ID) + allReviews, err := issues_model.GetReviewsByIssueID(db.DefaultContext, issue.ID) assert.NoError(t, err) for _, review := range allReviews { assert.NoError(t, review.LoadReviewer(db.DefaultContext)) @@ -157,7 +157,7 @@ func TestGetReviewersByIssueID(t *testing.T) { } } - allReviews, err = issues_model.GetReviewsByIssueID(issue.ID) + allReviews, err = issues_model.GetReviewsByIssueID(db.DefaultContext, issue.ID) assert.NoError(t, err) assert.NoError(t, allReviews.LoadReviewers(db.DefaultContext)) if assert.Len(t, allReviews, 3) { |