本站源代码
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.

187 lines
4.3KB

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