You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

review.go 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. })
  63. if err != nil {
  64. return nil, err
  65. }
  66. }
  67. comment, err := createCodeComment(
  68. doer,
  69. issue.Repo,
  70. issue,
  71. content,
  72. treePath,
  73. line,
  74. review.ID,
  75. )
  76. if err != nil {
  77. return nil, err
  78. }
  79. if !isReview && !existsReview {
  80. // Submit the review we've just created so the comment shows up in the issue view
  81. if _, _, err = SubmitReview(doer, issue, models.ReviewTypeComment, ""); err != nil {
  82. return nil, err
  83. }
  84. }
  85. // NOTICE: if it's a pending review the notifications will not be fired until user submit review.
  86. return comment, nil
  87. }
  88. // createCodeComment creates a plain code comment at the specified line / path
  89. func createCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
  90. var commitID, patch string
  91. if err := issue.LoadPullRequest(); err != nil {
  92. return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
  93. }
  94. pr := issue.PullRequest
  95. if err := pr.GetBaseRepo(); err != nil {
  96. return nil, fmt.Errorf("GetHeadRepo: %v", err)
  97. }
  98. gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  99. if err != nil {
  100. return nil, fmt.Errorf("OpenRepository: %v", err)
  101. }
  102. defer gitRepo.Close()
  103. // FIXME validate treePath
  104. // Get latest commit referencing the commented line
  105. // No need for get commit for base branch changes
  106. if line > 0 {
  107. commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
  108. if err == nil {
  109. commitID = commit.ID.String()
  110. } else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
  111. return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
  112. }
  113. }
  114. // Only fetch diff if comment is review comment
  115. if reviewID != 0 {
  116. headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
  117. if err != nil {
  118. return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
  119. }
  120. patchBuf := new(bytes.Buffer)
  121. if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
  122. return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
  123. }
  124. patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
  125. }
  126. return models.CreateCommentWithNoAction(&models.CreateCommentOptions{
  127. Type: models.CommentTypeCode,
  128. Doer: doer,
  129. Repo: repo,
  130. Issue: issue,
  131. Content: content,
  132. LineNum: line,
  133. TreePath: treePath,
  134. CommitSHA: commitID,
  135. ReviewID: reviewID,
  136. Patch: patch,
  137. })
  138. }
  139. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  140. func SubmitReview(doer *models.User, issue *models.Issue, reviewType models.ReviewType, content string) (*models.Review, *models.Comment, error) {
  141. review, comm, err := models.SubmitReview(doer, issue, reviewType, content)
  142. if err != nil {
  143. return nil, nil, err
  144. }
  145. pr, err := issue.GetPullRequest()
  146. if err != nil {
  147. return nil, nil, err
  148. }
  149. notification.NotifyPullRequestReview(pr, review, comm)
  150. return review, comm, nil
  151. }