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_assignees.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // MakeAssigneeList concats a string with all names of the assignees. Useful for logs.
  67. func MakeAssigneeList(issue *Issue) (assigneeList string, err error) {
  68. err = issue.loadAssignees(x)
  69. if err != nil {
  70. return "", err
  71. }
  72. for in, assignee := range issue.Assignees {
  73. assigneeList += assignee.Name
  74. if len(issue.Assignees) > (in + 1) {
  75. assigneeList += ", "
  76. }
  77. }
  78. return
  79. }
  80. // ClearAssigneeByUserID deletes all assignments of an user
  81. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  82. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  83. return
  84. }
  85. // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
  86. func (issue *Issue) ToggleAssignee(doer *User, assigneeID int64) (removed bool, comment *Comment, err error) {
  87. sess := x.NewSession()
  88. defer sess.Close()
  89. if err := sess.Begin(); err != nil {
  90. return false, nil, err
  91. }
  92. removed, comment, err = issue.toggleAssignee(sess, doer, assigneeID, false)
  93. if err != nil {
  94. return false, nil, err
  95. }
  96. if err := sess.Commit(); err != nil {
  97. return false, nil, err
  98. }
  99. return removed, comment, nil
  100. }
  101. func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
  102. removed, err = toggleUserAssignee(sess, issue, assigneeID)
  103. if err != nil {
  104. return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  105. }
  106. // Repo infos
  107. if err = issue.loadRepo(sess); err != nil {
  108. return false, nil, fmt.Errorf("loadRepo: %v", err)
  109. }
  110. var opts = &CreateCommentOptions{
  111. Type: CommentTypeAssignees,
  112. Doer: doer,
  113. Repo: issue.Repo,
  114. Issue: issue,
  115. RemovedAssignee: removed,
  116. AssigneeID: assigneeID,
  117. }
  118. // Comment
  119. comment, err = createComment(sess, opts)
  120. if err != nil {
  121. return false, nil, fmt.Errorf("createComment: %v", err)
  122. }
  123. // if pull request is in the middle of creation - don't call webhook
  124. if isCreate {
  125. return removed, comment, err
  126. }
  127. return removed, comment, nil
  128. }
  129. // toggles user assignee state in database
  130. func toggleUserAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (removed bool, err error) {
  131. // Check if the user exists
  132. assignee, err := getUserByID(e, assigneeID)
  133. if err != nil {
  134. return false, err
  135. }
  136. // Check if the submitted user is already assigned, if yes delete him otherwise add him
  137. var i int
  138. for i = 0; i < len(issue.Assignees); i++ {
  139. if issue.Assignees[i].ID == assigneeID {
  140. break
  141. }
  142. }
  143. assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
  144. toBeDeleted := i < len(issue.Assignees)
  145. if toBeDeleted {
  146. issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...)
  147. _, err = e.Delete(assigneeIn)
  148. if err != nil {
  149. return toBeDeleted, err
  150. }
  151. } else {
  152. issue.Assignees = append(issue.Assignees, assignee)
  153. _, err = e.Insert(assigneeIn)
  154. if err != nil {
  155. return toBeDeleted, err
  156. }
  157. }
  158. return toBeDeleted, nil
  159. }
  160. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  161. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  162. var requestAssignees []string
  163. // Keeping the old assigning method for compatibility reasons
  164. if oneAssignee != "" && !util.IsStringInSlice(oneAssignee, multipleAssignees) {
  165. requestAssignees = append(requestAssignees, oneAssignee)
  166. }
  167. //Prevent empty assignees
  168. if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
  169. requestAssignees = append(requestAssignees, multipleAssignees...)
  170. }
  171. // Get the IDs of all assignees
  172. assigneeIDs, err = GetUserIDsByNames(requestAssignees, false)
  173. return
  174. }