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.

192 lines
5.1 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. // GetAssigneesByIssue returns everyone assigned to that issue
  34. func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) {
  35. return getAssigneesByIssue(x, issue)
  36. }
  37. func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error) {
  38. err = issue.loadAssignees(e)
  39. if err != nil {
  40. return assignees, err
  41. }
  42. return issue.Assignees, nil
  43. }
  44. // IsUserAssignedToIssue returns true when the user is assigned to the issue
  45. func IsUserAssignedToIssue(issue *Issue, user *User) (isAssigned bool, err error) {
  46. return isUserAssignedToIssue(x, issue, user)
  47. }
  48. func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool, err error) {
  49. return e.Get(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
  50. }
  51. // MakeAssigneeList concats a string with all names of the assignees. Useful for logs.
  52. func MakeAssigneeList(issue *Issue) (assigneeList string, err error) {
  53. err = issue.loadAssignees(x)
  54. if err != nil {
  55. return "", err
  56. }
  57. for in, assignee := range issue.Assignees {
  58. assigneeList += assignee.Name
  59. if len(issue.Assignees) > (in + 1) {
  60. assigneeList += ", "
  61. }
  62. }
  63. return
  64. }
  65. // ClearAssigneeByUserID deletes all assignments of an user
  66. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  67. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  68. return
  69. }
  70. // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
  71. func (issue *Issue) ToggleAssignee(doer *User, assigneeID int64) (removed bool, comment *Comment, err error) {
  72. sess := x.NewSession()
  73. defer sess.Close()
  74. if err := sess.Begin(); err != nil {
  75. return false, nil, err
  76. }
  77. removed, comment, err = issue.toggleAssignee(sess, doer, assigneeID, false)
  78. if err != nil {
  79. return false, nil, err
  80. }
  81. if err := sess.Commit(); err != nil {
  82. return false, nil, err
  83. }
  84. return removed, comment, nil
  85. }
  86. func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
  87. removed, err = toggleUserAssignee(sess, issue, assigneeID)
  88. if err != nil {
  89. return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  90. }
  91. // Repo infos
  92. if err = issue.loadRepo(sess); err != nil {
  93. return false, nil, fmt.Errorf("loadRepo: %v", err)
  94. }
  95. // Comment
  96. comment, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed)
  97. if err != nil {
  98. return false, nil, fmt.Errorf("createAssigneeComment: %v", err)
  99. }
  100. // if pull request is in the middle of creation - don't call webhook
  101. if isCreate {
  102. return removed, comment, err
  103. }
  104. return removed, comment, nil
  105. }
  106. // toggles user assignee state in database
  107. func toggleUserAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (removed bool, err error) {
  108. // Check if the user exists
  109. assignee, err := getUserByID(e, assigneeID)
  110. if err != nil {
  111. return false, err
  112. }
  113. // Check if the submitted user is already assigned, if yes delete him otherwise add him
  114. var i int
  115. for i = 0; i < len(issue.Assignees); i++ {
  116. if issue.Assignees[i].ID == assigneeID {
  117. break
  118. }
  119. }
  120. assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
  121. toBeDeleted := i < len(issue.Assignees)
  122. if toBeDeleted {
  123. issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...)
  124. _, err = e.Delete(assigneeIn)
  125. if err != nil {
  126. return toBeDeleted, err
  127. }
  128. } else {
  129. issue.Assignees = append(issue.Assignees, assignee)
  130. _, err = e.Insert(assigneeIn)
  131. if err != nil {
  132. return toBeDeleted, err
  133. }
  134. }
  135. return toBeDeleted, nil
  136. }
  137. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  138. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  139. var requestAssignees []string
  140. // Keeping the old assigning method for compatibility reasons
  141. if oneAssignee != "" && !util.IsStringInSlice(oneAssignee, multipleAssignees) {
  142. requestAssignees = append(requestAssignees, oneAssignee)
  143. }
  144. //Prevent empty assignees
  145. if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
  146. requestAssignees = append(requestAssignees, multipleAssignees...)
  147. }
  148. // Get the IDs of all assignees
  149. assigneeIDs, err = GetUserIDsByNames(requestAssignees, false)
  150. return
  151. }