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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, issue *models.Issue, line int64, content string, treePath string, isReview bool, replyReviewID int64) (*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. })
  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, issue, models.ReviewTypeComment, ""); 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.GetBaseRepo(); err != nil {
  97. return nil, fmt.Errorf("GetHeadRepo: %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 := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.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 = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
  126. }
  127. return models.CreateCommentWithNoAction(&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, issue *models.Issue, reviewType models.ReviewType, content string) (*models.Review, *models.Comment, error) {
  142. review, comm, err := models.SubmitReview(doer, issue, reviewType, content)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. pr, err := issue.GetPullRequest()
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. notification.NotifyPullRequestReview(pr, review, comm)
  151. return review, comm, nil
  152. }