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.

review.go 5.5KB

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