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.

196 lines
5.6 KiB

  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "bytes"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/services/gitdiff"
  15. )
  16. // CreateCodeComment creates a comment on the code line
  17. func CreateCodeComment(doer *models.User, gitRepo *git.Repository, issue *models.Issue, line int64, content string, treePath string, isReview bool, replyReviewID int64, latestCommitID string) (*models.Comment, error) {
  18. var (
  19. existsReview bool
  20. err error
  21. )
  22. // CreateCodeComment() is used for:
  23. // - Single comments
  24. // - Comments that are part of a review
  25. // - Comments that reply to an existing review
  26. if !isReview {
  27. // It's not part of a review; maybe a reply to a review comment or a single comment.
  28. // Check if there are reviews for that line already; if there are, this is a reply
  29. if existsReview, err = models.ReviewExists(issue, treePath, line); err != nil {
  30. return nil, err
  31. }
  32. }
  33. // Comments that are replies don't require a review header to show up in the issue view
  34. if !isReview && existsReview {
  35. if err = issue.LoadRepo(); err != nil {
  36. return nil, err
  37. }
  38. comment, err := createCodeComment(
  39. doer,
  40. issue.Repo,
  41. issue,
  42. content,
  43. treePath,
  44. line,
  45. replyReviewID,
  46. )
  47. if err != nil {
  48. return nil, err
  49. }
  50. notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment)
  51. return comment, nil
  52. }
  53. review, err := models.GetCurrentReview(doer, issue)
  54. if err != nil {
  55. if !models.IsErrReviewNotExist(err) {
  56. return nil, err
  57. }
  58. review, err = models.CreateReview(models.CreateReviewOptions{
  59. Type: models.ReviewTypePending,
  60. Reviewer: doer,
  61. Issue: issue,
  62. Official: false,
  63. CommitID: latestCommitID,
  64. })
  65. if err != nil {
  66. return nil, err
  67. }
  68. }
  69. comment, err := createCodeComment(
  70. doer,
  71. issue.Repo,
  72. issue,
  73. content,
  74. treePath,
  75. line,
  76. review.ID,
  77. )
  78. if err != nil {
  79. return nil, err
  80. }
  81. if !isReview && !existsReview {
  82. // Submit the review we've just created so the comment shows up in the issue view
  83. if _, _, err = SubmitReview(doer, gitRepo, issue, models.ReviewTypeComment, "", latestCommitID); err != nil {
  84. return nil, err
  85. }
  86. }
  87. // NOTICE: if it's a pending review the notifications will not be fired until user submit review.
  88. return comment, nil
  89. }
  90. // createCodeComment creates a plain code comment at the specified line / path
  91. func createCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
  92. var commitID, patch string
  93. if err := issue.LoadPullRequest(); err != nil {
  94. return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
  95. }
  96. pr := issue.PullRequest
  97. if err := pr.GetBaseRepo(); err != nil {
  98. return nil, fmt.Errorf("GetHeadRepo: %v", err)
  99. }
  100. gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  101. if err != nil {
  102. return nil, fmt.Errorf("OpenRepository: %v", err)
  103. }
  104. defer gitRepo.Close()
  105. // FIXME validate treePath
  106. // Get latest commit referencing the commented line
  107. // No need for get commit for base branch changes
  108. if line > 0 {
  109. commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
  110. if err == nil {
  111. commitID = commit.ID.String()
  112. } else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
  113. return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
  114. }
  115. }
  116. // Only fetch diff if comment is review comment
  117. if reviewID != 0 {
  118. headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
  119. if err != nil {
  120. return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
  121. }
  122. patchBuf := new(bytes.Buffer)
  123. if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
  124. return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
  125. }
  126. patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
  127. }
  128. return models.CreateComment(&models.CreateCommentOptions{
  129. Type: models.CommentTypeCode,
  130. Doer: doer,
  131. Repo: repo,
  132. Issue: issue,
  133. Content: content,
  134. LineNum: line,
  135. TreePath: treePath,
  136. CommitSHA: commitID,
  137. ReviewID: reviewID,
  138. Patch: patch,
  139. })
  140. }
  141. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  142. func SubmitReview(doer *models.User, gitRepo *git.Repository, issue *models.Issue, reviewType models.ReviewType, content, commitID string) (*models.Review, *models.Comment, error) {
  143. pr, err := issue.GetPullRequest()
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. var stale bool
  148. if reviewType != models.ReviewTypeApprove && reviewType != models.ReviewTypeReject {
  149. stale = false
  150. } else {
  151. headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
  152. if err != nil {
  153. return nil, nil, err
  154. }
  155. if headCommitID == commitID {
  156. stale = false
  157. } else {
  158. stale, err = checkIfPRContentChanged(pr, commitID, headCommitID)
  159. if err != nil {
  160. return nil, nil, err
  161. }
  162. }
  163. }
  164. review, comm, err := models.SubmitReview(doer, issue, reviewType, content, commitID, stale)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. notification.NotifyPullRequestReview(pr, review, comm)
  169. return review, comm, nil
  170. }