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.

145 lines
3.3 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. // InsertIssue insert one issue to database
  7. func InsertIssue(issue *Issue, labelIDs []int64) error {
  8. sess := x.NewSession()
  9. if err := sess.Begin(); err != nil {
  10. return err
  11. }
  12. if err := insertIssue(sess, issue, labelIDs); err != nil {
  13. return err
  14. }
  15. return sess.Commit()
  16. }
  17. func insertIssue(sess *xorm.Session, issue *Issue, labelIDs []int64) error {
  18. if issue.MilestoneID > 0 {
  19. sess.Incr("num_issues")
  20. if issue.IsClosed {
  21. sess.Incr("num_closed_issues")
  22. }
  23. if _, err := sess.ID(issue.MilestoneID).NoAutoTime().Update(new(Milestone)); err != nil {
  24. return err
  25. }
  26. }
  27. if _, err := sess.NoAutoTime().Insert(issue); err != nil {
  28. return err
  29. }
  30. var issueLabels = make([]IssueLabel, 0, len(labelIDs))
  31. for _, labelID := range labelIDs {
  32. issueLabels = append(issueLabels, IssueLabel{
  33. IssueID: issue.ID,
  34. LabelID: labelID,
  35. })
  36. }
  37. if _, err := sess.Insert(issueLabels); err != nil {
  38. return err
  39. }
  40. if !issue.IsPull {
  41. sess.ID(issue.RepoID).Incr("num_issues")
  42. if issue.IsClosed {
  43. sess.Incr("num_closed_issues")
  44. }
  45. } else {
  46. sess.ID(issue.RepoID).Incr("num_pulls")
  47. if issue.IsClosed {
  48. sess.Incr("num_closed_pulls")
  49. }
  50. }
  51. if _, err := sess.NoAutoTime().Update(issue.Repo); err != nil {
  52. return err
  53. }
  54. sess.Incr("num_issues")
  55. if issue.IsClosed {
  56. sess.Incr("num_closed_issues")
  57. }
  58. if _, err := sess.In("id", labelIDs).Update(new(Label)); err != nil {
  59. return err
  60. }
  61. if issue.MilestoneID > 0 {
  62. if _, err := sess.ID(issue.MilestoneID).SetExpr("completeness", "num_closed_issues * 100 / num_issues").Update(new(Milestone)); err != nil {
  63. return err
  64. }
  65. }
  66. return nil
  67. }
  68. // InsertComment inserted a comment
  69. func InsertComment(comment *Comment) error {
  70. sess := x.NewSession()
  71. defer sess.Close()
  72. if err := sess.Begin(); err != nil {
  73. return err
  74. }
  75. if _, err := sess.NoAutoTime().Insert(comment); err != nil {
  76. return err
  77. }
  78. if _, err := sess.ID(comment.IssueID).Incr("num_comments").Update(new(Issue)); err != nil {
  79. return err
  80. }
  81. return sess.Commit()
  82. }
  83. // InsertPullRequest inserted a pull request
  84. func InsertPullRequest(pr *PullRequest, labelIDs []int64) error {
  85. sess := x.NewSession()
  86. defer sess.Close()
  87. if err := sess.Begin(); err != nil {
  88. return err
  89. }
  90. if err := insertIssue(sess, pr.Issue, labelIDs); err != nil {
  91. return err
  92. }
  93. pr.IssueID = pr.Issue.ID
  94. if _, err := sess.NoAutoTime().Insert(pr); err != nil {
  95. return err
  96. }
  97. return sess.Commit()
  98. }
  99. // MigrateRelease migrates release
  100. func MigrateRelease(rel *Release) error {
  101. sess := x.NewSession()
  102. if err := sess.Begin(); err != nil {
  103. return err
  104. }
  105. var oriRel = Release{
  106. RepoID: rel.RepoID,
  107. TagName: rel.TagName,
  108. }
  109. exist, err := sess.Get(&oriRel)
  110. if err != nil {
  111. return err
  112. }
  113. if !exist {
  114. if _, err := sess.NoAutoTime().Insert(rel); err != nil {
  115. return err
  116. }
  117. } else {
  118. rel.ID = oriRel.ID
  119. if _, err := sess.ID(rel.ID).Cols("target, title, note, is_tag, num_commits").Update(rel); err != nil {
  120. return err
  121. }
  122. }
  123. for i := 0; i < len(rel.Attachments); i++ {
  124. rel.Attachments[i].ReleaseID = rel.ID
  125. }
  126. if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
  127. return err
  128. }
  129. return sess.Commit()
  130. }