You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/notification"
  8. )
  9. // NewIssue creates new issue with labels for repository.
  10. func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
  11. if err := models.NewIssue(repo, issue, labelIDs, uuids); err != nil {
  12. return err
  13. }
  14. for _, assigneeID := range assigneeIDs {
  15. if err := AddAssigneeIfNotAssigned(issue, issue.Poster, assigneeID); err != nil {
  16. return err
  17. }
  18. }
  19. notification.NotifyNewIssue(issue)
  20. return nil
  21. }
  22. // ChangeTitle changes the title of this issue, as the given user.
  23. func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err error) {
  24. oldTitle := issue.Title
  25. issue.Title = title
  26. if err = issue.ChangeTitle(doer, oldTitle); err != nil {
  27. return
  28. }
  29. notification.NotifyIssueChangeTitle(doer, issue, oldTitle)
  30. return nil
  31. }
  32. // UpdateAssignees is a helper function to add or delete one or multiple issue assignee(s)
  33. // Deleting is done the GitHub way (quote from their api documentation):
  34. // https://developer.github.com/v3/issues/#edit-an-issue
  35. // "assignees" (array): Logins for Users to assign to this issue.
  36. // Pass one or more user logins to replace the set of assignees on this Issue.
  37. // Send an empty array ([]) to clear all assignees from the Issue.
  38. func UpdateAssignees(issue *models.Issue, oneAssignee string, multipleAssignees []string, doer *models.User) (err error) {
  39. var allNewAssignees []*models.User
  40. // Keep the old assignee thingy for compatibility reasons
  41. if oneAssignee != "" {
  42. // Prevent double adding assignees
  43. var isDouble bool
  44. for _, assignee := range multipleAssignees {
  45. if assignee == oneAssignee {
  46. isDouble = true
  47. break
  48. }
  49. }
  50. if !isDouble {
  51. multipleAssignees = append(multipleAssignees, oneAssignee)
  52. }
  53. }
  54. // Loop through all assignees to add them
  55. for _, assigneeName := range multipleAssignees {
  56. assignee, err := models.GetUserByName(assigneeName)
  57. if err != nil {
  58. return err
  59. }
  60. allNewAssignees = append(allNewAssignees, assignee)
  61. }
  62. // Delete all old assignees not passed
  63. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  64. return err
  65. }
  66. // Add all new assignees
  67. // Update the assignee. The function will check if the user exists, is already
  68. // assigned (which he shouldn't as we deleted all assignees before) and
  69. // has access to the repo.
  70. for _, assignee := range allNewAssignees {
  71. // Extra method to prevent double adding (which would result in removing)
  72. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return
  78. }
  79. // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue.
  80. // Also checks for access of assigned user
  81. func AddAssigneeIfNotAssigned(issue *models.Issue, doer *models.User, assigneeID int64) (err error) {
  82. assignee, err := models.GetUserByID(assigneeID)
  83. if err != nil {
  84. return err
  85. }
  86. // Check if the user is already assigned
  87. isAssigned, err := models.IsUserAssignedToIssue(issue, assignee)
  88. if err != nil {
  89. return err
  90. }
  91. if isAssigned {
  92. // nothing to to
  93. return nil
  94. }
  95. valid, err := models.CanBeAssigned(assignee, issue.Repo, issue.IsPull)
  96. if err != nil {
  97. return err
  98. }
  99. if !valid {
  100. return models.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name}
  101. }
  102. _, _, err = ToggleAssignee(issue, doer, assigneeID)
  103. if err != nil {
  104. return err
  105. }
  106. return nil
  107. }