From 791d7fc76aa41370860126e861cf14d98efe710e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 9 Aug 2024 09:29:02 +0800 Subject: Add issue comment when moving issues from one column to another of the project (#29311) Fix #27278 Replace #27816 This PR adds a meta-comment for an issue when dragging an issue from one column to another of a project. image --------- Co-authored-by: wxiaoguang Co-authored-by: yp05327 <576951401@qq.com> --- services/projects/issue.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 services/projects/issue.go (limited to 'services/projects/issue.go') diff --git a/services/projects/issue.go b/services/projects/issue.go new file mode 100644 index 0000000000..db1621a39f --- /dev/null +++ b/services/projects/issue.go @@ -0,0 +1,79 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package project + +import ( + "context" + "fmt" + + "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" + project_model "code.gitea.io/gitea/models/project" + user_model "code.gitea.io/gitea/models/user" +) + +// MoveIssuesOnProjectColumn moves or keeps issues in a column and sorts them inside that column +func MoveIssuesOnProjectColumn(ctx context.Context, doer *user_model.User, column *project_model.Column, sortedIssueIDs map[int64]int64) error { + return db.WithTx(ctx, func(ctx context.Context) error { + issueIDs := make([]int64, 0, len(sortedIssueIDs)) + for _, issueID := range sortedIssueIDs { + issueIDs = append(issueIDs, issueID) + } + count, err := db.GetEngine(ctx). + Where("project_id=?", column.ProjectID). + In("issue_id", issueIDs). + Count(new(project_model.ProjectIssue)) + if err != nil { + return err + } + if int(count) != len(sortedIssueIDs) { + return fmt.Errorf("all issues have to be added to a project first") + } + + issues, err := issues_model.GetIssuesByIDs(ctx, issueIDs) + if err != nil { + return err + } + if _, err := issues.LoadRepositories(ctx); err != nil { + return err + } + + project, err := project_model.GetProjectByID(ctx, column.ProjectID) + if err != nil { + return err + } + + issuesMap := make(map[int64]*issues_model.Issue, len(issues)) + for _, issue := range issues { + issuesMap[issue.ID] = issue + } + + for sorting, issueID := range sortedIssueIDs { + curIssue := issuesMap[issueID] + if curIssue == nil { + continue + } + + _, err = db.Exec(ctx, "UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?", column.ID, sorting, issueID) + if err != nil { + return err + } + + // add timeline to issue + if _, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{ + Type: issues_model.CommentTypeProjectColumn, + Doer: doer, + Repo: curIssue.Repo, + Issue: curIssue, + ProjectID: column.ProjectID, + ProjectTitle: project.Title, + ProjectColumnID: column.ID, + ProjectColumnTitle: column.Title, + }); err != nil { + return err + } + } + return nil + }) +} -- cgit v1.2.3