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.

issue_comment.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2016 The Gogs 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 models
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/gogs/modules/log"
  12. )
  13. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  14. type CommentType int
  15. const (
  16. // Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
  17. COMMENT_TYPE_COMMENT CommentType = iota
  18. COMMENT_TYPE_REOPEN
  19. COMMENT_TYPE_CLOSE
  20. // References.
  21. COMMENT_TYPE_ISSUE_REF
  22. // Reference from a commit (not part of a pull request)
  23. COMMENT_TYPE_COMMIT_REF
  24. // Reference from a comment
  25. COMMENT_TYPE_COMMENT_REF
  26. // Reference from a pull request
  27. COMMENT_TYPE_PULL_REF
  28. )
  29. type CommentTag int
  30. const (
  31. COMMENT_TAG_NONE CommentTag = iota
  32. COMMENT_TAG_POSTER
  33. COMMENT_TAG_ADMIN
  34. COMMENT_TAG_OWNER
  35. )
  36. // Comment represents a comment in commit and issue page.
  37. type Comment struct {
  38. ID int64 `xorm:"pk autoincr"`
  39. Type CommentType
  40. PosterID int64
  41. Poster *User `xorm:"-"`
  42. IssueID int64 `xorm:"INDEX"`
  43. CommitID int64
  44. Line int64
  45. Content string `xorm:"TEXT"`
  46. RenderedContent string `xorm:"-"`
  47. Created time.Time `xorm:"CREATED"`
  48. // Reference issue in commit message
  49. CommitSHA string `xorm:"VARCHAR(40)"`
  50. Attachments []*Attachment `xorm:"-"`
  51. // For view issue page.
  52. ShowTag CommentTag `xorm:"-"`
  53. }
  54. func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
  55. var err error
  56. switch colName {
  57. case "id":
  58. c.Attachments, err = GetAttachmentsByCommentID(c.ID)
  59. if err != nil {
  60. log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
  61. }
  62. case "poster_id":
  63. c.Poster, err = GetUserByID(c.PosterID)
  64. if err != nil {
  65. if IsErrUserNotExist(err) {
  66. c.PosterID = -1
  67. c.Poster = NewFakeUser()
  68. } else {
  69. log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
  70. }
  71. }
  72. case "created":
  73. c.Created = regulateTimeZone(c.Created)
  74. }
  75. }
  76. func (c *Comment) AfterDelete() {
  77. _, err := DeleteAttachmentsByComment(c.ID, true)
  78. if err != nil {
  79. log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
  80. }
  81. }
  82. // HashTag returns unique hash tag for comment.
  83. func (c *Comment) HashTag() string {
  84. return "issuecomment-" + com.ToStr(c.ID)
  85. }
  86. // EventTag returns unique event hash tag for comment.
  87. func (c *Comment) EventTag() string {
  88. return "event-" + com.ToStr(c.ID)
  89. }
  90. func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
  91. comment := &Comment{
  92. Type: opts.Type,
  93. PosterID: opts.Doer.Id,
  94. IssueID: opts.Issue.ID,
  95. CommitID: opts.CommitID,
  96. CommitSHA: opts.CommitSHA,
  97. Line: opts.LineNum,
  98. Content: opts.Content,
  99. }
  100. if _, err = e.Insert(comment); err != nil {
  101. return nil, err
  102. }
  103. // Compose comment action, could be plain comment, close or reopen issue/pull request.
  104. // This object will be used to notify watchers in the end of function.
  105. act := &Action{
  106. ActUserID: opts.Doer.Id,
  107. ActUserName: opts.Doer.Name,
  108. ActEmail: opts.Doer.Email,
  109. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  110. RepoID: opts.Repo.ID,
  111. RepoUserName: opts.Repo.Owner.Name,
  112. RepoName: opts.Repo.Name,
  113. IsPrivate: opts.Repo.IsPrivate,
  114. }
  115. // Check comment type.
  116. switch opts.Type {
  117. case COMMENT_TYPE_COMMENT:
  118. act.OpType = ACTION_COMMENT_ISSUE
  119. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  120. return nil, err
  121. }
  122. // Check attachments
  123. attachments := make([]*Attachment, 0, len(opts.Attachments))
  124. for _, uuid := range opts.Attachments {
  125. attach, err := getAttachmentByUUID(e, uuid)
  126. if err != nil {
  127. if IsErrAttachmentNotExist(err) {
  128. continue
  129. }
  130. return nil, fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err)
  131. }
  132. attachments = append(attachments, attach)
  133. }
  134. for i := range attachments {
  135. attachments[i].IssueID = opts.Issue.ID
  136. attachments[i].CommentID = comment.ID
  137. // No assign value could be 0, so ignore AllCols().
  138. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  139. return nil, fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err)
  140. }
  141. }
  142. case COMMENT_TYPE_REOPEN:
  143. act.OpType = ACTION_REOPEN_ISSUE
  144. if opts.Issue.IsPull {
  145. act.OpType = ACTION_REOPEN_PULL_REQUEST
  146. }
  147. if opts.Issue.IsPull {
  148. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  149. } else {
  150. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  151. }
  152. if err != nil {
  153. return nil, err
  154. }
  155. case COMMENT_TYPE_CLOSE:
  156. act.OpType = ACTION_CLOSE_ISSUE
  157. if opts.Issue.IsPull {
  158. act.OpType = ACTION_CLOSE_PULL_REQUEST
  159. }
  160. if opts.Issue.IsPull {
  161. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  162. } else {
  163. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  164. }
  165. if err != nil {
  166. return nil, err
  167. }
  168. }
  169. // Notify watchers for whatever action comes in
  170. if err = notifyWatchers(e, act); err != nil {
  171. return nil, fmt.Errorf("notifyWatchers: %v", err)
  172. }
  173. return comment, nil
  174. }
  175. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  176. cmtType := COMMENT_TYPE_CLOSE
  177. if !issue.IsClosed {
  178. cmtType = COMMENT_TYPE_REOPEN
  179. }
  180. return createComment(e, &CreateCommentOptions{
  181. Type: cmtType,
  182. Doer: doer,
  183. Repo: repo,
  184. Issue: issue,
  185. })
  186. }
  187. type CreateCommentOptions struct {
  188. Type CommentType
  189. Doer *User
  190. Repo *Repository
  191. Issue *Issue
  192. CommitID int64
  193. CommitSHA string
  194. LineNum int64
  195. Content string
  196. Attachments []string // UUIDs of attachments
  197. }
  198. // CreateComment creates comment of issue or commit.
  199. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  200. sess := x.NewSession()
  201. defer sessionRelease(sess)
  202. if err = sess.Begin(); err != nil {
  203. return nil, err
  204. }
  205. comment, err = createComment(sess, opts)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return comment, sess.Commit()
  210. }
  211. // CreateIssueComment creates a plain issue comment.
  212. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  213. return CreateComment(&CreateCommentOptions{
  214. Type: COMMENT_TYPE_COMMENT,
  215. Doer: doer,
  216. Repo: repo,
  217. Issue: issue,
  218. Content: content,
  219. Attachments: attachments,
  220. })
  221. }
  222. // CreateRefComment creates a commit reference comment to issue.
  223. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  224. if len(commitSHA) == 0 {
  225. return fmt.Errorf("cannot create reference with empty commit SHA")
  226. }
  227. // Check if same reference from same commit has already existed.
  228. has, err := x.Get(&Comment{
  229. Type: COMMENT_TYPE_COMMIT_REF,
  230. IssueID: issue.ID,
  231. CommitSHA: commitSHA,
  232. })
  233. if err != nil {
  234. return fmt.Errorf("check reference comment: %v", err)
  235. } else if has {
  236. return nil
  237. }
  238. _, err = CreateComment(&CreateCommentOptions{
  239. Type: COMMENT_TYPE_COMMIT_REF,
  240. Doer: doer,
  241. Repo: repo,
  242. Issue: issue,
  243. CommitSHA: commitSHA,
  244. Content: content,
  245. })
  246. return err
  247. }
  248. // GetCommentByID returns the comment by given ID.
  249. func GetCommentByID(id int64) (*Comment, error) {
  250. c := new(Comment)
  251. has, err := x.Id(id).Get(c)
  252. if err != nil {
  253. return nil, err
  254. } else if !has {
  255. return nil, ErrCommentNotExist{id}
  256. }
  257. return c, nil
  258. }
  259. // GetCommentsByIssueID returns all comments of issue by given ID.
  260. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  261. comments := make([]*Comment, 0, 10)
  262. return comments, x.Where("issue_id=?", issueID).Asc("created").Find(&comments)
  263. }
  264. // UpdateComment updates information of comment.
  265. func UpdateComment(c *Comment) error {
  266. _, err := x.Id(c.ID).AllCols().Update(c)
  267. return err
  268. }