2019-11-02 09:03:20 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package issue
|
|
|
|
|
|
|
|
import (
|
2022-04-08 14:41:15 +05:30
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-02 09:03:20 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
)
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error {
|
2022-06-30 21:25:08 +05:30
|
|
|
// 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-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("HasMilestoneByRepoID: %w", err)
|
2022-06-30 21:25:08 +05:30
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return fmt.Errorf("HasMilestoneByRepoID: issue doesn't exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
|
2022-04-08 14:41:15 +05:30
|
|
|
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 15:07:59 +05:30
|
|
|
opts := &issues_model.CreateCommentOptions{
|
|
|
|
Type: issues_model.CommentTypeMilestone,
|
2022-04-08 14:41:15 +05:30
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldMilestoneID: oldMilestoneID,
|
|
|
|
MilestoneID: issue.MilestoneID,
|
|
|
|
}
|
2022-06-13 15:07:59 +05:30
|
|
|
if _, err := issues_model.CreateCommentCtx(ctx, opts); err != nil {
|
2022-04-08 14:41:15 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 09:03:20 +05:30
|
|
|
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
2022-06-13 15:07:59 +05:30
|
|
|
func ChangeMilestoneAssign(issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
|
2022-11-13 01:48:50 +05:30
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-04-08 14:41:15 +05:30
|
|
|
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-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Commit: %w", err)
|
2019-11-02 09:03:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
notification.NotifyIssueChangeMilestone(doer, issue, oldMilestoneID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|