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_board.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/builder"
  9. "xorm.io/xorm"
  10. )
  11. type (
  12. // ProjectBoardType is used to represent a project board type
  13. ProjectBoardType uint8
  14. // ProjectBoardList is a list of all project boards in a repository
  15. ProjectBoardList []*ProjectBoard
  16. )
  17. const (
  18. // ProjectBoardTypeNone is a project board type that has no predefined columns
  19. ProjectBoardTypeNone ProjectBoardType = iota
  20. // ProjectBoardTypeBasicKanban is a project board type that has basic predefined columns
  21. ProjectBoardTypeBasicKanban
  22. // ProjectBoardTypeBugTriage is a project board type that has predefined columns suited to hunting down bugs
  23. ProjectBoardTypeBugTriage
  24. )
  25. // ProjectBoard is used to represent boards on a project
  26. type ProjectBoard struct {
  27. ID int64 `xorm:"pk autoincr"`
  28. Title string
  29. Default bool `xorm:"NOT NULL DEFAULT false"` // issues not assigned to a specific board will be assigned to this board
  30. Sorting int8 `xorm:"NOT NULL DEFAULT 0"`
  31. ProjectID int64 `xorm:"INDEX NOT NULL"`
  32. CreatorID int64 `xorm:"NOT NULL"`
  33. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  34. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  35. Issues []*Issue `xorm:"-"`
  36. }
  37. // IsProjectBoardTypeValid checks if the project board type is valid
  38. func IsProjectBoardTypeValid(p ProjectBoardType) bool {
  39. switch p {
  40. case ProjectBoardTypeNone, ProjectBoardTypeBasicKanban, ProjectBoardTypeBugTriage:
  41. return true
  42. default:
  43. return false
  44. }
  45. }
  46. func createBoardsForProjectsType(sess *xorm.Session, project *Project) error {
  47. var items []string
  48. switch project.BoardType {
  49. case ProjectBoardTypeBugTriage:
  50. items = setting.Project.ProjectBoardBugTriageType
  51. case ProjectBoardTypeBasicKanban:
  52. items = setting.Project.ProjectBoardBasicKanbanType
  53. case ProjectBoardTypeNone:
  54. fallthrough
  55. default:
  56. return nil
  57. }
  58. if len(items) == 0 {
  59. return nil
  60. }
  61. boards := make([]ProjectBoard, 0, len(items))
  62. for _, v := range items {
  63. boards = append(boards, ProjectBoard{
  64. CreatedUnix: timeutil.TimeStampNow(),
  65. CreatorID: project.CreatorID,
  66. Title: v,
  67. ProjectID: project.ID,
  68. })
  69. }
  70. _, err := sess.Insert(boards)
  71. return err
  72. }
  73. // NewProjectBoard adds a new project board to a given project
  74. func NewProjectBoard(board *ProjectBoard) error {
  75. _, err := x.Insert(board)
  76. return err
  77. }
  78. // DeleteProjectBoardByID removes all issues references to the project board.
  79. func DeleteProjectBoardByID(boardID int64) error {
  80. sess := x.NewSession()
  81. defer sess.Close()
  82. if err := sess.Begin(); err != nil {
  83. return err
  84. }
  85. if err := deleteProjectBoardByID(sess, boardID); err != nil {
  86. return err
  87. }
  88. return sess.Commit()
  89. }
  90. func deleteProjectBoardByID(e Engine, boardID int64) error {
  91. board, err := getProjectBoard(e, boardID)
  92. if err != nil {
  93. if IsErrProjectBoardNotExist(err) {
  94. return nil
  95. }
  96. return err
  97. }
  98. if err = board.removeIssues(e); err != nil {
  99. return err
  100. }
  101. if _, err := e.ID(board.ID).Delete(board); err != nil {
  102. return err
  103. }
  104. return nil
  105. }
  106. func deleteProjectBoardByProjectID(e Engine, projectID int64) error {
  107. _, err := e.Where("project_id=?", projectID).Delete(&ProjectBoard{})
  108. return err
  109. }
  110. // GetProjectBoard fetches the current board of a project
  111. func GetProjectBoard(boardID int64) (*ProjectBoard, error) {
  112. return getProjectBoard(x, boardID)
  113. }
  114. func getProjectBoard(e Engine, boardID int64) (*ProjectBoard, error) {
  115. board := new(ProjectBoard)
  116. has, err := e.ID(boardID).Get(board)
  117. if err != nil {
  118. return nil, err
  119. } else if !has {
  120. return nil, ErrProjectBoardNotExist{BoardID: boardID}
  121. }
  122. return board, nil
  123. }
  124. // UpdateProjectBoard updates a project board
  125. func UpdateProjectBoard(board *ProjectBoard) error {
  126. return updateProjectBoard(x, board)
  127. }
  128. func updateProjectBoard(e Engine, board *ProjectBoard) error {
  129. var fieldToUpdate []string
  130. if board.Sorting != 0 {
  131. fieldToUpdate = append(fieldToUpdate, "sorting")
  132. }
  133. if board.Title != "" {
  134. fieldToUpdate = append(fieldToUpdate, "title")
  135. }
  136. _, err := e.ID(board.ID).Cols(fieldToUpdate...).Update(board)
  137. return err
  138. }
  139. // GetProjectBoards fetches all boards related to a project
  140. // if no default board set, first board is a temporary "Uncategorized" board
  141. func GetProjectBoards(projectID int64) (ProjectBoardList, error) {
  142. return getProjectBoards(x, projectID)
  143. }
  144. func getProjectBoards(e Engine, projectID int64) ([]*ProjectBoard, error) {
  145. boards := make([]*ProjectBoard, 0, 5)
  146. if err := e.Where("project_id=? AND `default`=?", projectID, false).OrderBy("Sorting").Find(&boards); err != nil {
  147. return nil, err
  148. }
  149. defaultB, err := getDefaultBoard(e, projectID)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return append([]*ProjectBoard{defaultB}, boards...), nil
  154. }
  155. // getDefaultBoard return default board and create a dummy if none exist
  156. func getDefaultBoard(e Engine, projectID int64) (*ProjectBoard, error) {
  157. var board ProjectBoard
  158. exist, err := e.Where("project_id=? AND `default`=?", projectID, true).Get(&board)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if exist {
  163. return &board, nil
  164. }
  165. // represents a board for issues not assigned to one
  166. return &ProjectBoard{
  167. ProjectID: projectID,
  168. Title: "Uncategorized",
  169. Default: true,
  170. }, nil
  171. }
  172. // SetDefaultBoard represents a board for issues not assigned to one
  173. // if boardID is 0 unset default
  174. func SetDefaultBoard(projectID, boardID int64) error {
  175. sess := x
  176. _, err := sess.Where(builder.Eq{
  177. "project_id": projectID,
  178. "`default`": true,
  179. }).Cols("`default`").Update(&ProjectBoard{Default: false})
  180. if err != nil {
  181. return err
  182. }
  183. if boardID > 0 {
  184. _, err = sess.ID(boardID).Where(builder.Eq{"project_id": projectID}).
  185. Cols("`default`").Update(&ProjectBoard{Default: true})
  186. }
  187. return err
  188. }
  189. // LoadIssues load issues assigned to this board
  190. func (b *ProjectBoard) LoadIssues() (IssueList, error) {
  191. issueList := make([]*Issue, 0, 10)
  192. if b.ID != 0 {
  193. issues, err := Issues(&IssuesOptions{
  194. ProjectBoardID: b.ID,
  195. ProjectID: b.ProjectID,
  196. })
  197. if err != nil {
  198. return nil, err
  199. }
  200. issueList = issues
  201. }
  202. if b.Default {
  203. issues, err := Issues(&IssuesOptions{
  204. ProjectBoardID: -1, // Issues without ProjectBoardID
  205. ProjectID: b.ProjectID,
  206. })
  207. if err != nil {
  208. return nil, err
  209. }
  210. issueList = append(issueList, issues...)
  211. }
  212. if err := IssueList(issueList).LoadComments(); err != nil {
  213. return nil, err
  214. }
  215. b.Issues = issueList
  216. return issueList, nil
  217. }
  218. // LoadIssues load issues assigned to the boards
  219. func (bs ProjectBoardList) LoadIssues() (IssueList, error) {
  220. issues := make(IssueList, 0, len(bs)*10)
  221. for i := range bs {
  222. il, err := bs[i].LoadIssues()
  223. if err != nil {
  224. return nil, err
  225. }
  226. bs[i].Issues = il
  227. issues = append(issues, il...)
  228. }
  229. return issues, nil
  230. }
  231. // UpdateProjectBoardSorting update project board sorting
  232. func UpdateProjectBoardSorting(bs ProjectBoardList) error {
  233. for i := range bs {
  234. _, err := x.ID(bs[i].ID).Cols(
  235. "sorting",
  236. ).Update(bs[i])
  237. if err != nil {
  238. return err
  239. }
  240. }
  241. return nil
  242. }