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.

comments.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 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 comments
  5. import (
  6. "bytes"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/services/gitdiff"
  15. )
  16. // CreateIssueComment creates a plain issue comment.
  17. func CreateIssueComment(doer *models.User, repo *models.Repository, issue *models.Issue, content string, attachments []string) (*models.Comment, error) {
  18. comment, err := models.CreateComment(&models.CreateCommentOptions{
  19. Type: models.CommentTypeComment,
  20. Doer: doer,
  21. Repo: repo,
  22. Issue: issue,
  23. Content: content,
  24. Attachments: attachments,
  25. })
  26. if err != nil {
  27. return nil, err
  28. }
  29. mode, _ := models.AccessLevel(doer, repo)
  30. if err = models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
  31. Action: api.HookIssueCommentCreated,
  32. Issue: issue.APIFormat(),
  33. Comment: comment.APIFormat(),
  34. Repository: repo.APIFormat(mode),
  35. Sender: doer.APIFormat(),
  36. }); err != nil {
  37. log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
  38. } else {
  39. go models.HookQueue.Add(repo.ID)
  40. }
  41. return comment, nil
  42. }
  43. // CreateCodeComment creates a plain code comment at the specified line / path
  44. func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
  45. var commitID, patch string
  46. pr, err := models.GetPullRequestByIssueID(issue.ID)
  47. if err != nil {
  48. return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
  49. }
  50. if err := pr.GetBaseRepo(); err != nil {
  51. return nil, fmt.Errorf("GetHeadRepo: %v", err)
  52. }
  53. gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  54. if err != nil {
  55. return nil, fmt.Errorf("OpenRepository: %v", err)
  56. }
  57. // FIXME validate treePath
  58. // Get latest commit referencing the commented line
  59. // No need for get commit for base branch changes
  60. if line > 0 {
  61. commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
  62. if err == nil {
  63. commitID = commit.ID.String()
  64. } else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
  65. return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
  66. }
  67. }
  68. // Only fetch diff if comment is review comment
  69. if reviewID != 0 {
  70. headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
  71. if err != nil {
  72. return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
  73. }
  74. patchBuf := new(bytes.Buffer)
  75. if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
  76. return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
  77. }
  78. patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
  79. }
  80. return models.CreateComment(&models.CreateCommentOptions{
  81. Type: models.CommentTypeCode,
  82. Doer: doer,
  83. Repo: repo,
  84. Issue: issue,
  85. Content: content,
  86. LineNum: line,
  87. TreePath: treePath,
  88. CommitSHA: commitID,
  89. ReviewID: reviewID,
  90. Patch: patch,
  91. })
  92. }
  93. // UpdateComment updates information of comment.
  94. func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
  95. if err := models.UpdateComment(c, doer); err != nil {
  96. return err
  97. }
  98. if err := c.LoadPoster(); err != nil {
  99. return err
  100. }
  101. if err := c.LoadIssue(); err != nil {
  102. return err
  103. }
  104. if err := c.Issue.LoadAttributes(); err != nil {
  105. return err
  106. }
  107. mode, _ := models.AccessLevel(doer, c.Issue.Repo)
  108. if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
  109. Action: api.HookIssueCommentEdited,
  110. Issue: c.Issue.APIFormat(),
  111. Comment: c.APIFormat(),
  112. Changes: &api.ChangesPayload{
  113. Body: &api.ChangesFromPayload{
  114. From: oldContent,
  115. },
  116. },
  117. Repository: c.Issue.Repo.APIFormat(mode),
  118. Sender: doer.APIFormat(),
  119. }); err != nil {
  120. log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
  121. } else {
  122. go models.HookQueue.Add(c.Issue.Repo.ID)
  123. }
  124. return nil
  125. }
  126. // DeleteComment deletes the comment
  127. func DeleteComment(comment *models.Comment, doer *models.User) error {
  128. if err := models.DeleteComment(comment, doer); err != nil {
  129. return err
  130. }
  131. if err := comment.LoadPoster(); err != nil {
  132. return err
  133. }
  134. if err := comment.LoadIssue(); err != nil {
  135. return err
  136. }
  137. if err := comment.Issue.LoadAttributes(); err != nil {
  138. return err
  139. }
  140. mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
  141. if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
  142. Action: api.HookIssueCommentDeleted,
  143. Issue: comment.Issue.APIFormat(),
  144. Comment: comment.APIFormat(),
  145. Repository: comment.Issue.Repo.APIFormat(mode),
  146. Sender: doer.APIFormat(),
  147. }); err != nil {
  148. log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
  149. } else {
  150. go models.HookQueue.Add(comment.Issue.Repo.ID)
  151. }
  152. return nil
  153. }