issue_comment.go 8.1 KB

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