2019-11-01 22:33:20 -05:00
|
|
|
// 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 04:11:15 -05:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-11-01 22:33:20 -05:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-04-08 04:11:15 -05:00
|
|
|
"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"
|
2019-11-01 22:33:20 -05:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
)
|
|
|
|
|
2022-04-08 04:11:15 -05:00
|
|
|
func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *models.Issue, oldMilestoneID int64) error {
|
|
|
|
if err := models.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &models.CreateCommentOptions{
|
|
|
|
Type: models.CommentTypeMilestone,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldMilestoneID: oldMilestoneID,
|
|
|
|
MilestoneID: issue.MilestoneID,
|
|
|
|
}
|
|
|
|
if _, err := models.CreateCommentCtx(ctx, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 22:33:20 -05:00
|
|
|
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
2021-11-24 04:49:20 -05:00
|
|
|
func ChangeMilestoneAssign(issue *models.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
|
2022-04-08 04:11:15 -05:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Commit: %v", err)
|
2019-11-01 22:33:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
notification.NotifyIssueChangeMilestone(doer, issue, oldMilestoneID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|