gitea源码

issue.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package project
  4. import (
  5. "context"
  6. "errors"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/util"
  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. // ProjectColumnID should not be zero since 1.22. If it's zero, the issue will not be displayed on UI and it might result in errors.
  16. ProjectColumnID int64 `xorm:"'project_board_id' INDEX"`
  17. // the sorting order on the column
  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. func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Column) error {
  28. if c.ProjectID != newColumn.ProjectID {
  29. return errors.New("columns have to be in the same project")
  30. }
  31. if c.ID == newColumn.ID {
  32. return nil
  33. }
  34. res := struct {
  35. MaxSorting int64
  36. IssueCount int64
  37. }{}
  38. if _, err := db.GetEngine(ctx).Select("max(sorting) as max_sorting, count(*) as issue_count").
  39. Table("project_issue").
  40. Where("project_id=?", newColumn.ProjectID).
  41. And("project_board_id=?", newColumn.ID).
  42. Get(&res); err != nil {
  43. return err
  44. }
  45. issues, err := c.GetIssues(ctx)
  46. if err != nil {
  47. return err
  48. }
  49. if len(issues) == 0 {
  50. return nil
  51. }
  52. nextSorting := util.Iif(res.IssueCount > 0, res.MaxSorting+1, 0)
  53. return db.WithTx(ctx, func(ctx context.Context) error {
  54. for i, issue := range issues {
  55. issue.ProjectColumnID = newColumn.ID
  56. issue.Sorting = nextSorting + int64(i)
  57. if _, err := db.GetEngine(ctx).ID(issue.ID).Cols("project_board_id", "sorting").Update(issue); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. })
  63. }
  64. // DeleteAllProjectIssueByIssueIDsAndProjectIDs delete all project's issues by issue's and project's ids
  65. func DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx context.Context, issueIDs, projectIDs []int64) error {
  66. _, err := db.GetEngine(ctx).In("project_id", projectIDs).In("issue_id", issueIDs).Delete(&ProjectIssue{})
  67. return err
  68. }