diff options
author | 6543 <6543@obermui.de> | 2020-05-02 02:20:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-02 03:20:51 +0300 |
commit | c97494a4f4a0e8ba5453e293bcebb76274062b99 (patch) | |
tree | 29511446fbdc0296f77390a6566d4ad1eef4668c /models/review.go | |
parent | 4ed7d2a2bbb421e37cc0bc23a9f997e5606258ea (diff) | |
download | gitea-c97494a4f4a0e8ba5453e293bcebb76274062b99.tar.gz gitea-c97494a4f4a0e8ba5453e293bcebb76274062b99.zip |
API: Add pull review endpoints (#11224)
* API: Added pull review read only endpoints
* Update Structs, move Conversion, Refactor
* refactor
* lint & co
* fix lint + refactor
* add new Review state, rm unessesary, refacotr loadAttributes, convert patch to diff
* add DeletePullReview
* add paggination
* draft1: Create & submit review
* fix lint
* fix lint
* impruve test
* DONT use GhostUser for loadReviewer
* expose comments_count of a PullReview
* infent GetCodeCommentsCount()
* fixes
* fix+impruve
* some nits
* Handle Ghosts :ghost:
* add TEST for GET apis
* complete TESTS
* add HTMLURL to PullReview responce
* code format as per @lafriks
* update swagger definition
* Update routers/api/v1/repo/pull_review.go
Co-authored-by: David Svantesson <davidsvantesson@gmail.com>
* add comments
Co-authored-by: Thomas Berger <loki@lokis-chaos.de>
Co-authored-by: David Svantesson <davidsvantesson@gmail.com>
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() +} |