summaryrefslogtreecommitdiffstats
path: root/services/issue/milestone.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /services/issue/milestone.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--services/issue/milestone.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/services/issue/milestone.go b/services/issue/milestone.go
new file mode 100644
index 0000000..31490c7
--- /dev/null
+++ b/services/issue/milestone.go
@@ -0,0 +1,110 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package issue
+
+import (
+ "context"
+ "fmt"
+
+ "code.gitea.io/gitea/models/db"
+ issues_model "code.gitea.io/gitea/models/issues"
+ user_model "code.gitea.io/gitea/models/user"
+ notify_service "code.gitea.io/gitea/services/notify"
+)
+
+func updateMilestoneCounters(ctx context.Context, issue *issues_model.Issue, id int64) error {
+ if issue.NoAutoTime {
+ // We set the milestone's update date to the max of the
+ // milestone and issue update dates.
+ // Note: we can not call UpdateMilestoneCounters() if the
+ // milestone's update date is to be kept, because that function
+ // auto-updates the dates.
+ milestone, err := issues_model.GetMilestoneByRepoID(ctx, issue.RepoID, id)
+ if err != nil {
+ return fmt.Errorf("GetMilestoneByRepoID: %w", err)
+ }
+ updatedUnix := milestone.UpdatedUnix
+ if issue.UpdatedUnix > updatedUnix {
+ updatedUnix = issue.UpdatedUnix
+ }
+ if err := issues_model.UpdateMilestoneCountersWithDate(ctx, id, updatedUnix); err != nil {
+ return err
+ }
+ } else {
+ if err := issues_model.UpdateMilestoneCounters(ctx, id); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error {
+ // Only check if milestone exists if we don't remove it.
+ if issue.MilestoneID > 0 {
+ has, err := issues_model.HasMilestoneByRepoID(ctx, issue.RepoID, issue.MilestoneID)
+ if err != nil {
+ return fmt.Errorf("HasMilestoneByRepoID: %w", err)
+ }
+ if !has {
+ return fmt.Errorf("HasMilestoneByRepoID: issue doesn't exist")
+ }
+ }
+
+ if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
+ return err
+ }
+
+ if oldMilestoneID > 0 {
+ if err := updateMilestoneCounters(ctx, issue, oldMilestoneID); err != nil {
+ return err
+ }
+ }
+
+ if issue.MilestoneID > 0 {
+ if err := updateMilestoneCounters(ctx, issue, issue.MilestoneID); err != nil {
+ return err
+ }
+ }
+
+ if oldMilestoneID > 0 || issue.MilestoneID > 0 {
+ if err := issue.LoadRepo(ctx); err != nil {
+ return err
+ }
+
+ opts := &issues_model.CreateCommentOptions{
+ Type: issues_model.CommentTypeMilestone,
+ Doer: doer,
+ Repo: issue.Repo,
+ Issue: issue,
+ OldMilestoneID: oldMilestoneID,
+ MilestoneID: issue.MilestoneID,
+ }
+ if _, err := issues_model.CreateComment(ctx, opts); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// ChangeMilestoneAssign changes assignment of milestone for issue.
+func ChangeMilestoneAssign(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
+ dbCtx, committer, err := db.TxContext(ctx)
+ if err != nil {
+ return err
+ }
+ defer committer.Close()
+
+ if err = changeMilestoneAssign(dbCtx, doer, issue, oldMilestoneID); err != nil {
+ return err
+ }
+
+ if err = committer.Commit(); err != nil {
+ return fmt.Errorf("Commit: %w", err)
+ }
+
+ notify_service.IssueChangeMilestone(ctx, doer, issue, oldMilestoneID)
+
+ return nil
+}