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