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.

207 lines
5.1 KiB

  1. // Copyright 2020 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. "fmt"
  7. "xorm.io/xorm"
  8. )
  9. // ProjectIssue saves relation from issue to a project
  10. type ProjectIssue struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. IssueID int64 `xorm:"INDEX"`
  13. ProjectID int64 `xorm:"INDEX"`
  14. // If 0, then it has not been added to a specific board in the project
  15. ProjectBoardID int64 `xorm:"INDEX"`
  16. }
  17. func deleteProjectIssuesByProjectID(e Engine, projectID int64) error {
  18. _, err := e.Where("project_id=?", projectID).Delete(&ProjectIssue{})
  19. return err
  20. }
  21. // ___
  22. // |_ _|___ ___ _ _ ___
  23. // | |/ __/ __| | | |/ _ \
  24. // | |\__ \__ \ |_| | __/
  25. // |___|___/___/\__,_|\___|
  26. // LoadProject load the project the issue was assigned to
  27. func (i *Issue) LoadProject() (err error) {
  28. return i.loadProject(x)
  29. }
  30. func (i *Issue) loadProject(e Engine) (err error) {
  31. if i.Project == nil {
  32. var p Project
  33. if _, err = e.Table("project").
  34. Join("INNER", "project_issue", "project.id=project_issue.project_id").
  35. Where("project_issue.issue_id = ?", i.ID).
  36. Get(&p); err != nil {
  37. return err
  38. }
  39. i.Project = &p
  40. }
  41. return
  42. }
  43. // ProjectID return project id if issue was assigned to one
  44. func (i *Issue) ProjectID() int64 {
  45. return i.projectID(x)
  46. }
  47. func (i *Issue) projectID(e Engine) int64 {
  48. var ip ProjectIssue
  49. has, err := e.Where("issue_id=?", i.ID).Get(&ip)
  50. if err != nil || !has {
  51. return 0
  52. }
  53. return ip.ProjectID
  54. }
  55. // ProjectBoardID return project board id if issue was assigned to one
  56. func (i *Issue) ProjectBoardID() int64 {
  57. return i.projectBoardID(x)
  58. }
  59. func (i *Issue) projectBoardID(e Engine) int64 {
  60. var ip ProjectIssue
  61. has, err := e.Where("issue_id=?", i.ID).Get(&ip)
  62. if err != nil || !has {
  63. return 0
  64. }
  65. return ip.ProjectBoardID
  66. }
  67. // ____ _ _
  68. // | _ \ _ __ ___ (_) ___ ___| |_
  69. // | |_) | '__/ _ \| |/ _ \/ __| __|
  70. // | __/| | | (_) | | __/ (__| |_
  71. // |_| |_| \___// |\___|\___|\__|
  72. // |__/
  73. // NumIssues return counter of all issues assigned to a project
  74. func (p *Project) NumIssues() int {
  75. c, err := x.Table("project_issue").
  76. Where("project_id=?", p.ID).
  77. GroupBy("issue_id").
  78. Cols("issue_id").
  79. Count()
  80. if err != nil {
  81. return 0
  82. }
  83. return int(c)
  84. }
  85. // NumClosedIssues return counter of closed issues assigned to a project
  86. func (p *Project) NumClosedIssues() int {
  87. c, err := x.Table("project_issue").
  88. Join("INNER", "issue", "project_issue.issue_id=issue.id").
  89. Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, true).
  90. Cols("issue_id").
  91. Count()
  92. if err != nil {
  93. return 0
  94. }
  95. return int(c)
  96. }
  97. // NumOpenIssues return counter of open issues assigned to a project
  98. func (p *Project) NumOpenIssues() int {
  99. c, err := x.Table("project_issue").
  100. Join("INNER", "issue", "project_issue.issue_id=issue.id").
  101. Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, false).Count("issue.id")
  102. if err != nil {
  103. return 0
  104. }
  105. return int(c)
  106. }
  107. // ChangeProjectAssign changes the project associated with an issue
  108. func ChangeProjectAssign(issue *Issue, doer *User, newProjectID int64) error {
  109. sess := x.NewSession()
  110. defer sess.Close()
  111. if err := sess.Begin(); err != nil {
  112. return err
  113. }
  114. if err := addUpdateIssueProject(sess, issue, doer, newProjectID); err != nil {
  115. return err
  116. }
  117. return sess.Commit()
  118. }
  119. func addUpdateIssueProject(e *xorm.Session, issue *Issue, doer *User, newProjectID int64) error {
  120. oldProjectID := issue.projectID(e)
  121. if _, err := e.Where("project_issue.issue_id=?", issue.ID).Delete(&ProjectIssue{}); err != nil {
  122. return err
  123. }
  124. if err := issue.loadRepo(e); err != nil {
  125. return err
  126. }
  127. if oldProjectID > 0 || newProjectID > 0 {
  128. if _, err := createComment(e, &CreateCommentOptions{
  129. Type: CommentTypeProject,
  130. Doer: doer,
  131. Repo: issue.Repo,
  132. Issue: issue,
  133. OldProjectID: oldProjectID,
  134. ProjectID: newProjectID,
  135. }); err != nil {
  136. return err
  137. }
  138. }
  139. _, err := e.Insert(&ProjectIssue{
  140. IssueID: issue.ID,
  141. ProjectID: newProjectID,
  142. })
  143. return err
  144. }
  145. // ____ _ _ ____ _
  146. // | _ \ _ __ ___ (_) ___ ___| |_| __ ) ___ __ _ _ __ __| |
  147. // | |_) | '__/ _ \| |/ _ \/ __| __| _ \ / _ \ / _` | '__/ _` |
  148. // | __/| | | (_) | | __/ (__| |_| |_) | (_) | (_| | | | (_| |
  149. // |_| |_| \___// |\___|\___|\__|____/ \___/ \__,_|_| \__,_|
  150. // |__/
  151. // MoveIssueAcrossProjectBoards move a card from one board to another
  152. func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error {
  153. sess := x.NewSession()
  154. defer sess.Close()
  155. if err := sess.Begin(); err != nil {
  156. return err
  157. }
  158. var pis ProjectIssue
  159. has, err := sess.Where("issue_id=?", issue.ID).Get(&pis)
  160. if err != nil {
  161. return err
  162. }
  163. if !has {
  164. return fmt.Errorf("issue has to be added to a project first")
  165. }
  166. pis.ProjectBoardID = board.ID
  167. if _, err := sess.ID(pis.ID).Cols("project_board_id").Update(&pis); err != nil {
  168. return err
  169. }
  170. return sess.Commit()
  171. }
  172. func (pb *ProjectBoard) removeIssues(e Engine) error {
  173. _, err := e.Exec("UPDATE `project_issue` SET project_board_id = 0 WHERE project_board_id = ? ", pb.ID)
  174. return err
  175. }