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.

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