diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /models/review.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'models/review.go')
-rw-r--r-- | models/review.go | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/models/review.go b/models/review.go index eebf796876..eeb33611ce 100644 --- a/models/review.go +++ b/models/review.go @@ -5,6 +5,7 @@ package models import ( + "context" "fmt" "strings" @@ -85,20 +86,20 @@ func init() { db.RegisterModel(new(Review)) } -func (r *Review) loadCodeComments(e db.Engine) (err error) { +func (r *Review) loadCodeComments(ctx context.Context) (err error) { if r.CodeComments != nil { return } - if err = r.loadIssue(e); err != nil { + if err = r.loadIssue(db.GetEngine(ctx)); err != nil { return } - r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r) + r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r) return } // LoadCodeComments loads CodeComments func (r *Review) LoadCodeComments() error { - return r.loadCodeComments(db.GetEngine(db.DefaultContext)) + return r.loadCodeComments(db.DefaultContext) } func (r *Review) loadIssue(e db.Engine) (err error) { @@ -136,11 +137,12 @@ func (r *Review) LoadReviewerTeam() error { return r.loadReviewerTeam(db.GetEngine(db.DefaultContext)) } -func (r *Review) loadAttributes(e db.Engine) (err error) { +func (r *Review) loadAttributes(ctx context.Context) (err error) { + e := db.GetEngine(ctx) if err = r.loadIssue(e); err != nil { return } - if err = r.loadCodeComments(e); err != nil { + if err = r.loadCodeComments(ctx); err != nil { return } if err = r.loadReviewer(e); err != nil { @@ -154,7 +156,7 @@ func (r *Review) loadAttributes(e db.Engine) (err error) { // LoadAttributes loads all attributes except CodeComments func (r *Review) LoadAttributes() error { - return r.loadAttributes(db.GetEngine(db.DefaultContext)) + return r.loadAttributes(db.DefaultContext) } func getReviewByID(e db.Engine, id int64) (*Review, error) { @@ -235,15 +237,15 @@ type CreateReviewOptions struct { // IsOfficialReviewer check if at least one of the provided reviewers can make official reviews in issue (counts towards required approvals) func IsOfficialReviewer(issue *Issue, reviewers ...*user_model.User) (bool, error) { - return isOfficialReviewer(db.GetEngine(db.DefaultContext), issue, reviewers...) + return isOfficialReviewer(db.DefaultContext, issue, reviewers...) } -func isOfficialReviewer(e db.Engine, issue *Issue, reviewers ...*user_model.User) (bool, error) { - pr, err := getPullRequestByIssueID(e, issue.ID) +func isOfficialReviewer(ctx context.Context, issue *Issue, reviewers ...*user_model.User) (bool, error) { + pr, err := getPullRequestByIssueID(db.GetEngine(ctx), issue.ID) if err != nil { return false, err } - if err = pr.loadProtectedBranch(e); err != nil { + if err = pr.loadProtectedBranch(ctx); err != nil { return false, err } if pr.ProtectedBranch == nil { @@ -251,7 +253,7 @@ func isOfficialReviewer(e db.Engine, issue *Issue, reviewers ...*user_model.User } for _, reviewer := range reviewers { - official, err := pr.ProtectedBranch.isUserOfficialReviewer(e, reviewer) + official, err := isUserOfficialReviewer(ctx, pr.ProtectedBranch, reviewer) if official || err != nil { return official, err } @@ -262,15 +264,15 @@ func isOfficialReviewer(e db.Engine, issue *Issue, reviewers ...*user_model.User // IsOfficialReviewerTeam check if reviewer in this team can make official reviews in issue (counts towards required approvals) func IsOfficialReviewerTeam(issue *Issue, team *Team) (bool, error) { - return isOfficialReviewerTeam(db.GetEngine(db.DefaultContext), issue, team) + return isOfficialReviewerTeam(db.DefaultContext, issue, team) } -func isOfficialReviewerTeam(e db.Engine, issue *Issue, team *Team) (bool, error) { - pr, err := getPullRequestByIssueID(e, issue.ID) +func isOfficialReviewerTeam(ctx context.Context, issue *Issue, team *Team) (bool, error) { + pr, err := getPullRequestByIssueID(db.GetEngine(ctx), issue.ID) if err != nil { return false, err } - if err = pr.loadProtectedBranch(e); err != nil { + if err = pr.loadProtectedBranch(ctx); err != nil { return false, err } if pr.ProtectedBranch == nil { @@ -385,7 +387,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co if _, err := sess.Exec("UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?", false, issue.ID, doer.ID); err != nil { return nil, nil, err } - if official, err = isOfficialReviewer(sess, issue, doer); err != nil { + if official, err = isOfficialReviewer(ctx, issue, doer); err != nil { return nil, nil, err } } @@ -403,7 +405,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co return nil, nil, err } } else { - if err := review.loadCodeComments(sess); err != nil { + if err := review.loadCodeComments(ctx); err != nil { return nil, nil, err } if reviewType != ReviewTypeApprove && len(review.CodeComments) == 0 && len(strings.TrimSpace(content)) == 0 { @@ -415,7 +417,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co if _, err := sess.Exec("UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?", false, issue.ID, doer.ID); err != nil { return nil, nil, err } - if official, err = isOfficialReviewer(sess, issue, doer); err != nil { + if official, err = isOfficialReviewer(ctx, issue, doer); err != nil { return nil, nil, err } } @@ -647,7 +649,7 @@ func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, return nil, nil } - official, err := isOfficialReviewer(sess, issue, reviewer, doer) + official, err := isOfficialReviewer(ctx, issue, reviewer, doer) if err != nil { return nil, err } else if official { @@ -705,7 +707,7 @@ func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Commen return nil, err } - official, err := isOfficialReviewer(sess, issue, reviewer) + official, err := isOfficialReviewer(ctx, issue, reviewer) if err != nil { return nil, err } else if official { @@ -756,11 +758,11 @@ func AddTeamReviewRequest(issue *Issue, reviewer *Team, doer *user_model.User) ( return nil, nil } - official, err := isOfficialReviewerTeam(sess, issue, reviewer) + official, err := isOfficialReviewerTeam(ctx, issue, reviewer) if err != nil { return nil, fmt.Errorf("isOfficialReviewerTeam(): %v", err) } else if !official { - if official, err = isOfficialReviewer(sess, issue, doer); err != nil { + if official, err = isOfficialReviewer(ctx, issue, doer); err != nil { return nil, fmt.Errorf("isOfficialReviewer(): %v", err) } } @@ -819,7 +821,7 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *Team, doer *user_model.User return nil, err } - official, err := isOfficialReviewerTeam(sess, issue, reviewer) + official, err := isOfficialReviewerTeam(ctx, issue, reviewer) if err != nil { return nil, fmt.Errorf("isOfficialReviewerTeam(): %v", err) } |