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.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "context"
  6. "fmt"
  7. activities_model "code.gitea.io/gitea/models/activities"
  8. "code.gitea.io/gitea/models/db"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. project_model "code.gitea.io/gitea/models/project"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. system_model "code.gitea.io/gitea/models/system"
  14. user_model "code.gitea.io/gitea/models/user"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/notification"
  17. "code.gitea.io/gitea/modules/storage"
  18. )
  19. // NewIssue creates new issue with labels for repository.
  20. func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
  21. if err := issues_model.NewIssue(repo, issue, labelIDs, uuids); err != nil {
  22. return err
  23. }
  24. for _, assigneeID := range assigneeIDs {
  25. if err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID); err != nil {
  26. return err
  27. }
  28. }
  29. mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, issue.Poster, issue.Content)
  30. if err != nil {
  31. return err
  32. }
  33. notification.NotifyNewIssue(ctx, issue, mentions)
  34. if len(issue.Labels) > 0 {
  35. notification.NotifyIssueChangeLabels(ctx, issue.Poster, issue, issue.Labels, nil)
  36. }
  37. if issue.Milestone != nil {
  38. notification.NotifyIssueChangeMilestone(ctx, issue.Poster, issue, 0)
  39. }
  40. return nil
  41. }
  42. // ChangeTitle changes the title of this issue, as the given user.
  43. func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, title string) (err error) {
  44. oldTitle := issue.Title
  45. issue.Title = title
  46. if err = issues_model.ChangeIssueTitle(ctx, issue, doer, oldTitle); err != nil {
  47. return
  48. }
  49. notification.NotifyIssueChangeTitle(ctx, doer, issue, oldTitle)
  50. return nil
  51. }
  52. // ChangeIssueRef changes the branch of this issue, as the given user.
  53. func ChangeIssueRef(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, ref string) error {
  54. oldRef := issue.Ref
  55. issue.Ref = ref
  56. if err := issues_model.ChangeIssueRef(issue, doer, oldRef); err != nil {
  57. return err
  58. }
  59. notification.NotifyIssueChangeRef(ctx, doer, issue, oldRef)
  60. return nil
  61. }
  62. // UpdateAssignees is a helper function to add or delete one or multiple issue assignee(s)
  63. // Deleting is done the GitHub way (quote from their api documentation):
  64. // https://developer.github.com/v3/issues/#edit-an-issue
  65. // "assignees" (array): Logins for Users to assign to this issue.
  66. // Pass one or more user logins to replace the set of assignees on this Issue.
  67. // Send an empty array ([]) to clear all assignees from the Issue.
  68. func UpdateAssignees(ctx context.Context, issue *issues_model.Issue, oneAssignee string, multipleAssignees []string, doer *user_model.User) (err error) {
  69. var allNewAssignees []*user_model.User
  70. // Keep the old assignee thingy for compatibility reasons
  71. if oneAssignee != "" {
  72. // Prevent double adding assignees
  73. var isDouble bool
  74. for _, assignee := range multipleAssignees {
  75. if assignee == oneAssignee {
  76. isDouble = true
  77. break
  78. }
  79. }
  80. if !isDouble {
  81. multipleAssignees = append(multipleAssignees, oneAssignee)
  82. }
  83. }
  84. // Loop through all assignees to add them
  85. for _, assigneeName := range multipleAssignees {
  86. assignee, err := user_model.GetUserByName(ctx, assigneeName)
  87. if err != nil {
  88. return err
  89. }
  90. allNewAssignees = append(allNewAssignees, assignee)
  91. }
  92. // Delete all old assignees not passed
  93. if err = DeleteNotPassedAssignee(ctx, issue, doer, allNewAssignees); err != nil {
  94. return err
  95. }
  96. // Add all new assignees
  97. // Update the assignee. The function will check if the user exists, is already
  98. // assigned (which he shouldn't as we deleted all assignees before) and
  99. // has access to the repo.
  100. for _, assignee := range allNewAssignees {
  101. // Extra method to prevent double adding (which would result in removing)
  102. err = AddAssigneeIfNotAssigned(ctx, issue, doer, assignee.ID)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. return err
  108. }
  109. // DeleteIssue deletes an issue
  110. func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue) error {
  111. // load issue before deleting it
  112. if err := issue.LoadAttributes(ctx); err != nil {
  113. return err
  114. }
  115. if err := issue.LoadPullRequest(ctx); err != nil {
  116. return err
  117. }
  118. // delete entries in database
  119. if err := deleteIssue(ctx, issue); err != nil {
  120. return err
  121. }
  122. // delete pull request related git data
  123. if issue.IsPull && gitRepo != nil {
  124. if err := gitRepo.RemoveReference(fmt.Sprintf("%s%d/head", git.PullPrefix, issue.PullRequest.Index)); err != nil {
  125. return err
  126. }
  127. }
  128. // If the Issue is pinned, we should unpin it before deletion to avoid problems with other pinned Issues
  129. if issue.IsPinned() {
  130. if err := issue.Unpin(ctx, doer); err != nil {
  131. return err
  132. }
  133. }
  134. notification.NotifyDeleteIssue(ctx, doer, issue)
  135. return nil
  136. }
  137. // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue.
  138. // Also checks for access of assigned user
  139. func AddAssigneeIfNotAssigned(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) {
  140. assignee, err := user_model.GetUserByID(ctx, assigneeID)
  141. if err != nil {
  142. return err
  143. }
  144. // Check if the user is already assigned
  145. isAssigned, err := issues_model.IsUserAssignedToIssue(ctx, issue, assignee)
  146. if err != nil {
  147. return err
  148. }
  149. if isAssigned {
  150. // nothing to to
  151. return nil
  152. }
  153. valid, err := access_model.CanBeAssigned(ctx, assignee, issue.Repo, issue.IsPull)
  154. if err != nil {
  155. return err
  156. }
  157. if !valid {
  158. return repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name}
  159. }
  160. _, _, err = ToggleAssignee(ctx, issue, doer, assigneeID)
  161. if err != nil {
  162. return err
  163. }
  164. return nil
  165. }
  166. // GetRefEndNamesAndURLs retrieves the ref end names (e.g. refs/heads/branch-name -> branch-name)
  167. // and their respective URLs.
  168. func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[int64]string, map[int64]string) {
  169. issueRefEndNames := make(map[int64]string, len(issues))
  170. issueRefURLs := make(map[int64]string, len(issues))
  171. for _, issue := range issues {
  172. if issue.Ref != "" {
  173. issueRefEndNames[issue.ID] = git.RefName(issue.Ref).ShortName()
  174. issueRefURLs[issue.ID] = git.RefURL(repoLink, issue.Ref)
  175. }
  176. }
  177. return issueRefEndNames, issueRefURLs
  178. }
  179. // deleteIssue deletes the issue
  180. func deleteIssue(ctx context.Context, issue *issues_model.Issue) error {
  181. ctx, committer, err := db.TxContext(ctx)
  182. if err != nil {
  183. return err
  184. }
  185. defer committer.Close()
  186. e := db.GetEngine(ctx)
  187. if _, err := e.ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
  188. return err
  189. }
  190. // update the total issue numbers
  191. if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, false); err != nil {
  192. return err
  193. }
  194. // if the issue is closed, update the closed issue numbers
  195. if issue.IsClosed {
  196. if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
  197. return err
  198. }
  199. }
  200. if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil {
  201. return fmt.Errorf("error updating counters for milestone id %d: %w",
  202. issue.MilestoneID, err)
  203. }
  204. if err := activities_model.DeleteIssueActions(ctx, issue.RepoID, issue.ID); err != nil {
  205. return err
  206. }
  207. // find attachments related to this issue and remove them
  208. if err := issue.LoadAttributes(ctx); err != nil {
  209. return err
  210. }
  211. for i := range issue.Attachments {
  212. system_model.RemoveStorageWithNotice(ctx, storage.Attachments, "Delete issue attachment", issue.Attachments[i].RelativePath())
  213. }
  214. // delete all database data still assigned to this issue
  215. if err := issues_model.DeleteInIssue(ctx, issue.ID,
  216. &issues_model.ContentHistory{},
  217. &issues_model.Comment{},
  218. &issues_model.IssueLabel{},
  219. &issues_model.IssueDependency{},
  220. &issues_model.IssueAssignees{},
  221. &issues_model.IssueUser{},
  222. &activities_model.Notification{},
  223. &issues_model.Reaction{},
  224. &issues_model.IssueWatch{},
  225. &issues_model.Stopwatch{},
  226. &issues_model.TrackedTime{},
  227. &project_model.ProjectIssue{},
  228. &repo_model.Attachment{},
  229. &issues_model.PullRequest{},
  230. ); err != nil {
  231. return err
  232. }
  233. // References to this issue in other issues
  234. if _, err := db.DeleteByBean(ctx, &issues_model.Comment{
  235. RefIssueID: issue.ID,
  236. }); err != nil {
  237. return err
  238. }
  239. // Delete dependencies for issues in other repositories
  240. if _, err := db.DeleteByBean(ctx, &issues_model.IssueDependency{
  241. DependencyID: issue.ID,
  242. }); err != nil {
  243. return err
  244. }
  245. // delete from dependent issues
  246. if _, err := db.DeleteByBean(ctx, &issues_model.Comment{
  247. DependentIssueID: issue.ID,
  248. }); err != nil {
  249. return err
  250. }
  251. return committer.Commit()
  252. }