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.0KB

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