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.

v147.go 4.2KB

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