|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package project |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/db" |
| 11 | + issues_model "code.gitea.io/gitea/models/issues" |
| 12 | + project_model "code.gitea.io/gitea/models/project" |
| 13 | + user_model "code.gitea.io/gitea/models/user" |
| 14 | +) |
| 15 | + |
| 16 | +// MoveIssuesOnProjectColumn moves or keeps issues in a column and sorts them inside that column |
| 17 | +func MoveIssuesOnProjectColumn(ctx context.Context, doer *user_model.User, column *project_model.Column, sortedIssueIDs map[int64]int64) error { |
| 18 | + return db.WithTx(ctx, func(ctx context.Context) error { |
| 19 | + issueIDs := make([]int64, 0, len(sortedIssueIDs)) |
| 20 | + for _, issueID := range sortedIssueIDs { |
| 21 | + issueIDs = append(issueIDs, issueID) |
| 22 | + } |
| 23 | + count, err := db.GetEngine(ctx). |
| 24 | + Where("project_id=?", column.ProjectID). |
| 25 | + In("issue_id", issueIDs). |
| 26 | + Count(new(project_model.ProjectIssue)) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + if int(count) != len(sortedIssueIDs) { |
| 31 | + return fmt.Errorf("all issues have to be added to a project first") |
| 32 | + } |
| 33 | + |
| 34 | + issues, err := issues_model.GetIssuesByIDs(ctx, issueIDs) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + if _, err := issues.LoadRepositories(ctx); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + project, err := project_model.GetProjectByID(ctx, column.ProjectID) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + issuesMap := make(map[int64]*issues_model.Issue, len(issues)) |
| 48 | + for _, issue := range issues { |
| 49 | + issuesMap[issue.ID] = issue |
| 50 | + } |
| 51 | + |
| 52 | + for sorting, issueID := range sortedIssueIDs { |
| 53 | + curIssue := issuesMap[issueID] |
| 54 | + if curIssue == nil { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + _, err = db.Exec(ctx, "UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?", column.ID, sorting, issueID) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // add timeline to issue |
| 64 | + if _, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{ |
| 65 | + Type: issues_model.CommentTypeProjectColumn, |
| 66 | + Doer: doer, |
| 67 | + Repo: curIssue.Repo, |
| 68 | + Issue: curIssue, |
| 69 | + ProjectID: column.ProjectID, |
| 70 | + ProjectTitle: project.Title, |
| 71 | + ProjectColumnID: column.ID, |
| 72 | + ProjectColumnTitle: column.Title, |
| 73 | + }); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | + }) |
| 79 | +} |
0 commit comments