gitea源码

v147.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_13
  4. import (
  5. "code.gitea.io/gitea/modules/timeutil"
  6. "xorm.io/xorm"
  7. )
  8. func CreateReviewsForCodeComments(x *xorm.Engine) error {
  9. // Review
  10. type Review struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. Type int
  13. ReviewerID int64 `xorm:"index"`
  14. OriginalAuthor string
  15. OriginalAuthorID int64
  16. IssueID int64 `xorm:"index"`
  17. Content string `xorm:"TEXT"`
  18. // Official is a review made by an assigned approver (counts towards approval)
  19. Official bool `xorm:"NOT NULL DEFAULT false"`
  20. CommitID string `xorm:"VARCHAR(40)"`
  21. Stale bool `xorm:"NOT NULL DEFAULT false"`
  22. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  23. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  24. }
  25. const ReviewTypeComment = 2
  26. // Comment represents a comment in commit and issue page.
  27. type Comment struct {
  28. ID int64 `xorm:"pk autoincr"`
  29. Type int `xorm:"INDEX"`
  30. PosterID int64 `xorm:"INDEX"`
  31. OriginalAuthor string
  32. OriginalAuthorID int64
  33. IssueID int64 `xorm:"INDEX"`
  34. LabelID int64
  35. OldProjectID int64
  36. ProjectID int64
  37. OldMilestoneID int64
  38. MilestoneID int64
  39. AssigneeID int64
  40. RemovedAssignee bool
  41. ResolveDoerID int64
  42. OldTitle string
  43. NewTitle string
  44. OldRef string
  45. NewRef string
  46. DependentIssueID int64
  47. CommitID int64
  48. Line int64 // - previous line / + proposed line
  49. TreePath string
  50. Content string `xorm:"TEXT"`
  51. // Path represents the 4 lines of code cemented by this comment
  52. PatchQuoted string `xorm:"TEXT patch"`
  53. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  54. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  55. // Reference issue in commit message
  56. CommitSHA string `xorm:"VARCHAR(40)"`
  57. ReviewID int64 `xorm:"index"`
  58. Invalidated bool
  59. // Reference an issue or pull from another comment, issue or PR
  60. // All information is about the origin of the reference
  61. RefRepoID int64 `xorm:"index"` // Repo where the referencing
  62. RefIssueID int64 `xorm:"index"`
  63. RefCommentID int64 `xorm:"index"` // 0 if origin is Issue title or content (or PR's)
  64. RefAction int `xorm:"SMALLINT"` // What happens if RefIssueID resolves
  65. RefIsPull bool
  66. }
  67. if err := x.Sync(new(Review), new(Comment)); err != nil {
  68. return err
  69. }
  70. updateComment := func(comments []*Comment) error {
  71. sess := x.NewSession()
  72. defer sess.Close()
  73. if err := sess.Begin(); err != nil {
  74. return err
  75. }
  76. for _, comment := range comments {
  77. review := &Review{
  78. Type: ReviewTypeComment,
  79. ReviewerID: comment.PosterID,
  80. IssueID: comment.IssueID,
  81. Official: false,
  82. CommitID: comment.CommitSHA,
  83. Stale: comment.Invalidated,
  84. OriginalAuthor: comment.OriginalAuthor,
  85. OriginalAuthorID: comment.OriginalAuthorID,
  86. CreatedUnix: comment.CreatedUnix,
  87. UpdatedUnix: comment.CreatedUnix,
  88. }
  89. if _, err := sess.NoAutoTime().Insert(review); err != nil {
  90. return err
  91. }
  92. reviewComment := &Comment{
  93. Type: 22,
  94. PosterID: comment.PosterID,
  95. Content: "",
  96. IssueID: comment.IssueID,
  97. ReviewID: review.ID,
  98. OriginalAuthor: comment.OriginalAuthor,
  99. OriginalAuthorID: comment.OriginalAuthorID,
  100. CreatedUnix: comment.CreatedUnix,
  101. UpdatedUnix: comment.CreatedUnix,
  102. }
  103. if _, err := sess.NoAutoTime().Insert(reviewComment); err != nil {
  104. return err
  105. }
  106. comment.ReviewID = review.ID
  107. if _, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment); err != nil {
  108. return err
  109. }
  110. }
  111. return sess.Commit()
  112. }
  113. start := 0
  114. batchSize := 100
  115. for {
  116. comments := make([]*Comment, 0, batchSize)
  117. if err := x.Where("review_id = 0 and type = 21").Limit(batchSize, start).Find(&comments); err != nil {
  118. return err
  119. }
  120. if err := updateComment(comments); err != nil {
  121. return err
  122. }
  123. start += len(comments)
  124. if len(comments) < batchSize {
  125. break
  126. }
  127. }
  128. return nil
  129. }