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.

board.go 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package project
  4. import (
  5. "context"
  6. "fmt"
  7. "regexp"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "xorm.io/builder"
  12. )
  13. type (
  14. // BoardType is used to represent a project board type
  15. BoardType uint8
  16. // CardType is used to represent a project board card type
  17. CardType uint8
  18. // BoardList is a list of all project boards in a repository
  19. BoardList []*Board
  20. )
  21. const (
  22. // BoardTypeNone is a project board type that has no predefined columns
  23. BoardTypeNone BoardType = iota
  24. // BoardTypeBasicKanban is a project board type that has basic predefined columns
  25. BoardTypeBasicKanban
  26. // BoardTypeBugTriage is a project board type that has predefined columns suited to hunting down bugs
  27. BoardTypeBugTriage
  28. )
  29. const (
  30. // CardTypeTextOnly is a project board card type that is text only
  31. CardTypeTextOnly CardType = iota
  32. // CardTypeImagesAndText is a project board card type that has images and text
  33. CardTypeImagesAndText
  34. )
  35. // BoardColorPattern is a regexp witch can validate BoardColor
  36. var BoardColorPattern = regexp.MustCompile("^#[0-9a-fA-F]{6}$")
  37. // Board is used to represent boards on a project
  38. type Board struct {
  39. ID int64 `xorm:"pk autoincr"`
  40. Title string
  41. Default bool `xorm:"NOT NULL DEFAULT false"` // issues not assigned to a specific board will be assigned to this board
  42. Sorting int8 `xorm:"NOT NULL DEFAULT 0"`
  43. Color string `xorm:"VARCHAR(7)"`
  44. ProjectID int64 `xorm:"INDEX NOT NULL"`
  45. CreatorID int64 `xorm:"NOT NULL"`
  46. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  47. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  48. }
  49. // TableName return the real table name
  50. func (Board) TableName() string {
  51. return "project_board"
  52. }
  53. // NumIssues return counter of all issues assigned to the board
  54. func (b *Board) NumIssues(ctx context.Context) int {
  55. c, err := db.GetEngine(ctx).Table("project_issue").
  56. Where("project_id=?", b.ProjectID).
  57. And("project_board_id=?", b.ID).
  58. GroupBy("issue_id").
  59. Cols("issue_id").
  60. Count()
  61. if err != nil {
  62. return 0
  63. }
  64. return int(c)
  65. }
  66. func init() {
  67. db.RegisterModel(new(Board))
  68. }
  69. // IsBoardTypeValid checks if the project board type is valid
  70. func IsBoardTypeValid(p BoardType) bool {
  71. switch p {
  72. case BoardTypeNone, BoardTypeBasicKanban, BoardTypeBugTriage:
  73. return true
  74. default:
  75. return false
  76. }
  77. }
  78. // IsCardTypeValid checks if the project board card type is valid
  79. func IsCardTypeValid(p CardType) bool {
  80. switch p {
  81. case CardTypeTextOnly, CardTypeImagesAndText:
  82. return true
  83. default:
  84. return false
  85. }
  86. }
  87. func createBoardsForProjectsType(ctx context.Context, project *Project) error {
  88. var items []string
  89. switch project.BoardType {
  90. case BoardTypeBugTriage:
  91. items = setting.Project.ProjectBoardBugTriageType
  92. case BoardTypeBasicKanban:
  93. items = setting.Project.ProjectBoardBasicKanbanType
  94. case BoardTypeNone:
  95. fallthrough
  96. default:
  97. return nil
  98. }
  99. board := Board{
  100. CreatedUnix: timeutil.TimeStampNow(),
  101. CreatorID: project.CreatorID,
  102. Title: "Backlog",
  103. ProjectID: project.ID,
  104. Default: true,
  105. }
  106. if err := db.Insert(ctx, board); err != nil {
  107. return err
  108. }
  109. if len(items) == 0 {
  110. return nil
  111. }
  112. boards := make([]Board, 0, len(items))
  113. for _, v := range items {
  114. boards = append(boards, Board{
  115. CreatedUnix: timeutil.TimeStampNow(),
  116. CreatorID: project.CreatorID,
  117. Title: v,
  118. ProjectID: project.ID,
  119. })
  120. }
  121. return db.Insert(ctx, boards)
  122. }
  123. // NewBoard adds a new project board to a given project
  124. func NewBoard(ctx context.Context, board *Board) error {
  125. if len(board.Color) != 0 && !BoardColorPattern.MatchString(board.Color) {
  126. return fmt.Errorf("bad color code: %s", board.Color)
  127. }
  128. _, err := db.GetEngine(ctx).Insert(board)
  129. return err
  130. }
  131. // DeleteBoardByID removes all issues references to the project board.
  132. func DeleteBoardByID(ctx context.Context, boardID int64) error {
  133. ctx, committer, err := db.TxContext(ctx)
  134. if err != nil {
  135. return err
  136. }
  137. defer committer.Close()
  138. if err := deleteBoardByID(ctx, boardID); err != nil {
  139. return err
  140. }
  141. return committer.Commit()
  142. }
  143. func deleteBoardByID(ctx context.Context, boardID int64) error {
  144. board, err := GetBoard(ctx, boardID)
  145. if err != nil {
  146. if IsErrProjectBoardNotExist(err) {
  147. return nil
  148. }
  149. return err
  150. }
  151. if board.Default {
  152. return fmt.Errorf("deleteBoardByID: cannot delete default board")
  153. }
  154. if err = board.removeIssues(ctx); err != nil {
  155. return err
  156. }
  157. if _, err := db.GetEngine(ctx).ID(board.ID).NoAutoCondition().Delete(board); err != nil {
  158. return err
  159. }
  160. return nil
  161. }
  162. func deleteBoardByProjectID(ctx context.Context, projectID int64) error {
  163. _, err := db.GetEngine(ctx).Where("project_id=?", projectID).Delete(&Board{})
  164. return err
  165. }
  166. // GetBoard fetches the current board of a project
  167. func GetBoard(ctx context.Context, boardID int64) (*Board, error) {
  168. board := new(Board)
  169. has, err := db.GetEngine(ctx).ID(boardID).Get(board)
  170. if err != nil {
  171. return nil, err
  172. } else if !has {
  173. return nil, ErrProjectBoardNotExist{BoardID: boardID}
  174. }
  175. return board, nil
  176. }
  177. // UpdateBoard updates a project board
  178. func UpdateBoard(ctx context.Context, board *Board) error {
  179. var fieldToUpdate []string
  180. if board.Sorting != 0 {
  181. fieldToUpdate = append(fieldToUpdate, "sorting")
  182. }
  183. if board.Title != "" {
  184. fieldToUpdate = append(fieldToUpdate, "title")
  185. }
  186. if len(board.Color) != 0 && !BoardColorPattern.MatchString(board.Color) {
  187. return fmt.Errorf("bad color code: %s", board.Color)
  188. }
  189. fieldToUpdate = append(fieldToUpdate, "color")
  190. _, err := db.GetEngine(ctx).ID(board.ID).Cols(fieldToUpdate...).Update(board)
  191. return err
  192. }
  193. // GetBoards fetches all boards related to a project
  194. func (p *Project) GetBoards(ctx context.Context) (BoardList, error) {
  195. boards := make([]*Board, 0, 5)
  196. if err := db.GetEngine(ctx).Where("project_id=? AND `default`=?", p.ID, false).OrderBy("sorting").Find(&boards); err != nil {
  197. return nil, err
  198. }
  199. defaultB, err := p.getDefaultBoard(ctx)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return append([]*Board{defaultB}, boards...), nil
  204. }
  205. // getDefaultBoard return default board and ensure only one exists
  206. func (p *Project) getDefaultBoard(ctx context.Context) (*Board, error) {
  207. var boards []Board
  208. if err := db.GetEngine(ctx).Where("project_id=? AND `default` = ?", p.ID, true).OrderBy("sorting").Find(&boards); err != nil {
  209. return nil, err
  210. }
  211. // create a default board if none is found
  212. if len(boards) == 0 {
  213. board := Board{
  214. ProjectID: p.ID,
  215. Default: true,
  216. Title: "Uncategorized",
  217. CreatorID: p.CreatorID,
  218. }
  219. if _, err := db.GetEngine(ctx).Insert(); err != nil {
  220. return nil, err
  221. }
  222. return &board, nil
  223. }
  224. // unset default boards where too many default boards exist
  225. if len(boards) > 1 {
  226. var boardsToUpdate []int64
  227. for id, b := range boards {
  228. if id > 0 {
  229. boardsToUpdate = append(boardsToUpdate, b.ID)
  230. }
  231. }
  232. if _, err := db.GetEngine(ctx).Where(builder.Eq{"project_id": p.ID}.And(builder.In("id", boardsToUpdate))).
  233. Cols("`default`").Update(&Board{Default: false}); err != nil {
  234. return nil, err
  235. }
  236. }
  237. return &boards[0], nil
  238. }
  239. // SetDefaultBoard represents a board for issues not assigned to one
  240. func SetDefaultBoard(ctx context.Context, projectID, boardID int64) error {
  241. if _, err := GetBoard(ctx, boardID); err != nil {
  242. return err
  243. }
  244. if _, err := db.GetEngine(ctx).Where(builder.Eq{
  245. "project_id": projectID,
  246. "`default`": true,
  247. }).Cols("`default`").Update(&Board{Default: false}); err != nil {
  248. return err
  249. }
  250. _, err := db.GetEngine(ctx).ID(boardID).Where(builder.Eq{"project_id": projectID}).
  251. Cols("`default`").Update(&Board{Default: true})
  252. return err
  253. }
  254. // UpdateBoardSorting update project board sorting
  255. func UpdateBoardSorting(ctx context.Context, bs BoardList) error {
  256. for i := range bs {
  257. _, err := db.GetEngine(ctx).ID(bs[i].ID).Cols(
  258. "sorting",
  259. ).Update(bs[i])
  260. if err != nil {
  261. return err
  262. }
  263. }
  264. return nil
  265. }