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.

174 lines
3.9 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 "github.com/go-xorm/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. if !issue.IsPull {
  57. sess.ID(issue.RepoID).Incr("num_issues")
  58. if issue.IsClosed {
  59. sess.Incr("num_closed_issues")
  60. }
  61. } else {
  62. sess.ID(issue.RepoID).Incr("num_pulls")
  63. if issue.IsClosed {
  64. sess.Incr("num_closed_pulls")
  65. }
  66. }
  67. if _, err := sess.NoAutoTime().Update(issue.Repo); err != nil {
  68. return err
  69. }
  70. sess.Incr("num_issues")
  71. if issue.IsClosed {
  72. sess.Incr("num_closed_issues")
  73. }
  74. if _, err := sess.In("id", labelIDs).NoAutoTime().Update(new(Label)); err != nil {
  75. return err
  76. }
  77. if issue.MilestoneID > 0 {
  78. sess.Incr("num_issues")
  79. if issue.IsClosed {
  80. sess.Incr("num_closed_issues")
  81. }
  82. if _, err := sess.ID(issue.MilestoneID).
  83. SetExpr("completeness", "num_closed_issues * 100 / num_issues").
  84. NoAutoTime().
  85. Update(new(Milestone)); err != nil {
  86. return err
  87. }
  88. }
  89. return nil
  90. }
  91. // InsertIssueComments inserts many comments of issues.
  92. func InsertIssueComments(comments []*Comment) error {
  93. if len(comments) == 0 {
  94. return nil
  95. }
  96. var issueIDs = make(map[int64]bool)
  97. for _, comment := range comments {
  98. issueIDs[comment.IssueID] = true
  99. }
  100. sess := x.NewSession()
  101. defer sess.Close()
  102. if err := sess.Begin(); err != nil {
  103. return err
  104. }
  105. if _, err := sess.NoAutoTime().Insert(comments); err != nil {
  106. return err
  107. }
  108. for issueID := range issueIDs {
  109. if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
  110. return err
  111. }
  112. }
  113. return sess.Commit()
  114. }
  115. // InsertPullRequests inserted pull requests
  116. func InsertPullRequests(prs ...*PullRequest) error {
  117. sess := x.NewSession()
  118. defer sess.Close()
  119. if err := sess.Begin(); err != nil {
  120. return err
  121. }
  122. for _, pr := range prs {
  123. if err := insertIssue(sess, pr.Issue); err != nil {
  124. return err
  125. }
  126. pr.IssueID = pr.Issue.ID
  127. if _, err := sess.NoAutoTime().Insert(pr); err != nil {
  128. return err
  129. }
  130. }
  131. return sess.Commit()
  132. }
  133. // InsertReleases migrates release
  134. func InsertReleases(rels ...*Release) error {
  135. sess := x.NewSession()
  136. if err := sess.Begin(); err != nil {
  137. return err
  138. }
  139. for _, rel := range rels {
  140. if _, err := sess.NoAutoTime().Insert(rel); err != nil {
  141. return err
  142. }
  143. for i := 0; i < len(rel.Attachments); i++ {
  144. rel.Attachments[i].ReleaseID = rel.ID
  145. }
  146. if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
  147. return err
  148. }
  149. }
  150. return sess.Commit()
  151. }