A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

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