diff options
Diffstat (limited to 'models/review.go')
-rw-r--r-- | models/review.go | 98 |
1 files changed, 93 insertions, 5 deletions
diff --git a/models/review.go b/models/review.go index c6203e7097..522fe5886c 100644 --- a/models/review.go +++ b/models/review.go @@ -74,9 +74,13 @@ type Review struct { } func (r *Review) loadCodeComments(e Engine) (err error) { - if r.CodeComments == nil { - r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r) + if r.CodeComments != nil { + return + } + if err = r.loadIssue(e); err != nil { + return } + r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r) return } @@ -86,12 +90,15 @@ func (r *Review) LoadCodeComments() error { } func (r *Review) loadIssue(e Engine) (err error) { + if r.Issue != nil { + return + } r.Issue, err = getIssueByID(e, r.IssueID) return } func (r *Review) loadReviewer(e Engine) (err error) { - if r.ReviewerID == 0 { + if r.Reviewer != nil || r.ReviewerID == 0 { return nil } r.Reviewer, err = getUserByID(e, r.ReviewerID) @@ -104,10 +111,13 @@ func (r *Review) LoadReviewer() error { } func (r *Review) loadAttributes(e Engine) (err error) { - if err = r.loadReviewer(e); err != nil { + if err = r.loadIssue(e); err != nil { return } - if err = r.loadIssue(e); err != nil { + if err = r.loadCodeComments(e); err != nil { + return + } + if err = r.loadReviewer(e); err != nil { return } return @@ -136,6 +146,7 @@ func GetReviewByID(id int64) (*Review, error) { // FindReviewOptions represent possible filters to find reviews type FindReviewOptions struct { + ListOptions Type ReviewType IssueID int64 ReviewerID int64 @@ -162,6 +173,9 @@ func (opts *FindReviewOptions) toCond() builder.Cond { func findReviews(e Engine, opts FindReviewOptions) ([]*Review, error) { reviews := make([]*Review, 0, 10) sess := e.Where(opts.toCond()) + if opts.Page > 0 { + sess = opts.ListOptions.setSessionPagination(sess) + } return reviews, sess. Asc("created_unix"). Asc("id"). @@ -656,3 +670,77 @@ func CanMarkConversation(issue *Issue, doer *User) (permResult bool, err error) return true, nil } + +// DeleteReview delete a review and it's code comments +func DeleteReview(r *Review) error { + sess := x.NewSession() + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + if r.ID == 0 { + return fmt.Errorf("review is not allowed to be 0") + } + + opts := FindCommentsOptions{ + Type: CommentTypeCode, + IssueID: r.IssueID, + ReviewID: r.ID, + } + + if _, err := sess.Where(opts.toConds()).Delete(new(Comment)); err != nil { + return err + } + + opts = FindCommentsOptions{ + Type: CommentTypeReview, + IssueID: r.IssueID, + ReviewID: r.ID, + } + + if _, err := sess.Where(opts.toConds()).Delete(new(Comment)); err != nil { + return err + } + + if _, err := sess.ID(r.ID).Delete(new(Review)); err != nil { + return err + } + + return sess.Commit() +} + +// GetCodeCommentsCount return count of CodeComments a Review has +func (r *Review) GetCodeCommentsCount() int { + opts := FindCommentsOptions{ + Type: CommentTypeCode, + IssueID: r.IssueID, + ReviewID: r.ID, + } + conds := opts.toConds() + if r.ID == 0 { + conds = conds.And(builder.Eq{"invalidated": false}) + } + + count, err := x.Where(conds).Count(new(Comment)) + if err != nil { + return 0 + } + return int(count) +} + +// HTMLURL formats a URL-string to the related review issue-comment +func (r *Review) HTMLURL() string { + opts := FindCommentsOptions{ + Type: CommentTypeReview, + IssueID: r.IssueID, + ReviewID: r.ID, + } + comment := new(Comment) + has, err := x.Where(opts.toConds()).Get(comment) + if err != nil || !has { + return "" + } + return comment.HTMLURL() +} |