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

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