2019-11-02 04:33:20 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-02 04:33:20 +01:00
|
|
|
|
|
|
|
package issue
|
|
|
|
|
|
|
|
import (
|
2022-04-08 11:11:15 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-02 04:33:20 +01:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
)
|
|
|
|
|
2023-08-10 19:35:32 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error {
|
2022-06-30 17:55:08 +02:00
|
|
|
// 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 {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("HasMilestoneByRepoID: %w", err)
|
2022-06-30 17:55:08 +02:00
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return fmt.Errorf("HasMilestoneByRepoID: issue doesn't exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
|
2022-04-08 11:11:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMilestoneID > 0 {
|
2023-08-10 19:35:32 +02:00
|
|
|
if err := updateMilestoneCounters(ctx, issue, oldMilestoneID); err != nil {
|
|
|
|
return err
|
2022-04-08 11:11:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.MilestoneID > 0 {
|
2023-08-10 19:35:32 +02:00
|
|
|
if err := updateMilestoneCounters(ctx, issue, issue.MilestoneID); err != nil {
|
|
|
|
return err
|
2022-04-08 11:11:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMilestoneID > 0 || issue.MilestoneID > 0 {
|
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
opts := &issues_model.CreateCommentOptions{
|
|
|
|
Type: issues_model.CommentTypeMilestone,
|
2022-04-08 11:11:15 +02:00
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldMilestoneID: oldMilestoneID,
|
|
|
|
MilestoneID: issue.MilestoneID,
|
|
|
|
}
|
2022-12-10 03:46:31 +01:00
|
|
|
if _, err := issues_model.CreateComment(ctx, opts); err != nil {
|
2022-04-08 11:11:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 04:33:20 +01:00
|
|
|
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
2022-06-13 11:37:59 +02:00
|
|
|
func ChangeMilestoneAssign(issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
|
2022-11-12 21:18:50 +01:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-04-08 11:11:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
if err = changeMilestoneAssign(ctx, doer, issue, oldMilestoneID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = committer.Commit(); err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("Commit: %w", err)
|
2019-11-02 04:33:20 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
notification.NotifyIssueChangeMilestone(db.DefaultContext, doer, issue, oldMilestoneID)
|
2019-11-02 04:33:20 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|