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.

287 lines
7.8 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/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. mode, _ := accessLevel(sess, doer.ID, issue.Repo)
  129. if issue.IsPull {
  130. // if pull request is in the middle of creation - don't call webhook
  131. if isCreate {
  132. return nil
  133. }
  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.APIFormat(mode),
  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. apiIssue := &api.IssuePayload{
  155. Index: issue.Index,
  156. Issue: issue.APIFormat(),
  157. Repository: issue.Repo.APIFormat(mode),
  158. Sender: doer.APIFormat(),
  159. }
  160. if removed {
  161. apiIssue.Action = api.HookIssueUnassigned
  162. } else {
  163. apiIssue.Action = api.HookIssueAssigned
  164. }
  165. if err := prepareWebhooks(sess, issue.Repo, HookEventIssues, apiIssue); err != nil {
  166. log.Error(4, "PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  167. return nil
  168. }
  169. }
  170. go HookQueue.Add(issue.RepoID)
  171. return nil
  172. }
  173. // UpdateAPIAssignee is a helper function to add or delete one or multiple issue assignee(s)
  174. // Deleting is done the Github way (quote from their api documentation):
  175. // https://developer.github.com/v3/issues/#edit-an-issue
  176. // "assignees" (array): Logins for Users to assign to this issue.
  177. // Pass one or more user logins to replace the set of assignees on this Issue.
  178. // Send an empty array ([]) to clear all assignees from the Issue.
  179. func UpdateAPIAssignee(issue *Issue, oneAssignee string, multipleAssignees []string, doer *User) (err error) {
  180. var allNewAssignees []*User
  181. // Keep the old assignee thingy for compatibility reasons
  182. if oneAssignee != "" {
  183. // Prevent double adding assignees
  184. var isDouble bool
  185. for _, assignee := range multipleAssignees {
  186. if assignee == oneAssignee {
  187. isDouble = true
  188. break
  189. }
  190. }
  191. if !isDouble {
  192. multipleAssignees = append(multipleAssignees, oneAssignee)
  193. }
  194. }
  195. // Loop through all assignees to add them
  196. for _, assigneeName := range multipleAssignees {
  197. assignee, err := GetUserByName(assigneeName)
  198. if err != nil {
  199. return err
  200. }
  201. allNewAssignees = append(allNewAssignees, assignee)
  202. }
  203. // Delete all old assignees not passed
  204. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  205. return err
  206. }
  207. // Add all new assignees
  208. // Update the assignee. The function will check if the user exists, is already
  209. // assigned (which he shouldn't as we deleted all assignees before) and
  210. // has access to the repo.
  211. for _, assignee := range allNewAssignees {
  212. // Extra method to prevent double adding (which would result in removing)
  213. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. return
  219. }
  220. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  221. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  222. // Keeping the old assigning method for compatibility reasons
  223. if oneAssignee != "" {
  224. // Prevent double adding assignees
  225. var isDouble bool
  226. for _, assignee := range multipleAssignees {
  227. if assignee == oneAssignee {
  228. isDouble = true
  229. break
  230. }
  231. }
  232. if !isDouble {
  233. multipleAssignees = append(multipleAssignees, oneAssignee)
  234. }
  235. }
  236. // Get the IDs of all assignees
  237. assigneeIDs = GetUserIDsByNames(multipleAssignees)
  238. return
  239. }