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

issue_comment.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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#issuecomment-%d", issue.HTMLURL(), c.ID)
  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, 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. ActUserName: opts.Doer.Name,
  292. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  293. RepoID: opts.Repo.ID,
  294. RepoUserName: opts.Repo.Owner.Name,
  295. RepoName: opts.Repo.Name,
  296. IsPrivate: opts.Repo.IsPrivate,
  297. }
  298. // Check comment type.
  299. switch opts.Type {
  300. case CommentTypeComment:
  301. act.OpType = ActionCommentIssue
  302. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  303. return nil, err
  304. }
  305. // Check attachments
  306. attachments := make([]*Attachment, 0, len(opts.Attachments))
  307. for _, uuid := range opts.Attachments {
  308. attach, err := getAttachmentByUUID(e, uuid)
  309. if err != nil {
  310. if IsErrAttachmentNotExist(err) {
  311. continue
  312. }
  313. return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  314. }
  315. attachments = append(attachments, attach)
  316. }
  317. for i := range attachments {
  318. attachments[i].IssueID = opts.Issue.ID
  319. attachments[i].CommentID = comment.ID
  320. // No assign value could be 0, so ignore AllCols().
  321. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  322. return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  323. }
  324. }
  325. case CommentTypeReopen:
  326. act.OpType = ActionReopenIssue
  327. if opts.Issue.IsPull {
  328. act.OpType = ActionReopenPullRequest
  329. }
  330. if opts.Issue.IsPull {
  331. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  332. } else {
  333. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  334. }
  335. if err != nil {
  336. return nil, err
  337. }
  338. case CommentTypeClose:
  339. act.OpType = ActionCloseIssue
  340. if opts.Issue.IsPull {
  341. act.OpType = ActionClosePullRequest
  342. }
  343. if opts.Issue.IsPull {
  344. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  345. } else {
  346. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  347. }
  348. if err != nil {
  349. return nil, err
  350. }
  351. }
  352. // Notify watchers for whatever action comes in, ignore if no action type.
  353. if act.OpType > 0 {
  354. if err = notifyWatchers(e, act); err != nil {
  355. log.Error(4, "notifyWatchers: %v", err)
  356. }
  357. if err = comment.MailParticipants(e, act.OpType, opts.Issue); err != nil {
  358. log.Error(4, "MailParticipants: %v", err)
  359. }
  360. }
  361. return comment, nil
  362. }
  363. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  364. cmtType := CommentTypeClose
  365. if !issue.IsClosed {
  366. cmtType = CommentTypeReopen
  367. }
  368. return createComment(e, &CreateCommentOptions{
  369. Type: cmtType,
  370. Doer: doer,
  371. Repo: repo,
  372. Issue: issue,
  373. })
  374. }
  375. func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, label *Label, add bool) (*Comment, error) {
  376. var content string
  377. if add {
  378. content = "1"
  379. }
  380. return createComment(e, &CreateCommentOptions{
  381. Type: CommentTypeLabel,
  382. Doer: doer,
  383. Repo: repo,
  384. Issue: issue,
  385. Label: label,
  386. Content: content,
  387. })
  388. }
  389. func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
  390. return createComment(e, &CreateCommentOptions{
  391. Type: CommentTypeMilestone,
  392. Doer: doer,
  393. Repo: repo,
  394. Issue: issue,
  395. OldMilestoneID: oldMilestoneID,
  396. MilestoneID: milestoneID,
  397. })
  398. }
  399. func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldAssigneeID, assigneeID int64) (*Comment, error) {
  400. return createComment(e, &CreateCommentOptions{
  401. Type: CommentTypeAssignees,
  402. Doer: doer,
  403. Repo: repo,
  404. Issue: issue,
  405. OldAssigneeID: oldAssigneeID,
  406. AssigneeID: assigneeID,
  407. })
  408. }
  409. func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
  410. return createComment(e, &CreateCommentOptions{
  411. Type: CommentTypeChangeTitle,
  412. Doer: doer,
  413. Repo: repo,
  414. Issue: issue,
  415. OldTitle: oldTitle,
  416. NewTitle: newTitle,
  417. })
  418. }
  419. func createDeleteBranchComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, branchName string) (*Comment, error) {
  420. return createComment(e, &CreateCommentOptions{
  421. Type: CommentTypeDeleteBranch,
  422. Doer: doer,
  423. Repo: repo,
  424. Issue: issue,
  425. CommitSHA: branchName,
  426. })
  427. }
  428. // CreateCommentOptions defines options for creating comment
  429. type CreateCommentOptions struct {
  430. Type CommentType
  431. Doer *User
  432. Repo *Repository
  433. Issue *Issue
  434. Label *Label
  435. OldMilestoneID int64
  436. MilestoneID int64
  437. OldAssigneeID int64
  438. AssigneeID int64
  439. OldTitle string
  440. NewTitle string
  441. CommitID int64
  442. CommitSHA string
  443. LineNum int64
  444. Content string
  445. Attachments []string // UUIDs of attachments
  446. }
  447. // CreateComment creates comment of issue or commit.
  448. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  449. sess := x.NewSession()
  450. defer sessionRelease(sess)
  451. if err = sess.Begin(); err != nil {
  452. return nil, err
  453. }
  454. comment, err = createComment(sess, opts)
  455. if err != nil {
  456. return nil, err
  457. }
  458. return comment, sess.Commit()
  459. }
  460. // CreateIssueComment creates a plain issue comment.
  461. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  462. return CreateComment(&CreateCommentOptions{
  463. Type: CommentTypeComment,
  464. Doer: doer,
  465. Repo: repo,
  466. Issue: issue,
  467. Content: content,
  468. Attachments: attachments,
  469. })
  470. }
  471. // CreateRefComment creates a commit reference comment to issue.
  472. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  473. if len(commitSHA) == 0 {
  474. return fmt.Errorf("cannot create reference with empty commit SHA")
  475. }
  476. // Check if same reference from same commit has already existed.
  477. has, err := x.Get(&Comment{
  478. Type: CommentTypeCommitRef,
  479. IssueID: issue.ID,
  480. CommitSHA: commitSHA,
  481. })
  482. if err != nil {
  483. return fmt.Errorf("check reference comment: %v", err)
  484. } else if has {
  485. return nil
  486. }
  487. _, err = CreateComment(&CreateCommentOptions{
  488. Type: CommentTypeCommitRef,
  489. Doer: doer,
  490. Repo: repo,
  491. Issue: issue,
  492. CommitSHA: commitSHA,
  493. Content: content,
  494. })
  495. return err
  496. }
  497. // GetCommentByID returns the comment by given ID.
  498. func GetCommentByID(id int64) (*Comment, error) {
  499. c := new(Comment)
  500. has, err := x.Id(id).Get(c)
  501. if err != nil {
  502. return nil, err
  503. } else if !has {
  504. return nil, ErrCommentNotExist{id, 0}
  505. }
  506. return c, nil
  507. }
  508. func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
  509. comments := make([]*Comment, 0, 10)
  510. sess := e.
  511. Where("issue_id = ?", issueID).
  512. Asc("created_unix")
  513. if since > 0 {
  514. sess.And("updated_unix >= ?", since)
  515. }
  516. return comments, sess.Find(&comments)
  517. }
  518. func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
  519. comments := make([]*Comment, 0, 10)
  520. sess := e.Where("issue.repo_id = ?", repoID).
  521. Join("INNER", "issue", "issue.id = comment.issue_id").
  522. Asc("comment.created_unix")
  523. if since > 0 {
  524. sess.And("comment.updated_unix >= ?", since)
  525. }
  526. return comments, sess.Find(&comments)
  527. }
  528. func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
  529. return getCommentsByIssueIDSince(e, issueID, -1)
  530. }
  531. // GetCommentsByIssueID returns all comments of an issue.
  532. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  533. return getCommentsByIssueID(x, issueID)
  534. }
  535. // GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
  536. func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
  537. return getCommentsByIssueIDSince(x, issueID, since)
  538. }
  539. // GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
  540. func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
  541. return getCommentsByRepoIDSince(x, repoID, since)
  542. }
  543. // UpdateComment updates information of comment.
  544. func UpdateComment(c *Comment) error {
  545. _, err := x.Id(c.ID).AllCols().Update(c)
  546. return err
  547. }
  548. // DeleteComment deletes the comment
  549. func DeleteComment(comment *Comment) error {
  550. sess := x.NewSession()
  551. defer sessionRelease(sess)
  552. if err := sess.Begin(); err != nil {
  553. return err
  554. }
  555. if _, err := sess.Delete(&Comment{
  556. ID: comment.ID,
  557. }); err != nil {
  558. return err
  559. }
  560. if comment.Type == CommentTypeComment {
  561. if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
  562. return err
  563. }
  564. }
  565. return sess.Commit()
  566. }