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.

pull_review.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 repo
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/notification"
  12. )
  13. // CreateCodeComment will create a code comment including an pending review if required
  14. func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
  15. issue := GetActionIssue(ctx)
  16. if !issue.IsPull {
  17. return
  18. }
  19. if ctx.Written() {
  20. return
  21. }
  22. if ctx.HasError() {
  23. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  24. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  25. return
  26. }
  27. var comment *models.Comment
  28. defer func() {
  29. if comment != nil {
  30. ctx.Redirect(comment.HTMLURL())
  31. } else {
  32. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  33. }
  34. }()
  35. signedLine := form.Line
  36. if form.Side == "previous" {
  37. signedLine *= -1
  38. }
  39. review := new(models.Review)
  40. if form.IsReview {
  41. var err error
  42. // Check if the user has already a pending review for this issue
  43. if review, err = models.GetCurrentReview(ctx.User, issue); err != nil {
  44. if !models.IsErrReviewNotExist(err) {
  45. ctx.ServerError("CreateCodeComment", err)
  46. return
  47. }
  48. // No pending review exists
  49. // Create a new pending review for this issue & user
  50. if review, err = models.CreateReview(models.CreateReviewOptions{
  51. Type: models.ReviewTypePending,
  52. Reviewer: ctx.User,
  53. Issue: issue,
  54. }); err != nil {
  55. ctx.ServerError("CreateCodeComment", err)
  56. return
  57. }
  58. }
  59. }
  60. //FIXME check if line, commit and treepath exist
  61. comment, err := models.CreateCodeComment(
  62. ctx.User,
  63. issue.Repo,
  64. issue,
  65. form.Content,
  66. form.TreePath,
  67. signedLine,
  68. review.ID,
  69. )
  70. if err != nil {
  71. ctx.ServerError("CreateCodeComment", err)
  72. return
  73. }
  74. // Send no notification if comment is pending
  75. if !form.IsReview {
  76. notification.NotifyCreateIssueComment(ctx.User, issue.Repo, issue, comment)
  77. }
  78. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  79. }
  80. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  81. func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
  82. issue := GetActionIssue(ctx)
  83. if !issue.IsPull {
  84. return
  85. }
  86. if ctx.Written() {
  87. return
  88. }
  89. if ctx.HasError() {
  90. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  91. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  92. return
  93. }
  94. var review *models.Review
  95. var err error
  96. reviewType := form.ReviewType()
  97. switch reviewType {
  98. case models.ReviewTypeUnknown:
  99. ctx.ServerError("GetCurrentReview", fmt.Errorf("unknown ReviewType: %s", form.Type))
  100. return
  101. // can not approve/reject your own PR
  102. case models.ReviewTypeApprove, models.ReviewTypeReject:
  103. if issue.Poster.ID == ctx.User.ID {
  104. var translated string
  105. if reviewType == models.ReviewTypeApprove {
  106. translated = ctx.Tr("repo.issues.review.self.approval")
  107. } else {
  108. translated = ctx.Tr("repo.issues.review.self.rejection")
  109. }
  110. ctx.Flash.Error(translated)
  111. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  112. return
  113. }
  114. }
  115. review, err = models.GetCurrentReview(ctx.User, issue)
  116. if err == nil {
  117. review.Issue = issue
  118. if errl := review.LoadCodeComments(); errl != nil {
  119. ctx.ServerError("LoadCodeComments", err)
  120. return
  121. }
  122. }
  123. if ((err == nil && len(review.CodeComments) == 0) ||
  124. (err != nil && models.IsErrReviewNotExist(err))) &&
  125. form.HasEmptyContent() {
  126. ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
  127. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  128. return
  129. }
  130. if err != nil {
  131. if !models.IsErrReviewNotExist(err) {
  132. ctx.ServerError("GetCurrentReview", err)
  133. return
  134. }
  135. // No current review. Create a new one!
  136. if review, err = models.CreateReview(models.CreateReviewOptions{
  137. Type: reviewType,
  138. Issue: issue,
  139. Reviewer: ctx.User,
  140. Content: form.Content,
  141. }); err != nil {
  142. ctx.ServerError("CreateReview", err)
  143. return
  144. }
  145. } else {
  146. review.Content = form.Content
  147. review.Type = reviewType
  148. if err = models.UpdateReview(review); err != nil {
  149. ctx.ServerError("UpdateReview", err)
  150. return
  151. }
  152. }
  153. comm, err := models.CreateComment(&models.CreateCommentOptions{
  154. Type: models.CommentTypeReview,
  155. Doer: ctx.User,
  156. Content: review.Content,
  157. Issue: issue,
  158. Repo: issue.Repo,
  159. ReviewID: review.ID,
  160. })
  161. if err != nil || comm == nil {
  162. ctx.ServerError("CreateComment", err)
  163. return
  164. }
  165. if err = review.Publish(); err != nil {
  166. ctx.ServerError("Publish", err)
  167. return
  168. }
  169. pr, err := issue.GetPullRequest()
  170. if err != nil {
  171. ctx.ServerError("GetPullRequest", err)
  172. return
  173. }
  174. notification.NotifyPullRequestReview(pr, review, comm)
  175. ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
  176. }