2022-03-29 08:16:31 -06:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 08:16:31 -06:00
|
|
|
|
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-15 20:51:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-05-08 09:46:21 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-29 08:16:31 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProjectIssue saves relation from issue to a project
|
|
|
|
type ProjectIssue struct { //revive:disable-line:exported
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
IssueID int64 `xorm:"INDEX"`
|
|
|
|
ProjectID int64 `xorm:"INDEX"`
|
|
|
|
|
2024-05-08 09:46:21 -06:00
|
|
|
// ProjectBoardID 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.
|
2022-03-29 08:16:31 -06:00
|
|
|
ProjectBoardID int64 `xorm:"INDEX"`
|
2022-05-06 08:25:59 -06:00
|
|
|
|
|
|
|
// the sorting order on the board
|
|
|
|
Sorting int64 `xorm:"NOT NULL DEFAULT 0"`
|
2022-03-29 08:16:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(ProjectIssue))
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
func deleteProjectIssuesByProjectID(ctx context.Context, projectID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).Where("project_id=?", projectID).Delete(&ProjectIssue{})
|
2022-03-29 08:16:31 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumIssues return counter of all issues assigned to a project
|
2023-09-29 06:12:54 -06:00
|
|
|
func (p *Project) NumIssues(ctx context.Context) int {
|
|
|
|
c, err := db.GetEngine(ctx).Table("project_issue").
|
2022-03-29 08:16:31 -06:00
|
|
|
Where("project_id=?", p.ID).
|
|
|
|
GroupBy("issue_id").
|
|
|
|
Cols("issue_id").
|
|
|
|
Count()
|
|
|
|
if err != nil {
|
2022-06-15 20:51:34 -06:00
|
|
|
log.Error("NumIssues: %v", err)
|
2022-03-29 08:16:31 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumClosedIssues return counter of closed issues assigned to a project
|
2023-09-29 06:12:54 -06:00
|
|
|
func (p *Project) NumClosedIssues(ctx context.Context) int {
|
|
|
|
c, err := db.GetEngine(ctx).Table("project_issue").
|
2022-03-29 08:16:31 -06:00
|
|
|
Join("INNER", "issue", "project_issue.issue_id=issue.id").
|
|
|
|
Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, true).
|
|
|
|
Cols("issue_id").
|
|
|
|
Count()
|
|
|
|
if err != nil {
|
2022-06-15 20:51:34 -06:00
|
|
|
log.Error("NumClosedIssues: %v", err)
|
2022-03-29 08:16:31 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumOpenIssues return counter of open issues assigned to a project
|
2023-09-29 06:12:54 -06:00
|
|
|
func (p *Project) NumOpenIssues(ctx context.Context) int {
|
|
|
|
c, err := db.GetEngine(ctx).Table("project_issue").
|
2022-03-29 08:16:31 -06:00
|
|
|
Join("INNER", "issue", "project_issue.issue_id=issue.id").
|
2022-06-15 20:51:34 -06:00
|
|
|
Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, false).
|
|
|
|
Cols("issue_id").
|
|
|
|
Count()
|
2022-03-29 08:16:31 -06:00
|
|
|
if err != nil {
|
2022-06-15 20:51:34 -06:00
|
|
|
log.Error("NumOpenIssues: %v", err)
|
2022-03-29 08:16:31 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column
|
2023-09-29 06:12:54 -06:00
|
|
|
func MoveIssuesOnProjectBoard(ctx context.Context, board *Board, sortedIssueIDs map[int64]int64) error {
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
2022-03-29 08:16:31 -06:00
|
|
|
sess := db.GetEngine(ctx)
|
2024-05-08 09:46:21 -06:00
|
|
|
issueIDs := util.ValuesOfMap(sortedIssueIDs)
|
2022-03-29 08:16:31 -06:00
|
|
|
|
|
|
|
count, err := sess.Table(new(ProjectIssue)).Where("project_id=?", board.ProjectID).In("issue_id", issueIDs).Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if int(count) != len(sortedIssueIDs) {
|
|
|
|
return fmt.Errorf("all issues have to be added to a project first")
|
|
|
|
}
|
|
|
|
|
|
|
|
for sorting, issueID := range sortedIssueIDs {
|
|
|
|
_, err = sess.Exec("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?", board.ID, sorting, issueID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-08 09:46:21 -06:00
|
|
|
func (b *Board) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Board) error {
|
|
|
|
if b.ProjectID != newColumn.ProjectID {
|
|
|
|
return fmt.Errorf("columns have to be in the same project")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.ID == newColumn.ID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := struct {
|
|
|
|
MaxSorting int64
|
|
|
|
IssueCount int64
|
|
|
|
}{}
|
|
|
|
if _, err := db.GetEngine(ctx).Select("max(sorting) as max_sorting, count(*) as issue_count").
|
|
|
|
Table("project_issue").
|
|
|
|
Where("project_id=?", newColumn.ProjectID).
|
|
|
|
And("project_board_id=?", newColumn.ID).
|
|
|
|
Get(&res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
issues, err := b.GetIssues(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nextSorting := util.Iif(res.IssueCount > 0, res.MaxSorting+1, 0)
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
for i, issue := range issues {
|
|
|
|
issue.ProjectBoardID = newColumn.ID
|
|
|
|
issue.Sorting = nextSorting + int64(i)
|
|
|
|
if _, err := db.GetEngine(ctx).ID(issue.ID).Cols("project_board_id", "sorting").Update(issue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-29 08:16:31 -06:00
|
|
|
}
|