本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

146 Zeilen
3.9KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package issue
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/notification"
  10. )
  11. // NewIssue creates new issue with labels for repository.
  12. func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
  13. if err := models.NewIssue(repo, issue, labelIDs, uuids); err != nil {
  14. return err
  15. }
  16. for _, assigneeID := range assigneeIDs {
  17. if err := AddAssigneeIfNotAssigned(issue, issue.Poster, assigneeID); err != nil {
  18. return err
  19. }
  20. }
  21. if err := models.NotifyWatchers(&models.Action{
  22. ActUserID: issue.Poster.ID,
  23. ActUser: issue.Poster,
  24. OpType: models.ActionCreateIssue,
  25. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  26. RepoID: repo.ID,
  27. Repo: repo,
  28. IsPrivate: repo.IsPrivate,
  29. }); err != nil {
  30. log.Error("NotifyWatchers: %v", err)
  31. }
  32. notification.NotifyNewIssue(issue)
  33. return nil
  34. }
  35. // ChangeTitle changes the title of this issue, as the given user.
  36. func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err error) {
  37. oldTitle := issue.Title
  38. issue.Title = title
  39. if err = issue.ChangeTitle(doer, oldTitle); err != nil {
  40. return
  41. }
  42. notification.NotifyIssueChangeTitle(doer, issue, oldTitle)
  43. return nil
  44. }
  45. // UpdateAssignees is a helper function to add or delete one or multiple issue assignee(s)
  46. // Deleting is done the GitHub way (quote from their api documentation):
  47. // https://developer.github.com/v3/issues/#edit-an-issue
  48. // "assignees" (array): Logins for Users to assign to this issue.
  49. // Pass one or more user logins to replace the set of assignees on this Issue.
  50. // Send an empty array ([]) to clear all assignees from the Issue.
  51. func UpdateAssignees(issue *models.Issue, oneAssignee string, multipleAssignees []string, doer *models.User) (err error) {
  52. var allNewAssignees []*models.User
  53. // Keep the old assignee thingy for compatibility reasons
  54. if oneAssignee != "" {
  55. // Prevent double adding assignees
  56. var isDouble bool
  57. for _, assignee := range multipleAssignees {
  58. if assignee == oneAssignee {
  59. isDouble = true
  60. break
  61. }
  62. }
  63. if !isDouble {
  64. multipleAssignees = append(multipleAssignees, oneAssignee)
  65. }
  66. }
  67. // Loop through all assignees to add them
  68. for _, assigneeName := range multipleAssignees {
  69. assignee, err := models.GetUserByName(assigneeName)
  70. if err != nil {
  71. return err
  72. }
  73. allNewAssignees = append(allNewAssignees, assignee)
  74. }
  75. // Delete all old assignees not passed
  76. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  77. return err
  78. }
  79. // Add all new assignees
  80. // Update the assignee. The function will check if the user exists, is already
  81. // assigned (which he shouldn't as we deleted all assignees before) and
  82. // has access to the repo.
  83. for _, assignee := range allNewAssignees {
  84. // Extra method to prevent double adding (which would result in removing)
  85. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. return
  91. }
  92. // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue.
  93. // Also checks for access of assigned user
  94. func AddAssigneeIfNotAssigned(issue *models.Issue, doer *models.User, assigneeID int64) (err error) {
  95. assignee, err := models.GetUserByID(assigneeID)
  96. if err != nil {
  97. return err
  98. }
  99. // Check if the user is already assigned
  100. isAssigned, err := models.IsUserAssignedToIssue(issue, assignee)
  101. if err != nil {
  102. return err
  103. }
  104. if isAssigned {
  105. // nothing to to
  106. return nil
  107. }
  108. valid, err := models.CanBeAssigned(assignee, issue.Repo, issue.IsPull)
  109. if err != nil {
  110. return err
  111. }
  112. if !valid {
  113. return models.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name}
  114. }
  115. _, _, err = ToggleAssignee(issue, doer, assigneeID)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
上海开阖软件有限公司 沪ICP备12045867号-1