Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_comment.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. api "code.gitea.io/sdk/gitea"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/markdown"
  14. )
  15. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  16. type CommentType int
  17. // Enumerate all the comment types
  18. const (
  19. // Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
  20. CommentTypeComment CommentType = iota
  21. CommentTypeReopen
  22. CommentTypeClose
  23. // References.
  24. CommentTypeIssueRef
  25. // Reference from a commit (not part of a pull request)
  26. CommentTypeCommitRef
  27. // Reference from a comment
  28. CommentTypeCommentRef
  29. // Reference from a pull request
  30. CommentTypePullRef
  31. // Labels changed
  32. CommentTypeLabel
  33. // Milestone changed
  34. CommentTypeMilestone
  35. // Assignees changed
  36. CommentTypeAssignees
  37. // Change Title
  38. CommentTypeChangeTitle
  39. // Delete Branch
  40. CommentTypeDeleteBranch
  41. )
  42. // CommentTag defines comment tag type
  43. type CommentTag int
  44. // Enumerate all the comment tag types
  45. const (
  46. CommentTagNone CommentTag = iota
  47. CommentTagPoster
  48. CommentTagWriter
  49. CommentTagOwner
  50. )
  51. // Comment represents a comment in commit and issue page.
  52. type Comment struct {
  53. ID int64 `xorm:"pk autoincr"`
  54. Type CommentType
  55. PosterID int64 `xorm:"INDEX"`
  56. Poster *User `xorm:"-"`
  57. IssueID int64 `xorm:"INDEX"`
  58. LabelID int64
  59. Label *Label `xorm:"-"`
  60. OldMilestoneID int64
  61. MilestoneID int64
  62. OldMilestone *Milestone `xorm:"-"`
  63. Milestone *Milestone `xorm:"-"`
  64. OldAssigneeID int64
  65. AssigneeID int64
  66. Assignee *User `xorm:"-"`
  67. OldAssignee *User `xorm:"-"`
  68. OldTitle string
  69. NewTitle string
  70. CommitID int64
  71. Line int64
  72. Content string `xorm:"TEXT"`
  73. RenderedContent string `xorm:"-"`
  74. Created time.Time `xorm:"-"`
  75. CreatedUnix int64 `xorm:"INDEX"`
  76. Updated time.Time `xorm:"-"`
  77. UpdatedUnix int64 `xorm:"INDEX"`
  78. // Reference issue in commit message
  79. CommitSHA string `xorm:"VARCHAR(40)"`
  80. Attachments []*Attachment `xorm:"-"`
  81. // For view issue page.
  82. ShowTag CommentTag `xorm:"-"`
  83. }
  84. // BeforeInsert will be invoked by XORM before inserting a record
  85. // representing this object.
  86. func (c *Comment) BeforeInsert() {
  87. c.CreatedUnix = time.Now().Unix()
  88. c.UpdatedUnix = c.CreatedUnix
  89. }
  90. // BeforeUpdate is invoked from XORM before updating this object.
  91. func (c *Comment) BeforeUpdate() {
  92. c.UpdatedUnix = time.Now().Unix()
  93. }
  94. // AfterSet is invoked from XORM after setting the value of a field of this object.
  95. func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
  96. var err error
  97. switch colName {
  98. case "id":
  99. c.Attachments, err = GetAttachmentsByCommentID(c.ID)
  100. if err != nil {
  101. log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
  102. }
  103. case "poster_id":
  104. c.Poster, err = GetUserByID(c.PosterID)
  105. if err != nil {
  106. if IsErrUserNotExist(err) {
  107. c.PosterID = -1
  108. c.Poster = NewGhostUser()
  109. } else {
  110. log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
  111. }
  112. }
  113. case "created_unix":
  114. c.Created = time.Unix(c.CreatedUnix, 0).Local()
  115. case "updated_unix":
  116. c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
  117. }
  118. }
  119. // AfterDelete is invoked from XORM after the object is deleted.
  120. func (c *Comment) AfterDelete() {
  121. _, err := DeleteAttachmentsByComment(c.ID, true)
  122. if err != nil {
  123. log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
  124. }
  125. }
  126. // HTMLURL formats a URL-string to the issue-comment
  127. func (c *Comment) HTMLURL() string {
  128. issue, err := GetIssueByID(c.IssueID)
  129. if err != nil { // Silently dropping errors :unamused:
  130. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  131. return ""
  132. }
  133. return fmt.Sprintf("%s#%s", issue.HTMLURL(), c.HashTag())
  134. }
  135. // IssueURL formats a URL-string to the issue
  136. func (c *Comment) IssueURL() string {
  137. issue, err := GetIssueByID(c.IssueID)
  138. if err != nil { // Silently dropping errors :unamused:
  139. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  140. return ""
  141. }
  142. if issue.IsPull {
  143. return ""
  144. }
  145. return issue.HTMLURL()
  146. }
  147. // PRURL formats a URL-string to the pull-request
  148. func (c *Comment) PRURL() string {
  149. issue, err := GetIssueByID(c.IssueID)
  150. if err != nil { // Silently dropping errors :unamused:
  151. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  152. return ""
  153. }
  154. if !issue.IsPull {
  155. return ""
  156. }
  157. return issue.HTMLURL()
  158. }
  159. // APIFormat converts a Comment to the api.Comment format
  160. func (c *Comment) APIFormat() *api.Comment {
  161. return &api.Comment{
  162. ID: c.ID,
  163. Poster: c.Poster.APIFormat(),
  164. HTMLURL: c.HTMLURL(),
  165. IssueURL: c.IssueURL(),
  166. PRURL: c.PRURL(),
  167. Body: c.Content,
  168. Created: c.Created,
  169. Updated: c.Updated,
  170. }
  171. }
  172. // HashTag returns unique hash tag for comment.
  173. func (c *Comment) HashTag() string {
  174. return "issuecomment-" + com.ToStr(c.ID)
  175. }
  176. // EventTag returns unique event hash tag for comment.
  177. func (c *Comment) EventTag() string {
  178. return "event-" + com.ToStr(c.ID)
  179. }
  180. // LoadLabel if comment.Type is CommentTypeLabel, then load Label
  181. func (c *Comment) LoadLabel() error {
  182. var label Label
  183. has, err := x.ID(c.LabelID).Get(&label)
  184. if err != nil {
  185. return err
  186. } else if has {
  187. c.Label = &label
  188. } else {
  189. // Ignore Label is deleted, but not clear this table
  190. log.Warn("Commit %d cannot load label %d", c.ID, c.LabelID)
  191. }
  192. return nil
  193. }
  194. // LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
  195. func (c *Comment) LoadMilestone() error {
  196. if c.OldMilestoneID > 0 {
  197. var oldMilestone Milestone
  198. has, err := x.ID(c.OldMilestoneID).Get(&oldMilestone)
  199. if err != nil {
  200. return err
  201. } else if !has {
  202. return ErrMilestoneNotExist{
  203. ID: c.OldMilestoneID,
  204. }
  205. }
  206. c.OldMilestone = &oldMilestone
  207. }
  208. if c.MilestoneID > 0 {
  209. var milestone Milestone
  210. has, err := x.ID(c.MilestoneID).Get(&milestone)
  211. if err != nil {
  212. return err
  213. } else if !has {
  214. return ErrMilestoneNotExist{
  215. ID: c.MilestoneID,
  216. }
  217. }
  218. c.Milestone = &milestone
  219. }
  220. return nil
  221. }
  222. // LoadAssignees if comment.Type is CommentTypeAssignees, then load assignees
  223. func (c *Comment) LoadAssignees() error {
  224. var err error
  225. if c.OldAssigneeID > 0 {
  226. c.OldAssignee, err = getUserByID(x, c.OldAssigneeID)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. if c.AssigneeID > 0 {
  232. c.Assignee, err = getUserByID(x, c.AssigneeID)
  233. if err != nil {
  234. return err
  235. }
  236. }
  237. return nil
  238. }
  239. // MailParticipants sends new comment emails to repository watchers
  240. // and mentioned people.
  241. func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
  242. mentions := markdown.FindAllMentions(c.Content)
  243. if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
  244. return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
  245. }
  246. switch opType {
  247. case ActionCommentIssue:
  248. issue.Content = c.Content
  249. case ActionCloseIssue:
  250. issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
  251. case ActionReopenIssue:
  252. issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
  253. }
  254. if err = mailIssueCommentToParticipants(issue, c.Poster, c, mentions); err != nil {
  255. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  256. }
  257. return nil
  258. }
  259. func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
  260. var LabelID int64
  261. if opts.Label != nil {
  262. LabelID = opts.Label.ID
  263. }
  264. comment := &Comment{
  265. Type: opts.Type,
  266. PosterID: opts.Doer.ID,
  267. Poster: opts.Doer,
  268. IssueID: opts.Issue.ID,
  269. LabelID: LabelID,
  270. OldMilestoneID: opts.OldMilestoneID,
  271. MilestoneID: opts.MilestoneID,
  272. OldAssigneeID: opts.OldAssigneeID,
  273. AssigneeID: opts.AssigneeID,
  274. CommitID: opts.CommitID,
  275. CommitSHA: opts.CommitSHA,
  276. Line: opts.LineNum,
  277. Content: opts.Content,
  278. OldTitle: opts.OldTitle,
  279. NewTitle: opts.NewTitle,
  280. }
  281. if _, err = e.Insert(comment); err != nil {
  282. return nil, err
  283. }
  284. if err = opts.Repo.getOwner(e); err != nil {
  285. return nil, err
  286. }
  287. // Compose comment action, could be plain comment, close or reopen issue/pull request.
  288. // This object will be used to notify watchers in the end of function.
  289. act := &Action{
  290. ActUserID: opts.Doer.ID,
  291. ActUser: opts.Doer,
  292. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  293. RepoID: opts.Repo.ID,
  294. Repo: opts.Repo,
  295. IsPrivate: opts.Repo.IsPrivate,
  296. }
  297. // Check comment type.
  298. switch opts.Type {
  299. case CommentTypeComment:
  300. act.OpType = ActionCommentIssue
  301. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  302. return nil, err
  303. }
  304. // Check attachments
  305. attachments := make([]*Attachment, 0, len(opts.Attachments))
  306. for _, uuid := range opts.Attachments {
  307. attach, err := getAttachmentByUUID(e, uuid)
  308. if err != nil {
  309. if IsErrAttachmentNotExist(err) {
  310. continue
  311. }
  312. return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  313. }
  314. attachments = append(attachments, attach)
  315. }
  316. for i := range attachments {
  317. attachments[i].IssueID = opts.Issue.ID
  318. attachments[i].CommentID = comment.ID
  319. // No assign value could be 0, so ignore AllCols().
  320. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  321. return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  322. }
  323. }
  324. case CommentTypeReopen:
  325. act.OpType = ActionReopenIssue
  326. if opts.Issue.IsPull {
  327. act.OpType = ActionReopenPullRequest
  328. }
  329. if opts.Issue.IsPull {
  330. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  331. } else {
  332. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  333. }
  334. if err != nil {
  335. return nil, err
  336. }
  337. case CommentTypeClose:
  338. act.OpType = ActionCloseIssue
  339. if opts.Issue.IsPull {
  340. act.OpType = ActionClosePullRequest
  341. }
  342. if opts.Issue.IsPull {
  343. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  344. } else {
  345. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  346. }
  347. if err != nil {
  348. return nil, err
  349. }
  350. }
  351. // Notify watchers for whatever action comes in, ignore if no action type.
  352. if act.OpType > 0 {
  353. if err = notifyWatchers(e, act); err != nil {
  354. log.Error(4, "notifyWatchers: %v", err)
  355. }
  356. if err = comment.MailParticipants(e, act.OpType, opts.Issue); err != nil {
  357. log.Error(4, "MailParticipants: %v", err)
  358. }
  359. }
  360. return comment, nil
  361. }
  362. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  363. cmtType := CommentTypeClose
  364. if !issue.IsClosed {
  365. cmtType = CommentTypeReopen
  366. }
  367. return createComment(e, &CreateCommentOptions{
  368. Type: cmtType,
  369. Doer: doer,
  370. Repo: repo,
  371. Issue: issue,
  372. })
  373. }
  374. func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, label *Label, add bool) (*Comment, error) {
  375. var content string
  376. if add {
  377. content = "1"
  378. }
  379. return createComment(e, &CreateCommentOptions{
  380. Type: CommentTypeLabel,
  381. Doer: doer,
  382. Repo: repo,
  383. Issue: issue,
  384. Label: label,
  385. Content: content,
  386. })
  387. }
  388. func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
  389. return createComment(e, &CreateCommentOptions{
  390. Type: CommentTypeMilestone,
  391. Doer: doer,
  392. Repo: repo,
  393. Issue: issue,
  394. OldMilestoneID: oldMilestoneID,
  395. MilestoneID: milestoneID,
  396. })
  397. }
  398. func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldAssigneeID, assigneeID int64) (*Comment, error) {
  399. return createComment(e, &CreateCommentOptions{
  400. Type: CommentTypeAssignees,
  401. Doer: doer,
  402. Repo: repo,
  403. Issue: issue,
  404. OldAssigneeID: oldAssigneeID,
  405. AssigneeID: assigneeID,
  406. })
  407. }
  408. func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
  409. return createComment(e, &CreateCommentOptions{
  410. Type: CommentTypeChangeTitle,
  411. Doer: doer,
  412. Repo: repo,
  413. Issue: issue,
  414. OldTitle: oldTitle,
  415. NewTitle: newTitle,
  416. })
  417. }
  418. func createDeleteBranchComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, branchName string) (*Comment, error) {
  419. return createComment(e, &CreateCommentOptions{
  420. Type: CommentTypeDeleteBranch,
  421. Doer: doer,
  422. Repo: repo,
  423. Issue: issue,
  424. CommitSHA: branchName,
  425. })
  426. }
  427. // CreateCommentOptions defines options for creating comment
  428. type CreateCommentOptions struct {
  429. Type CommentType
  430. Doer *User
  431. Repo *Repository
  432. Issue *Issue
  433. Label *Label
  434. OldMilestoneID int64
  435. MilestoneID int64
  436. OldAssigneeID int64
  437. AssigneeID int64
  438. OldTitle string
  439. NewTitle string
  440. CommitID int64
  441. CommitSHA string
  442. LineNum int64
  443. Content string
  444. Attachments []string // UUIDs of attachments
  445. }
  446. // CreateComment creates comment of issue or commit.
  447. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  448. sess := x.NewSession()
  449. defer sessionRelease(sess)
  450. if err = sess.Begin(); err != nil {
  451. return nil, err
  452. }
  453. comment, err = createComment(sess, opts)
  454. if err != nil {
  455. return nil, err
  456. }
  457. return comment, sess.Commit()
  458. }
  459. // CreateIssueComment creates a plain issue comment.
  460. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  461. return CreateComment(&CreateCommentOptions{
  462. Type: CommentTypeComment,
  463. Doer: doer,
  464. Repo: repo,
  465. Issue: issue,
  466. Content: content,
  467. Attachments: attachments,
  468. })
  469. }
  470. // CreateRefComment creates a commit reference comment to issue.
  471. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  472. if len(commitSHA) == 0 {
  473. return fmt.Errorf("cannot create reference with empty commit SHA")
  474. }
  475. // Check if same reference from same commit has already existed.
  476. has, err := x.Get(&Comment{
  477. Type: CommentTypeCommitRef,
  478. IssueID: issue.ID,
  479. CommitSHA: commitSHA,
  480. })
  481. if err != nil {
  482. return fmt.Errorf("check reference comment: %v", err)
  483. } else if has {
  484. return nil
  485. }
  486. _, err = CreateComment(&CreateCommentOptions{
  487. Type: CommentTypeCommitRef,
  488. Doer: doer,
  489. Repo: repo,
  490. Issue: issue,
  491. CommitSHA: commitSHA,
  492. Content: content,
  493. })
  494. return err
  495. }
  496. // GetCommentByID returns the comment by given ID.
  497. func GetCommentByID(id int64) (*Comment, error) {
  498. c := new(Comment)
  499. has, err := x.Id(id).Get(c)
  500. if err != nil {
  501. return nil, err
  502. } else if !has {
  503. return nil, ErrCommentNotExist{id, 0}
  504. }
  505. return c, nil
  506. }
  507. func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
  508. comments := make([]*Comment, 0, 10)
  509. sess := e.
  510. Where("issue_id = ?", issueID).
  511. Asc("created_unix")
  512. if since > 0 {
  513. sess.And("updated_unix >= ?", since)
  514. }
  515. return comments, sess.Find(&comments)
  516. }
  517. func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
  518. comments := make([]*Comment, 0, 10)
  519. sess := e.Where("issue.repo_id = ?", repoID).
  520. Join("INNER", "issue", "issue.id = comment.issue_id").
  521. Asc("comment.created_unix")
  522. if since > 0 {
  523. sess.And("comment.updated_unix >= ?", since)
  524. }
  525. return comments, sess.Find(&comments)
  526. }
  527. func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
  528. return getCommentsByIssueIDSince(e, issueID, -1)
  529. }
  530. // GetCommentsByIssueID returns all comments of an issue.
  531. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  532. return getCommentsByIssueID(x, issueID)
  533. }
  534. // GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
  535. func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
  536. return getCommentsByIssueIDSince(x, issueID, since)
  537. }
  538. // GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
  539. func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
  540. return getCommentsByRepoIDSince(x, repoID, since)
  541. }
  542. // UpdateComment updates information of comment.
  543. func UpdateComment(c *Comment) error {
  544. _, err := x.Id(c.ID).AllCols().Update(c)
  545. return err
  546. }
  547. // DeleteComment deletes the comment
  548. func DeleteComment(comment *Comment) error {
  549. sess := x.NewSession()
  550. defer sessionRelease(sess)
  551. if err := sess.Begin(); err != nil {
  552. return err
  553. }
  554. if _, err := sess.Delete(&Comment{
  555. ID: comment.ID,
  556. }); err != nil {
  557. return err
  558. }
  559. if comment.Type == CommentTypeComment {
  560. if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
  561. return err
  562. }
  563. }
  564. return sess.Commit()
  565. }