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.

issue.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 project
  5. import (
  6. "context"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. )
  10. // ProjectIssue saves relation from issue to a project
  11. type ProjectIssue struct { //revive:disable-line:exported
  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. // the sorting order on the board
  18. Sorting int64 `xorm:"NOT NULL DEFAULT 0"`
  19. }
  20. func init() {
  21. db.RegisterModel(new(ProjectIssue))
  22. }
  23. func deleteProjectIssuesByProjectID(ctx context.Context, projectID int64) error {
  24. _, err := db.GetEngine(ctx).Where("project_id=?", projectID).Delete(&ProjectIssue{})
  25. return err
  26. }
  27. // NumIssues return counter of all issues assigned to a project
  28. func (p *Project) NumIssues() int {
  29. c, err := db.GetEngine(db.DefaultContext).Table("project_issue").
  30. Where("project_id=?", p.ID).
  31. GroupBy("issue_id").
  32. Cols("issue_id").
  33. Count()
  34. if err != nil {
  35. return 0
  36. }
  37. return int(c)
  38. }
  39. // NumClosedIssues return counter of closed issues assigned to a project
  40. func (p *Project) NumClosedIssues() int {
  41. c, err := db.GetEngine(db.DefaultContext).Table("project_issue").
  42. Join("INNER", "issue", "project_issue.issue_id=issue.id").
  43. Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, true).
  44. Cols("issue_id").
  45. Count()
  46. if err != nil {
  47. return 0
  48. }
  49. return int(c)
  50. }
  51. // NumOpenIssues return counter of open issues assigned to a project
  52. func (p *Project) NumOpenIssues() int {
  53. c, err := db.GetEngine(db.DefaultContext).Table("project_issue").
  54. Join("INNER", "issue", "project_issue.issue_id=issue.id").
  55. Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, false).Count("issue.id")
  56. if err != nil {
  57. return 0
  58. }
  59. return int(c)
  60. }
  61. // MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column
  62. func MoveIssuesOnProjectBoard(board *Board, sortedIssueIDs map[int64]int64) error {
  63. return db.WithTx(func(ctx context.Context) error {
  64. sess := db.GetEngine(ctx)
  65. issueIDs := make([]int64, 0, len(sortedIssueIDs))
  66. for _, issueID := range sortedIssueIDs {
  67. issueIDs = append(issueIDs, issueID)
  68. }
  69. count, err := sess.Table(new(ProjectIssue)).Where("project_id=?", board.ProjectID).In("issue_id", issueIDs).Count()
  70. if err != nil {
  71. return err
  72. }
  73. if int(count) != len(sortedIssueIDs) {
  74. return fmt.Errorf("all issues have to be added to a project first")
  75. }
  76. for sorting, issueID := range sortedIssueIDs {
  77. _, err = sess.Exec("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?", board.ID, sorting, issueID)
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. })
  84. }
  85. func (pb *Board) removeIssues(ctx context.Context) error {
  86. _, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id = 0 WHERE project_board_id = ? ", pb.ID)
  87. return err
  88. }