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

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