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

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