2022-03-29 14:16:31 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 14:16:31 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
package issues
|
2022-03-29 14:16:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
project_model "code.gitea.io/gitea/models/project"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadProject load the project the issue was assigned to
|
2023-02-20 19:21:56 +00:00
|
|
|
func (issue *Issue) LoadProject(ctx context.Context) (err error) {
|
2022-06-20 10:02:49 +00:00
|
|
|
if issue.Project == nil {
|
2022-03-29 14:16:31 +00:00
|
|
|
var p project_model.Project
|
2023-07-12 14:10:35 +00:00
|
|
|
has, err := db.GetEngine(ctx).Table("project").
|
2022-03-29 14:16:31 +00:00
|
|
|
Join("INNER", "project_issue", "project.id=project_issue.project_id").
|
2023-07-12 14:10:35 +00:00
|
|
|
Where("project_issue.issue_id = ?", issue.ID).Get(&p)
|
|
|
|
if err != nil {
|
2022-03-29 14:16:31 +00:00
|
|
|
return err
|
2023-07-12 14:10:35 +00:00
|
|
|
} else if has {
|
|
|
|
issue.Project = &p
|
2022-03-29 14:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 10:02:49 +00:00
|
|
|
return err
|
2022-03-29 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 10:02:49 +00:00
|
|
|
func (issue *Issue) projectID(ctx context.Context) int64 {
|
2022-03-29 14:16:31 +00:00
|
|
|
var ip project_model.ProjectIssue
|
2022-06-20 10:02:49 +00:00
|
|
|
has, err := db.GetEngine(ctx).Where("issue_id=?", issue.ID).Get(&ip)
|
2022-03-29 14:16:31 +00:00
|
|
|
if err != nil || !has {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ip.ProjectID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectBoardID return project board id if issue was assigned to one
|
2022-06-20 10:02:49 +00:00
|
|
|
func (issue *Issue) ProjectBoardID() int64 {
|
|
|
|
return issue.projectBoardID(db.DefaultContext)
|
2022-03-29 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 10:02:49 +00:00
|
|
|
func (issue *Issue) projectBoardID(ctx context.Context) int64 {
|
2022-03-29 14:16:31 +00:00
|
|
|
var ip project_model.ProjectIssue
|
2022-06-20 10:02:49 +00:00
|
|
|
has, err := db.GetEngine(ctx).Where("issue_id=?", issue.ID).Get(&ip)
|
2022-03-29 14:16:31 +00:00
|
|
|
if err != nil || !has {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ip.ProjectBoardID
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIssuesFromBoard load issues assigned to this board
|
2022-11-19 08:12:33 +00:00
|
|
|
func LoadIssuesFromBoard(ctx context.Context, b *project_model.Board) (IssueList, error) {
|
2023-08-07 19:26:40 +00:00
|
|
|
issueList := make(IssueList, 0, 10)
|
2022-03-29 14:16:31 +00:00
|
|
|
|
|
|
|
if b.ID != 0 {
|
2022-11-19 08:12:33 +00:00
|
|
|
issues, err := Issues(ctx, &IssuesOptions{
|
2022-03-29 14:16:31 +00:00
|
|
|
ProjectBoardID: b.ID,
|
|
|
|
ProjectID: b.ProjectID,
|
2022-08-31 18:15:12 +00:00
|
|
|
SortType: "project-column-sorting",
|
2022-03-29 14:16:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issueList = issues
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Default {
|
2022-11-19 08:12:33 +00:00
|
|
|
issues, err := Issues(ctx, &IssuesOptions{
|
2022-03-29 14:16:31 +00:00
|
|
|
ProjectBoardID: -1, // Issues without ProjectBoardID
|
|
|
|
ProjectID: b.ProjectID,
|
2022-08-31 18:15:12 +00:00
|
|
|
SortType: "project-column-sorting",
|
2022-03-29 14:16:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issueList = append(issueList, issues...)
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:26:40 +00:00
|
|
|
if err := issueList.LoadComments(ctx); err != nil {
|
2022-03-29 14:16:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issueList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIssuesFromBoardList load issues assigned to the boards
|
2022-11-19 08:12:33 +00:00
|
|
|
func LoadIssuesFromBoardList(ctx context.Context, bs project_model.BoardList) (map[int64]IssueList, error) {
|
2022-03-29 14:16:31 +00:00
|
|
|
issuesMap := make(map[int64]IssueList, len(bs))
|
|
|
|
for i := range bs {
|
2022-11-19 08:12:33 +00:00
|
|
|
il, err := LoadIssuesFromBoard(ctx, bs[i])
|
2022-03-29 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issuesMap[bs[i].ID] = il
|
|
|
|
}
|
|
|
|
return issuesMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeProjectAssign changes the project associated with an issue
|
|
|
|
func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error {
|
2022-11-12 20:18:50 +00:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-03-29 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
if err := addUpdateIssueProject(ctx, issue, doer, newProjectID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return committer.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.User, newProjectID int64) error {
|
2022-05-20 14:08:52 +00:00
|
|
|
oldProjectID := issue.projectID(ctx)
|
2022-03-29 14:16:31 +00:00
|
|
|
|
2023-01-20 11:42:33 +00:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:55:08 +00:00
|
|
|
// Only check if we add a new project and not remove it.
|
|
|
|
if newProjectID > 0 {
|
|
|
|
newProject, err := project_model.GetProjectByID(ctx, newProjectID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-20 11:42:33 +00:00
|
|
|
if newProject.RepoID != issue.RepoID && newProject.OwnerID != issue.Repo.OwnerID {
|
2022-06-30 15:55:08 +00:00
|
|
|
return fmt.Errorf("issue's repository is not the same as project's repository")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 14:08:52 +00:00
|
|
|
if _, err := db.GetEngine(ctx).Where("project_issue.issue_id=?", issue.ID).Delete(&project_model.ProjectIssue{}); err != nil {
|
2022-03-29 14:16:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldProjectID > 0 || newProjectID > 0 {
|
2022-12-10 02:46:31 +00:00
|
|
|
if _, err := CreateComment(ctx, &CreateCommentOptions{
|
2022-03-29 14:16:31 +00:00
|
|
|
Type: CommentTypeProject,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldProjectID: oldProjectID,
|
|
|
|
ProjectID: newProjectID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 14:08:52 +00:00
|
|
|
return db.Insert(ctx, &project_model.ProjectIssue{
|
2022-03-29 14:16:31 +00:00
|
|
|
IssueID: issue.ID,
|
|
|
|
ProjectID: newProjectID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveIssueAcrossProjectBoards move a card from one board to another
|
|
|
|
func MoveIssueAcrossProjectBoards(issue *Issue, board *project_model.Board) error {
|
2022-11-12 20:18:50 +00:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-03-29 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
var pis project_model.ProjectIssue
|
|
|
|
has, err := sess.Where("issue_id=?", issue.ID).Get(&pis)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
return fmt.Errorf("issue has to be added to a project first")
|
|
|
|
}
|
|
|
|
|
|
|
|
pis.ProjectBoardID = board.ID
|
|
|
|
if _, err := sess.ID(pis.ID).Cols("project_board_id").Update(&pis); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return committer.Commit()
|
|
|
|
}
|