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.

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