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 4.2KB

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