本站源代码
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.

203 lines
5.2KB

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