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

321 lines
8.7KB

  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/timeutil"
  9. "xorm.io/builder"
  10. "xorm.io/core"
  11. "xorm.io/xorm"
  12. )
  13. // ReviewType defines the sort of feedback a review gives
  14. type ReviewType int
  15. // ReviewTypeUnknown unknown review type
  16. const ReviewTypeUnknown ReviewType = -1
  17. const (
  18. // ReviewTypePending is a review which is not published yet
  19. ReviewTypePending ReviewType = iota
  20. // ReviewTypeApprove approves changes
  21. ReviewTypeApprove
  22. // ReviewTypeComment gives general feedback
  23. ReviewTypeComment
  24. // ReviewTypeReject gives feedback blocking merge
  25. ReviewTypeReject
  26. )
  27. // Icon returns the corresponding icon for the review type
  28. func (rt ReviewType) Icon() string {
  29. switch rt {
  30. case ReviewTypeApprove:
  31. return "eye"
  32. case ReviewTypeReject:
  33. return "x"
  34. case ReviewTypeComment, ReviewTypeUnknown:
  35. return "comment"
  36. default:
  37. return "comment"
  38. }
  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 timeutil.TimeStamp `xorm:"INDEX created"`
  50. UpdatedUnix timeutil.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. func getUniqueApprovalsByPullRequestID(e Engine, prID int64) (reviews []*Review, err error) {
  143. reviews = make([]*Review, 0)
  144. if err := e.
  145. Where("issue_id = ? AND type = ?", prID, ReviewTypeApprove).
  146. OrderBy("updated_unix").
  147. GroupBy("reviewer_id").
  148. Find(&reviews); err != nil {
  149. return nil, err
  150. }
  151. return
  152. }
  153. // GetUniqueApprovalsByPullRequestID returns all reviews submitted for a specific pull request
  154. func GetUniqueApprovalsByPullRequestID(prID int64) ([]*Review, error) {
  155. return getUniqueApprovalsByPullRequestID(x, prID)
  156. }
  157. // FindReviewOptions represent possible filters to find reviews
  158. type FindReviewOptions struct {
  159. Type ReviewType
  160. IssueID int64
  161. ReviewerID int64
  162. }
  163. func (opts *FindReviewOptions) toCond() builder.Cond {
  164. var cond = builder.NewCond()
  165. if opts.IssueID > 0 {
  166. cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
  167. }
  168. if opts.ReviewerID > 0 {
  169. cond = cond.And(builder.Eq{"reviewer_id": opts.ReviewerID})
  170. }
  171. if opts.Type != ReviewTypeUnknown {
  172. cond = cond.And(builder.Eq{"type": opts.Type})
  173. }
  174. return cond
  175. }
  176. func findReviews(e Engine, opts FindReviewOptions) ([]*Review, error) {
  177. reviews := make([]*Review, 0, 10)
  178. sess := e.Where(opts.toCond())
  179. return reviews, sess.
  180. Asc("created_unix").
  181. Asc("id").
  182. Find(&reviews)
  183. }
  184. // FindReviews returns reviews passing FindReviewOptions
  185. func FindReviews(opts FindReviewOptions) ([]*Review, error) {
  186. return findReviews(x, opts)
  187. }
  188. // CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
  189. type CreateReviewOptions struct {
  190. Content string
  191. Type ReviewType
  192. Issue *Issue
  193. Reviewer *User
  194. }
  195. func createReview(e Engine, opts CreateReviewOptions) (*Review, error) {
  196. review := &Review{
  197. Type: opts.Type,
  198. Issue: opts.Issue,
  199. IssueID: opts.Issue.ID,
  200. Reviewer: opts.Reviewer,
  201. ReviewerID: opts.Reviewer.ID,
  202. Content: opts.Content,
  203. }
  204. if _, err := e.Insert(review); err != nil {
  205. return nil, err
  206. }
  207. return review, nil
  208. }
  209. // CreateReview creates a new review based on opts
  210. func CreateReview(opts CreateReviewOptions) (*Review, error) {
  211. return createReview(x, opts)
  212. }
  213. func getCurrentReview(e Engine, reviewer *User, issue *Issue) (*Review, error) {
  214. if reviewer == nil {
  215. return nil, nil
  216. }
  217. reviews, err := findReviews(e, FindReviewOptions{
  218. Type: ReviewTypePending,
  219. IssueID: issue.ID,
  220. ReviewerID: reviewer.ID,
  221. })
  222. if err != nil {
  223. return nil, err
  224. }
  225. if len(reviews) == 0 {
  226. return nil, ErrReviewNotExist{}
  227. }
  228. reviews[0].Reviewer = reviewer
  229. reviews[0].Issue = issue
  230. return reviews[0], nil
  231. }
  232. // GetCurrentReview returns the current pending review of reviewer for given issue
  233. func GetCurrentReview(reviewer *User, issue *Issue) (*Review, error) {
  234. return getCurrentReview(x, reviewer, issue)
  235. }
  236. // UpdateReview will update all cols of the given review in db
  237. func UpdateReview(r *Review) error {
  238. if _, err := x.ID(r.ID).AllCols().Update(r); err != nil {
  239. return err
  240. }
  241. return nil
  242. }
  243. // PullReviewersWithType represents the type used to display a review overview
  244. type PullReviewersWithType struct {
  245. User `xorm:"extends"`
  246. Type ReviewType
  247. ReviewUpdatedUnix timeutil.TimeStamp `xorm:"review_updated_unix"`
  248. }
  249. // GetReviewersByPullID gets all reviewers for a pull request with the statuses
  250. func GetReviewersByPullID(pullID int64) (issueReviewers []*PullReviewersWithType, err error) {
  251. irs := []*PullReviewersWithType{}
  252. if x.Dialect().DBType() == core.MSSQL {
  253. err = x.SQL(`SELECT [user].*, review.type, review.review_updated_unix FROM
  254. (SELECT review.id, review.type, review.reviewer_id, max(review.updated_unix) as review_updated_unix
  255. FROM review WHERE review.issue_id=? AND (review.type = ? OR review.type = ?)
  256. GROUP BY review.id, review.type, review.reviewer_id) as review
  257. INNER JOIN [user] ON review.reviewer_id = [user].id ORDER BY review_updated_unix DESC`,
  258. pullID, ReviewTypeApprove, ReviewTypeReject).
  259. Find(&irs)
  260. } else {
  261. err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
  262. Table("review").
  263. Join("INNER", "`user`", "review.reviewer_id = `user`.id").
  264. Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)",
  265. pullID, ReviewTypeApprove, ReviewTypeReject).
  266. GroupBy("`user`.id, review.type").
  267. OrderBy("review_updated_unix DESC").
  268. Find(&irs)
  269. }
  270. // We need to group our results by user id _and_ review type, otherwise the query fails when using postgresql.
  271. // But becaus we're doing this, we need to manually filter out multiple reviews of different types by the
  272. // same person because we only want to show the newest review grouped by user. Thats why we're using a map here.
  273. issueReviewers = []*PullReviewersWithType{}
  274. usersInArray := make(map[int64]bool)
  275. for _, ir := range irs {
  276. if !usersInArray[ir.ID] {
  277. issueReviewers = append(issueReviewers, ir)
  278. usersInArray[ir.ID] = true
  279. }
  280. }
  281. return
  282. }
上海开阖软件有限公司 沪ICP备12045867号-1