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.

200 lines
5.4 KiB

  1. // Copyright 2018 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 models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/util"
  8. "xorm.io/xorm"
  9. )
  10. // IssueAssignees saves all issue assignees
  11. type IssueAssignees struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. AssigneeID int64 `xorm:"INDEX"`
  14. IssueID int64 `xorm:"INDEX"`
  15. }
  16. // LoadAssignees load assignees of this issue.
  17. func (issue *Issue) LoadAssignees() error {
  18. return issue.loadAssignees(x)
  19. }
  20. // This loads all assignees of an issue
  21. func (issue *Issue) loadAssignees(e Engine) (err error) {
  22. // Reset maybe preexisting assignees
  23. issue.Assignees = []*User{}
  24. err = e.Table("`user`").
  25. Join("INNER", "issue_assignees", "assignee_id = `user`.id").
  26. Where("issue_assignees.issue_id = ?", issue.ID).
  27. Find(&issue.Assignees)
  28. if err != nil {
  29. return err
  30. }
  31. // Check if we have at least one assignee and if yes put it in as `Assignee`
  32. if len(issue.Assignees) > 0 {
  33. issue.Assignee = issue.Assignees[0]
  34. }
  35. return
  36. }
  37. // GetAssigneeIDsByIssue returns the IDs of users assigned to an issue
  38. // but skips joining with `user` for performance reasons.
  39. // User permissions must be verified elsewhere if required.
  40. func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) {
  41. userIDs := make([]int64, 0, 5)
  42. return userIDs, x.Table("issue_assignees").
  43. Cols("assignee_id").
  44. Where("issue_id = ?", issueID).
  45. Distinct("assignee_id").
  46. Find(&userIDs)
  47. }
  48. // GetAssigneesByIssue returns everyone assigned to that issue
  49. func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) {
  50. return getAssigneesByIssue(x, issue)
  51. }
  52. func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error) {
  53. err = issue.loadAssignees(e)
  54. if err != nil {
  55. return assignees, err
  56. }
  57. return issue.Assignees, nil
  58. }
  59. // IsUserAssignedToIssue returns true when the user is assigned to the issue
  60. func IsUserAssignedToIssue(issue *Issue, user *User) (isAssigned bool, err error) {
  61. return isUserAssignedToIssue(x, issue, user)
  62. }
  63. func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool, err error) {
  64. return e.Get(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
  65. }
  66. // ClearAssigneeByUserID deletes all assignments of an user
  67. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  68. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  69. return
  70. }
  71. // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
  72. func (issue *Issue) ToggleAssignee(doer *User, assigneeID int64) (removed bool, comment *Comment, err error) {
  73. sess := x.NewSession()
  74. defer sess.Close()
  75. if err := sess.Begin(); err != nil {
  76. return false, nil, err
  77. }
  78. removed, comment, err = issue.toggleAssignee(sess, doer, assigneeID, false)
  79. if err != nil {
  80. return false, nil, err
  81. }
  82. if err := sess.Commit(); err != nil {
  83. return false, nil, err
  84. }
  85. return removed, comment, nil
  86. }
  87. func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
  88. removed, err = toggleUserAssignee(sess, issue, assigneeID)
  89. if err != nil {
  90. return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  91. }
  92. // Repo infos
  93. if err = issue.loadRepo(sess); err != nil {
  94. return false, nil, fmt.Errorf("loadRepo: %v", err)
  95. }
  96. var opts = &CreateCommentOptions{
  97. Type: CommentTypeAssignees,
  98. Doer: doer,
  99. Repo: issue.Repo,
  100. Issue: issue,
  101. RemovedAssignee: removed,
  102. AssigneeID: assigneeID,
  103. }
  104. // Comment
  105. comment, err = createComment(sess, opts)
  106. if err != nil {
  107. return false, nil, fmt.Errorf("createComment: %v", err)
  108. }
  109. // if pull request is in the middle of creation - don't call webhook
  110. if isCreate {
  111. return removed, comment, err
  112. }
  113. return removed, comment, nil
  114. }
  115. // toggles user assignee state in database
  116. func toggleUserAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (removed bool, err error) {
  117. // Check if the user exists
  118. assignee, err := getUserByID(e, assigneeID)
  119. if err != nil {
  120. return false, err
  121. }
  122. // Check if the submitted user is already assigned, if yes delete him otherwise add him
  123. var i int
  124. for i = 0; i < len(issue.Assignees); i++ {
  125. if issue.Assignees[i].ID == assigneeID {
  126. break
  127. }
  128. }
  129. assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
  130. toBeDeleted := i < len(issue.Assignees)
  131. if toBeDeleted {
  132. issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...)
  133. _, err = e.Delete(assigneeIn)
  134. if err != nil {
  135. return toBeDeleted, err
  136. }
  137. } else {
  138. issue.Assignees = append(issue.Assignees, assignee)
  139. _, err = e.Insert(assigneeIn)
  140. if err != nil {
  141. return toBeDeleted, err
  142. }
  143. }
  144. return toBeDeleted, nil
  145. }
  146. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  147. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  148. var requestAssignees []string
  149. // Keeping the old assigning method for compatibility reasons
  150. if oneAssignee != "" && !util.IsStringInSlice(oneAssignee, multipleAssignees) {
  151. requestAssignees = append(requestAssignees, oneAssignee)
  152. }
  153. //Prevent empty assignees
  154. if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
  155. requestAssignees = append(requestAssignees, multipleAssignees...)
  156. }
  157. // Get the IDs of all assignees
  158. assigneeIDs, err = GetUserIDsByNames(requestAssignees, false)
  159. return
  160. }