2022-03-29 10:16:31 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 10:16:31 -04:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
package issues
|
2022-03-29 10:16:31 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
project_model "code.gitea.io/gitea/models/project"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-05-08 09:44:57 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-29 10:16:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// LoadProject load the project the issue was assigned to
|
2023-02-20 14:21:56 -05:00
|
|
|
func (issue *Issue) LoadProject(ctx context.Context) (err error) {
|
2022-06-20 06:02:49 -04:00
|
|
|
if issue.Project == nil {
|
2022-03-29 10:16:31 -04:00
|
|
|
var p project_model.Project
|
2023-07-12 10:10:35 -04:00
|
|
|
has, err := db.GetEngine(ctx).Table("project").
|
2022-03-29 10:16:31 -04:00
|
|
|
Join("INNER", "project_issue", "project.id=project_issue.project_id").
|
2023-07-12 10:10:35 -04:00
|
|
|
Where("project_issue.issue_id = ?", issue.ID).Get(&p)
|
|
|
|
if err != nil {
|
2022-03-29 10:16:31 -04:00
|
|
|
return err
|
2023-07-12 10:10:35 -04:00
|
|
|
} else if has {
|
|
|
|
issue.Project = &p
|
2022-03-29 10:16:31 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 06:02:49 -04:00
|
|
|
return err
|
2022-03-29 10:16:31 -04:00
|
|
|
}
|
|
|
|
|
2022-06-20 06:02:49 -04:00
|
|
|
func (issue *Issue) projectID(ctx context.Context) int64 {
|
2022-03-29 10:16:31 -04:00
|
|
|
var ip project_model.ProjectIssue
|
2022-06-20 06:02:49 -04:00
|
|
|
has, err := db.GetEngine(ctx).Where("issue_id=?", issue.ID).Get(&ip)
|
2022-03-29 10:16:31 -04:00
|
|
|
if err != nil || !has {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ip.ProjectID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectBoardID return project board id if issue was assigned to one
|
2023-10-11 00:24:07 -04:00
|
|
|
func (issue *Issue) ProjectBoardID(ctx context.Context) int64 {
|
2022-03-29 10:16:31 -04:00
|
|
|
var ip project_model.ProjectIssue
|
2022-06-20 06:02:49 -04:00
|
|
|
has, err := db.GetEngine(ctx).Where("issue_id=?", issue.ID).Get(&ip)
|
2022-03-29 10:16:31 -04:00
|
|
|
if err != nil || !has {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ip.ProjectBoardID
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIssuesFromBoard load issues assigned to this board
|
2022-11-19 03:12:33 -05:00
|
|
|
func LoadIssuesFromBoard(ctx context.Context, b *project_model.Board) (IssueList, error) {
|
2024-03-27 16:54:32 -04:00
|
|
|
issueList, err := Issues(ctx, &IssuesOptions{
|
|
|
|
ProjectBoardID: b.ID,
|
|
|
|
ProjectID: b.ProjectID,
|
|
|
|
SortType: "project-column-sorting",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-03-29 10:16:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.Default {
|
2022-11-19 03:12:33 -05:00
|
|
|
issues, err := Issues(ctx, &IssuesOptions{
|
2023-10-20 08:01:25 -04:00
|
|
|
ProjectBoardID: db.NoConditionID,
|
2022-03-29 10:16:31 -04:00
|
|
|
ProjectID: b.ProjectID,
|
2022-08-31 14:15:12 -04:00
|
|
|
SortType: "project-column-sorting",
|
2022-03-29 10:16:31 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issueList = append(issueList, issues...)
|
|
|
|
}
|
|
|
|
|
2023-08-07 15:26:40 -04:00
|
|
|
if err := issueList.LoadComments(ctx); err != nil {
|
2022-03-29 10:16:31 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issueList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIssuesFromBoardList load issues assigned to the boards
|
2022-11-19 03:12:33 -05:00
|
|
|
func LoadIssuesFromBoardList(ctx context.Context, bs project_model.BoardList) (map[int64]IssueList, error) {
|
2022-03-29 10:16:31 -04:00
|
|
|
issuesMap := make(map[int64]IssueList, len(bs))
|
|
|
|
for i := range bs {
|
2022-11-19 03:12:33 -05:00
|
|
|
il, err := LoadIssuesFromBoard(ctx, bs[i])
|
2022-03-29 10:16:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issuesMap[bs[i].ID] = il
|
|
|
|
}
|
|
|
|
return issuesMap, nil
|
|
|
|
}
|
|
|
|
|
2024-05-08 09:44:57 -04:00
|
|
|
// IssueAssignOrRemoveProject changes the project associated with an issue
|
|
|
|
// If newProjectID is 0, the issue is removed from the project
|
|
|
|
func IssueAssignOrRemoveProject(ctx context.Context, issue *Issue, doer *user_model.User, newProjectID, newColumnID int64) error {
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
oldProjectID := issue.projectID(ctx)
|
2022-03-29 10:16:31 -04:00
|
|
|
|
2024-05-08 09:44:57 -04:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-29 10:16:31 -04:00
|
|
|
|
2024-05-08 09:44:57 -04: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
|
|
|
|
}
|
|
|
|
if !newProject.CanBeAccessedByOwnerRepo(issue.Repo.OwnerID, issue.Repo) {
|
|
|
|
return util.NewPermissionDeniedErrorf("issue %d can't be accessed by project %d", issue.ID, newProject.ID)
|
|
|
|
}
|
|
|
|
if newColumnID == 0 {
|
|
|
|
newDefaultColumn, err := newProject.GetDefaultBoard(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newColumnID = newDefaultColumn.ID
|
|
|
|
}
|
|
|
|
}
|
2023-01-20 06:42:33 -05:00
|
|
|
|
2024-05-08 09:44:57 -04:00
|
|
|
if _, err := db.GetEngine(ctx).Where("project_issue.issue_id=?", issue.ID).Delete(&project_model.ProjectIssue{}); err != nil {
|
2022-06-30 11:55:08 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-08 09:44:57 -04:00
|
|
|
if oldProjectID > 0 || newProjectID > 0 {
|
|
|
|
if _, err := CreateComment(ctx, &CreateCommentOptions{
|
|
|
|
Type: CommentTypeProject,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldProjectID: oldProjectID,
|
|
|
|
ProjectID: newProjectID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newProjectID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newColumnID == 0 {
|
|
|
|
panic("newColumnID must not be zero") // shouldn't happen
|
|
|
|
}
|
2022-03-29 10:16:31 -04:00
|
|
|
|
2024-05-08 09:44:57 -04:00
|
|
|
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=?", newProjectID).
|
|
|
|
And("project_board_id=?", newColumnID).
|
|
|
|
Get(&res); err != nil {
|
2022-03-29 10:16:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2024-05-08 09:44:57 -04:00
|
|
|
newSorting := util.Iif(res.IssueCount > 0, res.MaxSorting+1, 0)
|
|
|
|
return db.Insert(ctx, &project_model.ProjectIssue{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
ProjectID: newProjectID,
|
|
|
|
ProjectBoardID: newColumnID,
|
|
|
|
Sorting: newSorting,
|
|
|
|
})
|
2022-03-29 10:16:31 -04:00
|
|
|
})
|
|
|
|
}
|