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.

project_issue.go 5.5KB

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