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

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