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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. isAssigned, err = x.Exist(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
  48. return
  49. }
  50. // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
  51. func DeleteNotPassedAssignee(issue *Issue, doer *User, assignees []*User) (err error) {
  52. var found bool
  53. for _, assignee := range issue.Assignees {
  54. found = false
  55. for _, alreadyAssignee := range assignees {
  56. if assignee.ID == alreadyAssignee.ID {
  57. found = true
  58. break
  59. }
  60. }
  61. if !found {
  62. // This function also does comments and hooks, which is why we call it seperatly instead of directly removing the assignees here
  63. if err := UpdateAssignee(issue, doer, assignee.ID); err != nil {
  64. return err
  65. }
  66. }
  67. }
  68. return nil
  69. }
  70. // MakeAssigneeList concats a string with all names of the assignees. Useful for logs.
  71. func MakeAssigneeList(issue *Issue) (assigneeList string, err error) {
  72. err = issue.loadAssignees(x)
  73. if err != nil {
  74. return "", err
  75. }
  76. for in, assignee := range issue.Assignees {
  77. assigneeList += assignee.Name
  78. if len(issue.Assignees) > (in + 1) {
  79. assigneeList += ", "
  80. }
  81. }
  82. return
  83. }
  84. // ClearAssigneeByUserID deletes all assignments of an user
  85. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  86. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  87. return
  88. }
  89. // AddAssigneeIfNotAssigned adds an assignee only if he isn't aleady assigned to the issue
  90. func AddAssigneeIfNotAssigned(issue *Issue, doer *User, assigneeID int64) (err error) {
  91. // Check if the user is already assigned
  92. isAssigned, err := IsUserAssignedToIssue(issue, &User{ID: assigneeID})
  93. if err != nil {
  94. return err
  95. }
  96. if !isAssigned {
  97. return issue.ChangeAssignee(doer, assigneeID)
  98. }
  99. return nil
  100. }
  101. // UpdateAssignee deletes or adds an assignee to an issue
  102. func UpdateAssignee(issue *Issue, doer *User, assigneeID int64) (err error) {
  103. return issue.ChangeAssignee(doer, assigneeID)
  104. }
  105. // ChangeAssignee changes the Assignee of this issue.
  106. func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
  107. sess := x.NewSession()
  108. defer sess.Close()
  109. if err := sess.Begin(); err != nil {
  110. return err
  111. }
  112. if err := issue.changeAssignee(sess, doer, assigneeID, false); err != nil {
  113. return err
  114. }
  115. if err := sess.Commit(); err != nil {
  116. return err
  117. }
  118. go HookQueue.Add(issue.RepoID)
  119. return nil
  120. }
  121. func (issue *Issue) changeAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (err error) {
  122. // Update the assignee
  123. removed, err := updateIssueAssignee(sess, issue, assigneeID)
  124. if err != nil {
  125. return fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  126. }
  127. // Repo infos
  128. if err = issue.loadRepo(sess); err != nil {
  129. return fmt.Errorf("loadRepo: %v", err)
  130. }
  131. // Comment
  132. if _, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed); err != nil {
  133. return fmt.Errorf("createAssigneeComment: %v", err)
  134. }
  135. // if pull request is in the middle of creation - don't call webhook
  136. if isCreate {
  137. return nil
  138. }
  139. if issue.IsPull {
  140. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypePullRequests)
  141. if err = issue.loadPullRequest(sess); err != nil {
  142. return fmt.Errorf("loadPullRequest: %v", err)
  143. }
  144. issue.PullRequest.Issue = issue
  145. apiPullRequest := &api.PullRequestPayload{
  146. Index: issue.Index,
  147. PullRequest: issue.PullRequest.apiFormat(sess),
  148. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  149. Sender: doer.APIFormat(),
  150. }
  151. if removed {
  152. apiPullRequest.Action = api.HookIssueUnassigned
  153. } else {
  154. apiPullRequest.Action = api.HookIssueAssigned
  155. }
  156. if err := prepareWebhooks(sess, issue.Repo, HookEventPullRequest, apiPullRequest); err != nil {
  157. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  158. return nil
  159. }
  160. } else {
  161. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypeIssues)
  162. apiIssue := &api.IssuePayload{
  163. Index: issue.Index,
  164. Issue: issue.apiFormat(sess),
  165. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  166. Sender: doer.APIFormat(),
  167. }
  168. if removed {
  169. apiIssue.Action = api.HookIssueUnassigned
  170. } else {
  171. apiIssue.Action = api.HookIssueAssigned
  172. }
  173. if err := prepareWebhooks(sess, issue.Repo, HookEventIssues, apiIssue); err != nil {
  174. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  175. return nil
  176. }
  177. }
  178. return nil
  179. }
  180. // UpdateAPIAssignee is a helper function to add or delete one or multiple issue assignee(s)
  181. // Deleting is done the GitHub way (quote from their api documentation):
  182. // https://developer.github.com/v3/issues/#edit-an-issue
  183. // "assignees" (array): Logins for Users to assign to this issue.
  184. // Pass one or more user logins to replace the set of assignees on this Issue.
  185. // Send an empty array ([]) to clear all assignees from the Issue.
  186. func UpdateAPIAssignee(issue *Issue, oneAssignee string, multipleAssignees []string, doer *User) (err error) {
  187. var allNewAssignees []*User
  188. // Keep the old assignee thingy for compatibility reasons
  189. if oneAssignee != "" {
  190. // Prevent double adding assignees
  191. var isDouble bool
  192. for _, assignee := range multipleAssignees {
  193. if assignee == oneAssignee {
  194. isDouble = true
  195. break
  196. }
  197. }
  198. if !isDouble {
  199. multipleAssignees = append(multipleAssignees, oneAssignee)
  200. }
  201. }
  202. // Loop through all assignees to add them
  203. for _, assigneeName := range multipleAssignees {
  204. assignee, err := GetUserByName(assigneeName)
  205. if err != nil {
  206. return err
  207. }
  208. allNewAssignees = append(allNewAssignees, assignee)
  209. }
  210. // Delete all old assignees not passed
  211. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  212. return err
  213. }
  214. // Add all new assignees
  215. // Update the assignee. The function will check if the user exists, is already
  216. // assigned (which he shouldn't as we deleted all assignees before) and
  217. // has access to the repo.
  218. for _, assignee := range allNewAssignees {
  219. // Extra method to prevent double adding (which would result in removing)
  220. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  221. if err != nil {
  222. return err
  223. }
  224. }
  225. return
  226. }
  227. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  228. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  229. // Keeping the old assigning method for compatibility reasons
  230. if oneAssignee != "" {
  231. // Prevent double adding assignees
  232. var isDouble bool
  233. for _, assignee := range multipleAssignees {
  234. if assignee == oneAssignee {
  235. isDouble = true
  236. break
  237. }
  238. }
  239. if !isDouble {
  240. multipleAssignees = append(multipleAssignees, oneAssignee)
  241. }
  242. }
  243. // Get the IDs of all assignees
  244. assigneeIDs = GetUserIDsByNames(multipleAssignees)
  245. return
  246. }