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.

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