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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/log"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "xorm.io/xorm"
  10. )
  11. // IssueAssignees saves all issue assignees
  12. type IssueAssignees struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. AssigneeID int64 `xorm:"INDEX"`
  15. IssueID int64 `xorm:"INDEX"`
  16. }
  17. // This loads all assignees of an issue
  18. func (issue *Issue) loadAssignees(e Engine) (err error) {
  19. // Reset maybe preexisting assignees
  20. issue.Assignees = []*User{}
  21. err = e.Table("`user`").
  22. Join("INNER", "issue_assignees", "assignee_id = `user`.id").
  23. Where("issue_assignees.issue_id = ?", issue.ID).
  24. Find(&issue.Assignees)
  25. if err != nil {
  26. return err
  27. }
  28. // Check if we have at least one assignee and if yes put it in as `Assignee`
  29. if len(issue.Assignees) > 0 {
  30. issue.Assignee = issue.Assignees[0]
  31. }
  32. return
  33. }
  34. // GetAssigneesByIssue returns everyone assigned to that issue
  35. func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) {
  36. return getAssigneesByIssue(x, issue)
  37. }
  38. func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error) {
  39. err = issue.loadAssignees(e)
  40. if err != nil {
  41. return assignees, err
  42. }
  43. return issue.Assignees, nil
  44. }
  45. // IsUserAssignedToIssue returns true when the user is assigned to the issue
  46. func IsUserAssignedToIssue(issue *Issue, user *User) (isAssigned bool, err error) {
  47. return isUserAssignedToIssue(x, issue, user)
  48. }
  49. func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool, err error) {
  50. return e.Get(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
  51. }
  52. // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
  53. func DeleteNotPassedAssignee(issue *Issue, doer *User, assignees []*User) (err error) {
  54. var found bool
  55. for _, assignee := range issue.Assignees {
  56. found = false
  57. for _, alreadyAssignee := range assignees {
  58. if assignee.ID == alreadyAssignee.ID {
  59. found = true
  60. break
  61. }
  62. }
  63. if !found {
  64. // This function also does comments and hooks, which is why we call it seperatly instead of directly removing the assignees here
  65. if _, _, err := issue.ToggleAssignee(doer, assignee.ID); err != nil {
  66. return err
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. // MakeAssigneeList concats a string with all names of the assignees. Useful for logs.
  73. func MakeAssigneeList(issue *Issue) (assigneeList string, err error) {
  74. err = issue.loadAssignees(x)
  75. if err != nil {
  76. return "", err
  77. }
  78. for in, assignee := range issue.Assignees {
  79. assigneeList += assignee.Name
  80. if len(issue.Assignees) > (in + 1) {
  81. assigneeList += ", "
  82. }
  83. }
  84. return
  85. }
  86. // ClearAssigneeByUserID deletes all assignments of an user
  87. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  88. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  89. return
  90. }
  91. // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
  92. func (issue *Issue) ToggleAssignee(doer *User, assigneeID int64) (removed bool, comment *Comment, err error) {
  93. sess := x.NewSession()
  94. defer sess.Close()
  95. if err := sess.Begin(); err != nil {
  96. return false, nil, err
  97. }
  98. removed, comment, err = issue.toggleAssignee(sess, doer, assigneeID, false)
  99. if err != nil {
  100. return false, nil, err
  101. }
  102. if err := sess.Commit(); err != nil {
  103. return false, nil, err
  104. }
  105. go HookQueue.Add(issue.RepoID)
  106. return removed, comment, nil
  107. }
  108. func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
  109. removed, err = toggleUserAssignee(sess, issue, assigneeID)
  110. if err != nil {
  111. return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  112. }
  113. // Repo infos
  114. if err = issue.loadRepo(sess); err != nil {
  115. return false, nil, fmt.Errorf("loadRepo: %v", err)
  116. }
  117. // Comment
  118. comment, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed)
  119. if err != nil {
  120. return false, nil, fmt.Errorf("createAssigneeComment: %v", err)
  121. }
  122. // if pull request is in the middle of creation - don't call webhook
  123. if isCreate {
  124. return removed, comment, err
  125. }
  126. if issue.IsPull {
  127. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypePullRequests)
  128. if err = issue.loadPullRequest(sess); err != nil {
  129. return false, nil, fmt.Errorf("loadPullRequest: %v", err)
  130. }
  131. issue.PullRequest.Issue = issue
  132. apiPullRequest := &api.PullRequestPayload{
  133. Index: issue.Index,
  134. PullRequest: issue.PullRequest.apiFormat(sess),
  135. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  136. Sender: doer.APIFormat(),
  137. }
  138. if removed {
  139. apiPullRequest.Action = api.HookIssueUnassigned
  140. } else {
  141. apiPullRequest.Action = api.HookIssueAssigned
  142. }
  143. // Assignee comment triggers a webhook
  144. if err := prepareWebhooks(sess, issue.Repo, HookEventPullRequest, apiPullRequest); err != nil {
  145. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  146. return false, nil, err
  147. }
  148. } else {
  149. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypeIssues)
  150. apiIssue := &api.IssuePayload{
  151. Index: issue.Index,
  152. Issue: issue.apiFormat(sess),
  153. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  154. Sender: doer.APIFormat(),
  155. }
  156. if removed {
  157. apiIssue.Action = api.HookIssueUnassigned
  158. } else {
  159. apiIssue.Action = api.HookIssueAssigned
  160. }
  161. // Assignee comment triggers a webhook
  162. if err := prepareWebhooks(sess, issue.Repo, HookEventIssues, apiIssue); err != nil {
  163. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  164. return false, nil, err
  165. }
  166. }
  167. return removed, comment, nil
  168. }
  169. // toggles user assignee state in database
  170. func toggleUserAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (removed bool, err error) {
  171. // Check if the user exists
  172. assignee, err := getUserByID(e, assigneeID)
  173. if err != nil {
  174. return false, err
  175. }
  176. // Check if the submitted user is already assigned, if yes delete him otherwise add him
  177. var i int
  178. for i = 0; i < len(issue.Assignees); i++ {
  179. if issue.Assignees[i].ID == assigneeID {
  180. break
  181. }
  182. }
  183. assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
  184. toBeDeleted := i < len(issue.Assignees)
  185. if toBeDeleted {
  186. issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...)
  187. _, err = e.Delete(assigneeIn)
  188. if err != nil {
  189. return toBeDeleted, err
  190. }
  191. } else {
  192. issue.Assignees = append(issue.Assignees, assignee)
  193. _, err = e.Insert(assigneeIn)
  194. if err != nil {
  195. return toBeDeleted, err
  196. }
  197. }
  198. return toBeDeleted, nil
  199. }
  200. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  201. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  202. // Keeping the old assigning method for compatibility reasons
  203. if oneAssignee != "" {
  204. // Prevent double adding assignees
  205. var isDouble bool
  206. for _, assignee := range multipleAssignees {
  207. if assignee == oneAssignee {
  208. isDouble = true
  209. break
  210. }
  211. }
  212. if !isDouble {
  213. multipleAssignees = append(multipleAssignees, oneAssignee)
  214. }
  215. }
  216. // Get the IDs of all assignees
  217. assigneeIDs, err = GetUserIDsByNames(multipleAssignees, false)
  218. return
  219. }