gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. user_model "code.gitea.io/gitea/models/user"
  11. notify_service "code.gitea.io/gitea/services/notify"
  12. )
  13. func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error {
  14. // Only check if milestone exists if we don't remove it.
  15. if issue.MilestoneID > 0 {
  16. has, err := issues_model.HasMilestoneByRepoID(ctx, issue.RepoID, issue.MilestoneID)
  17. if err != nil {
  18. return fmt.Errorf("HasMilestoneByRepoID: %w", err)
  19. }
  20. if !has {
  21. return errors.New("HasMilestoneByRepoID: issue doesn't exist")
  22. }
  23. }
  24. if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
  25. return err
  26. }
  27. if oldMilestoneID > 0 {
  28. if err := issues_model.UpdateMilestoneCounters(ctx, oldMilestoneID); err != nil {
  29. return err
  30. }
  31. }
  32. if issue.MilestoneID > 0 {
  33. if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil {
  34. return err
  35. }
  36. }
  37. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  38. if err := issue.LoadRepo(ctx); err != nil {
  39. return err
  40. }
  41. opts := &issues_model.CreateCommentOptions{
  42. Type: issues_model.CommentTypeMilestone,
  43. Doer: doer,
  44. Repo: issue.Repo,
  45. Issue: issue,
  46. OldMilestoneID: oldMilestoneID,
  47. MilestoneID: issue.MilestoneID,
  48. }
  49. if _, err := issues_model.CreateComment(ctx, opts); err != nil {
  50. return err
  51. }
  52. }
  53. if issue.MilestoneID == 0 {
  54. issue.Milestone = nil
  55. }
  56. return nil
  57. }
  58. // ChangeMilestoneAssign changes assignment of milestone for issue.
  59. func ChangeMilestoneAssign(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
  60. if err := db.WithTx(ctx, func(dbCtx context.Context) error {
  61. return changeMilestoneAssign(dbCtx, doer, issue, oldMilestoneID)
  62. }); err != nil {
  63. return err
  64. }
  65. notify_service.IssueChangeMilestone(ctx, doer, issue, oldMilestoneID)
  66. return nil
  67. }