You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

review.go 6.3 kB

Pull request review/approval and comment on code (#3748) * Initial ui components for pull request review * Add Review Add IssueComment types Signed-off-by: Jonas Franz <info@jonasfranz.software> (cherry picked from commit 2b4daab) Signed-off-by: Jonas Franz <info@jonasfranz.software> * Replace ReviewComment with Content Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add load functions Add ReviewID to findComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add create review comment implementation Add migration for review Other small changes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Simplified create and find functions for review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved "Pending" to first position Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add GetCurrentReview to simplify fetching current review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Preview for listing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Move new comment form to its own file Signed-off-by: Jonas Franz <info@jonasfranz.software> * Implement Review form Show Review comments on comment stream Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for single comments Showing buttons in context Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add pending tag to pending review comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add unit tests for Review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fetch all review ids at once Add unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improved comment rendering in "Files" view by adding Comments to DiffLine Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for invalidating comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Switched back to code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved review migration from v64 to v65 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Rebuild css Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improve translations Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests by updating fixtures and updating outdated test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Comments will be shown at the right place now Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for deleting CodeComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems caused by files in subdirectories Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for showing code comments of reviews in conversation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for "Show/Hide outdated" Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for new webhooks Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update comparison Signed-off-by: Jonas Franz <info@jonasfranz.software> * Resolve conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Minor UI improvements * update code.gitea.io/git * Fix ui bug reported by @lunny causing wrong position of add button Add functionality to "Cancel" button Add scale effects to add button Hide "Cancel" button for existing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Prepare solving conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show add button only if no comments already exist for the line Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing vendor files Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if reviewer is nil Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show forms only to users who are logged in Signed-off-by: Jonas Franz <info@jonasfranz.software> * Revert "Show forms only to users who are logged in" This reverts commit c083682 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Save patch in comment Render patch for code comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add link to comment in code Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add reply form to comment list Show forms only to signed in users Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add 'Reply' as translatable Add CODE_COMMENT_LINES setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems introduced by checking for singed in user Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add v70 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update generated stylesheet Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix preview Beginn with new review comment patch system Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add new algo to generate diff for line range Remove old algo used for cutting big diffs (it was very buggy) * Add documentation and example for CutDiffAroundLine * Fix example of CutDiffAroundLine * Fix some comment UI rendering bugs * Add code comment edit mode * Send notifications / actions to users until review gets published Fix diff generation bug Fix wrong hashtag * Fix vet errors * Send notifications also for single comments * Fix some notification bugs, fix link * Fix: add comment icon is only shown on code lines * Add lint comment * Add unit tests for git diff * Add more error messages * Regenerated css Signed-off-by: Jonas Franz <info@jonasfranz.software> * fmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Regenerated CSS with latest less version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by updating comment type to new ID Signed-off-by: Jonas Franz <info@jonasfranz.software> * Introducing CodeComments as type for map[string]map[int64][]*Comment Other minor code improvements Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix data-tab issues Signed-off-by: Jonas Franz <info@jonasfranz.software> * Remove unnecessary change Signed-off-by: Jonas Franz <info@jonasfranz.software> * refactored checkForInvalidation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Append comments instead of setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * Use HeadRepo instead of BaseRepo Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update migration Signed-off-by: Jonas Franz <info@jonasfranz.de> * Regenerated CSS Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add copyright Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update index.css Signed-off-by: Jonas Franz <info@jonasfranz.software>
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/util"
  9. "github.com/go-xorm/xorm"
  10. "github.com/go-xorm/builder"
  11. )
  12. // ReviewType defines the sort of feedback a review gives
  13. type ReviewType int
  14. // ReviewTypeUnknown unknown review type
  15. const ReviewTypeUnknown ReviewType = -1
  16. const (
  17. // ReviewTypePending is a review which is not published yet
  18. ReviewTypePending ReviewType = iota
  19. // ReviewTypeApprove approves changes
  20. ReviewTypeApprove
  21. // ReviewTypeComment gives general feedback
  22. ReviewTypeComment
  23. // ReviewTypeReject gives feedback blocking merge
  24. ReviewTypeReject
  25. )
  26. // Icon returns the corresponding icon for the review type
  27. func (rt ReviewType) Icon() string {
  28. switch rt {
  29. case ReviewTypeApprove:
  30. return "eye"
  31. case ReviewTypeReject:
  32. return "x"
  33. default:
  34. case ReviewTypeComment:
  35. case ReviewTypeUnknown:
  36. return "comment"
  37. }
  38. return "comment"
  39. }
  40. // Review represents collection of code comments giving feedback for a PR
  41. type Review struct {
  42. ID int64 `xorm:"pk autoincr"`
  43. Type ReviewType
  44. Reviewer *User `xorm:"-"`
  45. ReviewerID int64 `xorm:"index"`
  46. Issue *Issue `xorm:"-"`
  47. IssueID int64 `xorm:"index"`
  48. Content string
  49. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  50. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  51. // CodeComments are the initial code comments of the review
  52. CodeComments CodeComments `xorm:"-"`
  53. }
  54. func (r *Review) loadCodeComments(e Engine) (err error) {
  55. r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r)
  56. return
  57. }
  58. // LoadCodeComments loads CodeComments
  59. func (r *Review) LoadCodeComments() error {
  60. return r.loadCodeComments(x)
  61. }
  62. func (r *Review) loadIssue(e Engine) (err error) {
  63. r.Issue, err = getIssueByID(e, r.IssueID)
  64. return
  65. }
  66. func (r *Review) loadReviewer(e Engine) (err error) {
  67. if r.ReviewerID == 0 {
  68. return nil
  69. }
  70. r.Reviewer, err = getUserByID(e, r.ReviewerID)
  71. return
  72. }
  73. func (r *Review) loadAttributes(e Engine) (err error) {
  74. if err = r.loadReviewer(e); err != nil {
  75. return
  76. }
  77. if err = r.loadIssue(e); err != nil {
  78. return
  79. }
  80. return
  81. }
  82. // LoadAttributes loads all attributes except CodeComments
  83. func (r *Review) LoadAttributes() error {
  84. return r.loadAttributes(x)
  85. }
  86. // Publish will send notifications / actions to participants for all code comments; parts are concurrent
  87. func (r *Review) Publish() error {
  88. return r.publish(x)
  89. }
  90. func (r *Review) publish(e *xorm.Engine) error {
  91. if r.Type == ReviewTypePending || r.Type == ReviewTypeUnknown {
  92. return fmt.Errorf("review cannot be published if type is pending or unknown")
  93. }
  94. if r.Issue == nil {
  95. if err := r.loadIssue(e); err != nil {
  96. return err
  97. }
  98. }
  99. if err := r.Issue.loadRepo(e); err != nil {
  100. return err
  101. }
  102. if len(r.CodeComments) == 0 {
  103. if err := r.loadCodeComments(e); err != nil {
  104. return err
  105. }
  106. }
  107. for _, lines := range r.CodeComments {
  108. for _, comments := range lines {
  109. for _, comment := range comments {
  110. go func(en *xorm.Engine, review *Review, comm *Comment) {
  111. sess := en.NewSession()
  112. defer sess.Close()
  113. if err := sendCreateCommentAction(sess, &CreateCommentOptions{
  114. Doer: comm.Poster,
  115. Issue: review.Issue,
  116. Repo: review.Issue.Repo,
  117. Type: comm.Type,
  118. Content: comm.Content,
  119. }, comm); err != nil {
  120. log.Warn("sendCreateCommentAction: %v", err)
  121. }
  122. }(e, r, comment)
  123. }
  124. }
  125. }
  126. return nil
  127. }
  128. func getReviewByID(e Engine, id int64) (*Review, error) {
  129. review := new(Review)
  130. if has, err := e.ID(id).Get(review); err != nil {
  131. return nil, err
  132. } else if !has {
  133. return nil, ErrReviewNotExist{ID: id}
  134. } else {
  135. return review, nil
  136. }
  137. }
  138. // GetReviewByID returns the review by the given ID
  139. func GetReviewByID(id int64) (*Review, error) {
  140. return getReviewByID(x, id)
  141. }
  142. // FindReviewOptions represent possible filters to find reviews
  143. type FindReviewOptions struct {
  144. Type ReviewType
  145. IssueID int64
  146. ReviewerID int64
  147. }
  148. func (opts *FindReviewOptions) toCond() builder.Cond {
  149. var cond = builder.NewCond()
  150. if opts.IssueID > 0 {
  151. cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
  152. }
  153. if opts.ReviewerID > 0 {
  154. cond = cond.And(builder.Eq{"reviewer_id": opts.ReviewerID})
  155. }
  156. if opts.Type != ReviewTypeUnknown {
  157. cond = cond.And(builder.Eq{"type": opts.Type})
  158. }
  159. return cond
  160. }
  161. func findReviews(e Engine, opts FindReviewOptions) ([]*Review, error) {
  162. reviews := make([]*Review, 0, 10)
  163. sess := e.Where(opts.toCond())
  164. return reviews, sess.
  165. Asc("created_unix").
  166. Asc("id").
  167. Find(&reviews)
  168. }
  169. // FindReviews returns reviews passing FindReviewOptions
  170. func FindReviews(opts FindReviewOptions) ([]*Review, error) {
  171. return findReviews(x, opts)
  172. }
  173. // CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
  174. type CreateReviewOptions struct {
  175. Content string
  176. Type ReviewType
  177. Issue *Issue
  178. Reviewer *User
  179. }
  180. func createReview(e Engine, opts CreateReviewOptions) (*Review, error) {
  181. review := &Review{
  182. Type: opts.Type,
  183. Issue: opts.Issue,
  184. IssueID: opts.Issue.ID,
  185. Reviewer: opts.Reviewer,
  186. ReviewerID: opts.Reviewer.ID,
  187. Content: opts.Content,
  188. }
  189. if _, err := e.Insert(review); err != nil {
  190. return nil, err
  191. }
  192. return review, nil
  193. }
  194. // CreateReview creates a new review based on opts
  195. func CreateReview(opts CreateReviewOptions) (*Review, error) {
  196. return createReview(x, opts)
  197. }
  198. func getCurrentReview(e Engine, reviewer *User, issue *Issue) (*Review, error) {
  199. if reviewer == nil {
  200. return nil, nil
  201. }
  202. reviews, err := findReviews(e, FindReviewOptions{
  203. Type: ReviewTypePending,
  204. IssueID: issue.ID,
  205. ReviewerID: reviewer.ID,
  206. })
  207. if err != nil {
  208. return nil, err
  209. }
  210. if len(reviews) == 0 {
  211. return nil, ErrReviewNotExist{}
  212. }
  213. return reviews[0], nil
  214. }
  215. // GetCurrentReview returns the current pending review of reviewer for given issue
  216. func GetCurrentReview(reviewer *User, issue *Issue) (*Review, error) {
  217. return getCurrentReview(x, reviewer, issue)
  218. }
  219. // UpdateReview will update all cols of the given review in db
  220. func UpdateReview(r *Review) error {
  221. if _, err := x.ID(r.ID).AllCols().Update(r); err != nil {
  222. return err
  223. }
  224. return nil
  225. }