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.

242 lines
5.6 KiB

  1. // Copyright 2019 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 models
  5. import (
  6. "code.gitea.io/gitea/modules/structs"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. // InsertMilestones creates milestones of repository.
  11. func InsertMilestones(ms ...*Milestone) (err error) {
  12. if len(ms) == 0 {
  13. return nil
  14. }
  15. sess := x.NewSession()
  16. defer sess.Close()
  17. if err = sess.Begin(); err != nil {
  18. return err
  19. }
  20. // to return the id, so we should not use batch insert
  21. for _, m := range ms {
  22. if _, err = sess.NoAutoTime().Insert(m); err != nil {
  23. return err
  24. }
  25. }
  26. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
  27. return err
  28. }
  29. return sess.Commit()
  30. }
  31. // InsertIssues insert issues to database
  32. func InsertIssues(issues ...*Issue) error {
  33. sess := x.NewSession()
  34. if err := sess.Begin(); err != nil {
  35. return err
  36. }
  37. for _, issue := range issues {
  38. if err := insertIssue(sess, issue); err != nil {
  39. return err
  40. }
  41. }
  42. return sess.Commit()
  43. }
  44. func insertIssue(sess *xorm.Session, issue *Issue) error {
  45. if _, err := sess.NoAutoTime().Insert(issue); err != nil {
  46. return err
  47. }
  48. var issueLabels = make([]IssueLabel, 0, len(issue.Labels))
  49. var labelIDs = make([]int64, 0, len(issue.Labels))
  50. for _, label := range issue.Labels {
  51. issueLabels = append(issueLabels, IssueLabel{
  52. IssueID: issue.ID,
  53. LabelID: label.ID,
  54. })
  55. labelIDs = append(labelIDs, label.ID)
  56. }
  57. if len(issueLabels) > 0 {
  58. if _, err := sess.Insert(issueLabels); err != nil {
  59. return err
  60. }
  61. }
  62. for _, reaction := range issue.Reactions {
  63. reaction.IssueID = issue.ID
  64. }
  65. if len(issue.Reactions) > 0 {
  66. if _, err := sess.Insert(issue.Reactions); err != nil {
  67. return err
  68. }
  69. }
  70. cols := make([]string, 0)
  71. if !issue.IsPull {
  72. sess.ID(issue.RepoID).Incr("num_issues")
  73. cols = append(cols, "num_issues")
  74. if issue.IsClosed {
  75. sess.Incr("num_closed_issues")
  76. cols = append(cols, "num_closed_issues")
  77. }
  78. } else {
  79. sess.ID(issue.RepoID).Incr("num_pulls")
  80. cols = append(cols, "num_pulls")
  81. if issue.IsClosed {
  82. sess.Incr("num_closed_pulls")
  83. cols = append(cols, "num_closed_pulls")
  84. }
  85. }
  86. if _, err := sess.NoAutoTime().Cols(cols...).Update(issue.Repo); err != nil {
  87. return err
  88. }
  89. cols = []string{"num_issues"}
  90. sess.Incr("num_issues")
  91. if issue.IsClosed {
  92. sess.Incr("num_closed_issues")
  93. cols = append(cols, "num_closed_issues")
  94. }
  95. if _, err := sess.In("id", labelIDs).NoAutoTime().Cols(cols...).Update(new(Label)); err != nil {
  96. return err
  97. }
  98. if issue.MilestoneID > 0 {
  99. cols = []string{"num_issues"}
  100. sess.Incr("num_issues")
  101. cl := "num_closed_issues"
  102. if issue.IsClosed {
  103. sess.Incr("num_closed_issues")
  104. cols = append(cols, "num_closed_issues")
  105. cl = "(num_closed_issues + 1)"
  106. }
  107. if _, err := sess.ID(issue.MilestoneID).
  108. SetExpr("completeness", cl+" * 100 / (num_issues + 1)").
  109. NoAutoTime().Cols(cols...).
  110. Update(new(Milestone)); err != nil {
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. // InsertIssueComments inserts many comments of issues.
  117. func InsertIssueComments(comments []*Comment) error {
  118. if len(comments) == 0 {
  119. return nil
  120. }
  121. var issueIDs = make(map[int64]bool)
  122. for _, comment := range comments {
  123. issueIDs[comment.IssueID] = true
  124. }
  125. sess := x.NewSession()
  126. defer sess.Close()
  127. if err := sess.Begin(); err != nil {
  128. return err
  129. }
  130. for _, comment := range comments {
  131. if _, err := sess.NoAutoTime().Insert(comment); err != nil {
  132. return err
  133. }
  134. for _, reaction := range comment.Reactions {
  135. reaction.IssueID = comment.IssueID
  136. reaction.CommentID = comment.ID
  137. }
  138. if len(comment.Reactions) > 0 {
  139. if _, err := sess.Insert(comment.Reactions); err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. for issueID := range issueIDs {
  145. if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
  146. return err
  147. }
  148. }
  149. return sess.Commit()
  150. }
  151. // InsertPullRequests inserted pull requests
  152. func InsertPullRequests(prs ...*PullRequest) error {
  153. sess := x.NewSession()
  154. defer sess.Close()
  155. if err := sess.Begin(); err != nil {
  156. return err
  157. }
  158. for _, pr := range prs {
  159. if err := insertIssue(sess, pr.Issue); err != nil {
  160. return err
  161. }
  162. pr.IssueID = pr.Issue.ID
  163. if _, err := sess.NoAutoTime().Insert(pr); err != nil {
  164. return err
  165. }
  166. }
  167. return sess.Commit()
  168. }
  169. // InsertReleases migrates release
  170. func InsertReleases(rels ...*Release) error {
  171. sess := x.NewSession()
  172. if err := sess.Begin(); err != nil {
  173. return err
  174. }
  175. for _, rel := range rels {
  176. if _, err := sess.NoAutoTime().Insert(rel); err != nil {
  177. return err
  178. }
  179. if len(rel.Attachments) > 0 {
  180. for i := range rel.Attachments {
  181. rel.Attachments[i].ReleaseID = rel.ID
  182. }
  183. if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
  184. return err
  185. }
  186. }
  187. }
  188. return sess.Commit()
  189. }
  190. func migratedIssueCond(tp structs.GitServiceType) builder.Cond {
  191. return builder.In("issue_id",
  192. builder.Select("issue.id").
  193. From("issue").
  194. InnerJoin("repository", "issue.repo_id = repository.id").
  195. Where(builder.Eq{
  196. "repository.original_service_type": tp,
  197. }),
  198. )
  199. }
  200. // UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
  201. func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
  202. _, err := x.Table("review").
  203. Where("original_author_id = ?", originalAuthorID).
  204. And(migratedIssueCond(tp)).
  205. Update(map[string]interface{}{
  206. "reviewer_id": posterID,
  207. "original_author": "",
  208. "original_author_id": 0,
  209. })
  210. return err
  211. }