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.

220 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. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "xorm.io/xorm"
  9. )
  10. type (
  11. // ProjectBoardType is used to represent a project board type
  12. ProjectBoardType uint8
  13. // ProjectBoardList is a list of all project boards in a repository
  14. ProjectBoardList []*ProjectBoard
  15. )
  16. const (
  17. // ProjectBoardTypeNone is a project board type that has no predefined columns
  18. ProjectBoardTypeNone ProjectBoardType = iota
  19. // ProjectBoardTypeBasicKanban is a project board type that has basic predefined columns
  20. ProjectBoardTypeBasicKanban
  21. // ProjectBoardTypeBugTriage is a project board type that has predefined columns suited to hunting down bugs
  22. ProjectBoardTypeBugTriage
  23. )
  24. // ProjectBoard is used to represent boards on a project
  25. type ProjectBoard struct {
  26. ID int64 `xorm:"pk autoincr"`
  27. Title string
  28. Default bool `xorm:"NOT NULL DEFAULT false"` // issues not assigned to a specific board will be assigned to this board
  29. ProjectID int64 `xorm:"INDEX NOT NULL"`
  30. CreatorID int64 `xorm:"NOT NULL"`
  31. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. Issues []*Issue `xorm:"-"`
  34. }
  35. // IsProjectBoardTypeValid checks if the project board type is valid
  36. func IsProjectBoardTypeValid(p ProjectBoardType) bool {
  37. switch p {
  38. case ProjectBoardTypeNone, ProjectBoardTypeBasicKanban, ProjectBoardTypeBugTriage:
  39. return true
  40. default:
  41. return false
  42. }
  43. }
  44. func createBoardsForProjectsType(sess *xorm.Session, project *Project) error {
  45. var items []string
  46. switch project.BoardType {
  47. case ProjectBoardTypeBugTriage:
  48. items = setting.Project.ProjectBoardBugTriageType
  49. case ProjectBoardTypeBasicKanban:
  50. items = setting.Project.ProjectBoardBasicKanbanType
  51. case ProjectBoardTypeNone:
  52. fallthrough
  53. default:
  54. return nil
  55. }
  56. if len(items) == 0 {
  57. return nil
  58. }
  59. var boards = make([]ProjectBoard, 0, len(items))
  60. for _, v := range items {
  61. boards = append(boards, ProjectBoard{
  62. CreatedUnix: timeutil.TimeStampNow(),
  63. CreatorID: project.CreatorID,
  64. Title: v,
  65. ProjectID: project.ID,
  66. })
  67. }
  68. _, err := sess.Insert(boards)
  69. return err
  70. }
  71. // NewProjectBoard adds a new project board to a given project
  72. func NewProjectBoard(board *ProjectBoard) error {
  73. _, err := x.Insert(board)
  74. return err
  75. }
  76. // DeleteProjectBoardByID removes all issues references to the project board.
  77. func DeleteProjectBoardByID(boardID int64) error {
  78. sess := x.NewSession()
  79. defer sess.Close()
  80. if err := sess.Begin(); err != nil {
  81. return err
  82. }
  83. if err := deleteProjectBoardByID(sess, boardID); err != nil {
  84. return err
  85. }
  86. return sess.Commit()
  87. }
  88. func deleteProjectBoardByID(e Engine, boardID int64) error {
  89. board, err := getProjectBoard(e, boardID)
  90. if err != nil {
  91. if IsErrProjectBoardNotExist(err) {
  92. return nil
  93. }
  94. return err
  95. }
  96. if err = board.removeIssues(e); err != nil {
  97. return err
  98. }
  99. if _, err := e.ID(board.ID).Delete(board); err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func deleteProjectBoardByProjectID(e Engine, projectID int64) error {
  105. _, err := e.Where("project_id=?", projectID).Delete(&ProjectBoard{})
  106. return err
  107. }
  108. // GetProjectBoard fetches the current board of a project
  109. func GetProjectBoard(boardID int64) (*ProjectBoard, error) {
  110. return getProjectBoard(x, boardID)
  111. }
  112. func getProjectBoard(e Engine, boardID int64) (*ProjectBoard, error) {
  113. board := new(ProjectBoard)
  114. has, err := e.ID(boardID).Get(board)
  115. if err != nil {
  116. return nil, err
  117. } else if !has {
  118. return nil, ErrProjectBoardNotExist{BoardID: boardID}
  119. }
  120. return board, nil
  121. }
  122. // UpdateProjectBoard updates the title of a project board
  123. func UpdateProjectBoard(board *ProjectBoard) error {
  124. return updateProjectBoard(x, board)
  125. }
  126. func updateProjectBoard(e Engine, board *ProjectBoard) error {
  127. _, err := e.ID(board.ID).Cols(
  128. "title",
  129. "default",
  130. ).Update(board)
  131. return err
  132. }
  133. // GetProjectBoards fetches all boards related to a project
  134. func GetProjectBoards(projectID int64) ([]*ProjectBoard, error) {
  135. var boards = make([]*ProjectBoard, 0, 5)
  136. sess := x.Where("project_id=?", projectID)
  137. return boards, sess.Find(&boards)
  138. }
  139. // GetUncategorizedBoard represents a board for issues not assigned to one
  140. func GetUncategorizedBoard(projectID int64) (*ProjectBoard, error) {
  141. return &ProjectBoard{
  142. ProjectID: projectID,
  143. Title: "Uncategorized",
  144. Default: true,
  145. }, nil
  146. }
  147. // LoadIssues load issues assigned to this board
  148. func (b *ProjectBoard) LoadIssues() (IssueList, error) {
  149. var boardID int64
  150. if !b.Default {
  151. boardID = b.ID
  152. } else {
  153. // Issues without ProjectBoardID
  154. boardID = -1
  155. }
  156. issues, err := Issues(&IssuesOptions{
  157. ProjectBoardID: boardID,
  158. ProjectID: b.ProjectID,
  159. })
  160. b.Issues = issues
  161. return issues, err
  162. }
  163. // LoadIssues load issues assigned to the boards
  164. func (bs ProjectBoardList) LoadIssues() (IssueList, error) {
  165. issues := make(IssueList, 0, len(bs)*10)
  166. for i := range bs {
  167. il, err := bs[i].LoadIssues()
  168. if err != nil {
  169. return nil, err
  170. }
  171. bs[i].Issues = il
  172. issues = append(issues, il...)
  173. }
  174. return issues, nil
  175. }