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.

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