gitea源码

comments.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/gitrepo"
  14. "code.gitea.io/gitea/modules/json"
  15. "code.gitea.io/gitea/modules/timeutil"
  16. git_service "code.gitea.io/gitea/services/git"
  17. notify_service "code.gitea.io/gitea/services/notify"
  18. )
  19. // CreateRefComment creates a commit reference comment to issue.
  20. func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, commitSHA string) error {
  21. if len(commitSHA) == 0 {
  22. return errors.New("cannot create reference with empty commit SHA")
  23. }
  24. if user_model.IsUserBlockedBy(ctx, doer, issue.PosterID, repo.OwnerID) {
  25. if isAdmin, _ := access_model.IsUserRepoAdmin(ctx, repo, doer); !isAdmin {
  26. return user_model.ErrBlockedUser
  27. }
  28. }
  29. // Check if same reference from same commit has already existed.
  30. has, err := db.GetEngine(ctx).Get(&issues_model.Comment{
  31. Type: issues_model.CommentTypeCommitRef,
  32. IssueID: issue.ID,
  33. CommitSHA: commitSHA,
  34. })
  35. if err != nil {
  36. return fmt.Errorf("check reference comment: %w", err)
  37. } else if has {
  38. return nil
  39. }
  40. _, err = issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
  41. Type: issues_model.CommentTypeCommitRef,
  42. Doer: doer,
  43. Repo: repo,
  44. Issue: issue,
  45. CommitSHA: commitSHA,
  46. Content: content,
  47. })
  48. return err
  49. }
  50. // CreateIssueComment creates a plain issue comment.
  51. func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) {
  52. if user_model.IsUserBlockedBy(ctx, doer, issue.PosterID, repo.OwnerID) {
  53. if isAdmin, _ := access_model.IsUserRepoAdmin(ctx, repo, doer); !isAdmin {
  54. return nil, user_model.ErrBlockedUser
  55. }
  56. }
  57. comment, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
  58. Type: issues_model.CommentTypeComment,
  59. Doer: doer,
  60. Repo: repo,
  61. Issue: issue,
  62. Content: content,
  63. Attachments: attachments,
  64. })
  65. if err != nil {
  66. return nil, err
  67. }
  68. mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, doer, comment.Content)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // reload issue to ensure it has the latest data, especially the number of comments
  73. issue, err = issues_model.GetIssueByID(ctx, issue.ID)
  74. if err != nil {
  75. return nil, err
  76. }
  77. notify_service.CreateIssueComment(ctx, doer, repo, issue, comment, mentions)
  78. return comment, nil
  79. }
  80. // UpdateComment updates information of comment.
  81. func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion int, doer *user_model.User, oldContent string) error {
  82. if err := c.LoadIssue(ctx); err != nil {
  83. return err
  84. }
  85. if err := c.Issue.LoadRepo(ctx); err != nil {
  86. return err
  87. }
  88. if user_model.IsUserBlockedBy(ctx, doer, c.Issue.PosterID, c.Issue.Repo.OwnerID) {
  89. if isAdmin, _ := access_model.IsUserRepoAdmin(ctx, c.Issue.Repo, doer); !isAdmin {
  90. return user_model.ErrBlockedUser
  91. }
  92. }
  93. needsContentHistory := c.Content != oldContent && c.Type.HasContentSupport()
  94. if needsContentHistory {
  95. hasContentHistory, err := issues_model.HasIssueContentHistory(ctx, c.IssueID, c.ID)
  96. if err != nil {
  97. return err
  98. }
  99. if !hasContentHistory {
  100. if err = issues_model.SaveIssueContentHistory(ctx, c.PosterID, c.IssueID, c.ID,
  101. c.CreatedUnix, oldContent, true); err != nil {
  102. return err
  103. }
  104. }
  105. }
  106. if err := issues_model.UpdateComment(ctx, c, contentVersion, doer); err != nil {
  107. return err
  108. }
  109. if needsContentHistory {
  110. err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. notify_service.UpdateComment(ctx, doer, c, oldContent)
  116. return nil
  117. }
  118. // DeleteComment deletes the comment
  119. func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) error {
  120. err := db.WithTx(ctx, func(ctx context.Context) error {
  121. return issues_model.DeleteComment(ctx, comment)
  122. })
  123. if err != nil {
  124. return err
  125. }
  126. notify_service.DeleteComment(ctx, doer, comment)
  127. return nil
  128. }
  129. // LoadCommentPushCommits Load push commits
  130. func LoadCommentPushCommits(ctx context.Context, c *issues_model.Comment) (err error) {
  131. if c.Content == "" || c.Commits != nil || c.Type != issues_model.CommentTypePullRequestPush {
  132. return nil
  133. }
  134. var data issues_model.PushActionContent
  135. err = json.Unmarshal([]byte(c.Content), &data)
  136. if err != nil {
  137. return err
  138. }
  139. c.IsForcePush = data.IsForcePush
  140. if c.IsForcePush {
  141. if len(data.CommitIDs) != 2 {
  142. return nil
  143. }
  144. c.OldCommit = data.CommitIDs[0]
  145. c.NewCommit = data.CommitIDs[1]
  146. } else {
  147. gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, c.Issue.Repo)
  148. if err != nil {
  149. return err
  150. }
  151. defer closer.Close()
  152. c.Commits, err = git_service.ConvertFromGitCommit(ctx, gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
  153. if err != nil {
  154. return err
  155. }
  156. c.CommitsNum = int64(len(c.Commits))
  157. }
  158. return err
  159. }