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.

332 lines
6.9 KiB

  1. // Copyright 2017 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 "fmt"
  6. // IssueList defines a list of issues
  7. type IssueList []*Issue
  8. func (issues IssueList) getRepoIDs() []int64 {
  9. repoIDs := make(map[int64]struct{}, len(issues))
  10. for _, issue := range issues {
  11. if _, ok := repoIDs[issue.RepoID]; !ok {
  12. repoIDs[issue.RepoID] = struct{}{}
  13. }
  14. }
  15. return keysInt64(repoIDs)
  16. }
  17. func (issues IssueList) loadRepositories(e Engine) ([]*Repository, error) {
  18. if len(issues) == 0 {
  19. return nil, nil
  20. }
  21. repoIDs := issues.getRepoIDs()
  22. repoMaps := make(map[int64]*Repository, len(repoIDs))
  23. err := e.
  24. In("id", repoIDs).
  25. Find(&repoMaps)
  26. if err != nil {
  27. return nil, fmt.Errorf("find repository: %v", err)
  28. }
  29. for _, issue := range issues {
  30. issue.Repo = repoMaps[issue.RepoID]
  31. }
  32. return valuesRepository(repoMaps), nil
  33. }
  34. // LoadRepositories loads issues' all repositories
  35. func (issues IssueList) LoadRepositories() ([]*Repository, error) {
  36. return issues.loadRepositories(x)
  37. }
  38. func (issues IssueList) getPosterIDs() []int64 {
  39. posterIDs := make(map[int64]struct{}, len(issues))
  40. for _, issue := range issues {
  41. if _, ok := posterIDs[issue.PosterID]; !ok {
  42. posterIDs[issue.PosterID] = struct{}{}
  43. }
  44. }
  45. return keysInt64(posterIDs)
  46. }
  47. func (issues IssueList) loadPosters(e Engine) error {
  48. if len(issues) == 0 {
  49. return nil
  50. }
  51. posterIDs := issues.getPosterIDs()
  52. posterMaps := make(map[int64]*User, len(posterIDs))
  53. err := e.
  54. In("id", posterIDs).
  55. Find(&posterMaps)
  56. if err != nil {
  57. return err
  58. }
  59. for _, issue := range issues {
  60. if issue.PosterID <= 0 {
  61. continue
  62. }
  63. var ok bool
  64. if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
  65. issue.Poster = NewGhostUser()
  66. }
  67. }
  68. return nil
  69. }
  70. func (issues IssueList) getIssueIDs() []int64 {
  71. var ids = make([]int64, 0, len(issues))
  72. for _, issue := range issues {
  73. ids = append(ids, issue.ID)
  74. }
  75. return ids
  76. }
  77. func (issues IssueList) loadLabels(e Engine) error {
  78. if len(issues) == 0 {
  79. return nil
  80. }
  81. type LabelIssue struct {
  82. Label *Label `xorm:"extends"`
  83. IssueLabel *IssueLabel `xorm:"extends"`
  84. }
  85. var issueLabels = make(map[int64][]*Label, len(issues)*3)
  86. rows, err := e.Table("label").
  87. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  88. In("issue_label.issue_id", issues.getIssueIDs()).
  89. Asc("label.name").
  90. Rows(new(LabelIssue))
  91. if err != nil {
  92. return err
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. var labelIssue LabelIssue
  97. err = rows.Scan(&labelIssue)
  98. if err != nil {
  99. return err
  100. }
  101. issueLabels[labelIssue.IssueLabel.IssueID] = append(issueLabels[labelIssue.IssueLabel.IssueID], labelIssue.Label)
  102. }
  103. for _, issue := range issues {
  104. issue.Labels = issueLabels[issue.ID]
  105. }
  106. return nil
  107. }
  108. func (issues IssueList) getMilestoneIDs() []int64 {
  109. var ids = make(map[int64]struct{}, len(issues))
  110. for _, issue := range issues {
  111. if _, ok := ids[issue.MilestoneID]; !ok {
  112. ids[issue.MilestoneID] = struct{}{}
  113. }
  114. }
  115. return keysInt64(ids)
  116. }
  117. func (issues IssueList) loadMilestones(e Engine) error {
  118. milestoneIDs := issues.getMilestoneIDs()
  119. if len(milestoneIDs) == 0 {
  120. return nil
  121. }
  122. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  123. err := e.
  124. In("id", milestoneIDs).
  125. Find(&milestoneMaps)
  126. if err != nil {
  127. return err
  128. }
  129. for _, issue := range issues {
  130. issue.Milestone = milestoneMaps[issue.MilestoneID]
  131. }
  132. return nil
  133. }
  134. func (issues IssueList) getAssigneeIDs() []int64 {
  135. var ids = make(map[int64]struct{}, len(issues))
  136. for _, issue := range issues {
  137. if _, ok := ids[issue.AssigneeID]; !ok {
  138. ids[issue.AssigneeID] = struct{}{}
  139. }
  140. }
  141. return keysInt64(ids)
  142. }
  143. func (issues IssueList) loadAssignees(e Engine) error {
  144. assigneeIDs := issues.getAssigneeIDs()
  145. if len(assigneeIDs) == 0 {
  146. return nil
  147. }
  148. assigneeMaps := make(map[int64]*User, len(assigneeIDs))
  149. err := e.
  150. In("id", assigneeIDs).
  151. Find(&assigneeMaps)
  152. if err != nil {
  153. return err
  154. }
  155. for _, issue := range issues {
  156. if issue.AssigneeID <= 0 {
  157. continue
  158. }
  159. var ok bool
  160. if issue.Assignee, ok = assigneeMaps[issue.AssigneeID]; !ok {
  161. issue.Assignee = NewGhostUser()
  162. }
  163. }
  164. return nil
  165. }
  166. func (issues IssueList) getPullIssueIDs() []int64 {
  167. var ids = make([]int64, 0, len(issues))
  168. for _, issue := range issues {
  169. if issue.IsPull && issue.PullRequest == nil {
  170. ids = append(ids, issue.ID)
  171. }
  172. }
  173. return ids
  174. }
  175. func (issues IssueList) loadPullRequests(e Engine) error {
  176. issuesIDs := issues.getPullIssueIDs()
  177. if len(issuesIDs) == 0 {
  178. return nil
  179. }
  180. pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
  181. rows, err := e.
  182. In("issue_id", issuesIDs).
  183. Rows(new(PullRequest))
  184. if err != nil {
  185. return err
  186. }
  187. defer rows.Close()
  188. for rows.Next() {
  189. var pr PullRequest
  190. err = rows.Scan(&pr)
  191. if err != nil {
  192. return err
  193. }
  194. pullRequestMaps[pr.IssueID] = &pr
  195. }
  196. for _, issue := range issues {
  197. issue.PullRequest = pullRequestMaps[issue.ID]
  198. }
  199. return nil
  200. }
  201. func (issues IssueList) loadAttachments(e Engine) (err error) {
  202. if len(issues) == 0 {
  203. return nil
  204. }
  205. var attachments = make(map[int64][]*Attachment, len(issues))
  206. rows, err := e.Table("attachment").
  207. Join("INNER", "issue", "issue.id = attachment.issue_id").
  208. In("issue.id", issues.getIssueIDs()).
  209. Rows(new(Attachment))
  210. if err != nil {
  211. return err
  212. }
  213. defer rows.Close()
  214. for rows.Next() {
  215. var attachment Attachment
  216. err = rows.Scan(&attachment)
  217. if err != nil {
  218. return err
  219. }
  220. attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
  221. }
  222. for _, issue := range issues {
  223. issue.Attachments = attachments[issue.ID]
  224. }
  225. return nil
  226. }
  227. func (issues IssueList) loadComments(e Engine) (err error) {
  228. if len(issues) == 0 {
  229. return nil
  230. }
  231. var comments = make(map[int64][]*Comment, len(issues))
  232. rows, err := e.Table("comment").
  233. Join("INNER", "issue", "issue.id = comment.issue_id").
  234. In("issue.id", issues.getIssueIDs()).
  235. Rows(new(Comment))
  236. if err != nil {
  237. return err
  238. }
  239. defer rows.Close()
  240. for rows.Next() {
  241. var comment Comment
  242. err = rows.Scan(&comment)
  243. if err != nil {
  244. return err
  245. }
  246. comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
  247. }
  248. for _, issue := range issues {
  249. issue.Comments = comments[issue.ID]
  250. }
  251. return nil
  252. }
  253. func (issues IssueList) loadAttributes(e Engine) (err error) {
  254. if _, err = issues.loadRepositories(e); err != nil {
  255. return
  256. }
  257. if err = issues.loadPosters(e); err != nil {
  258. return
  259. }
  260. if err = issues.loadLabels(e); err != nil {
  261. return
  262. }
  263. if err = issues.loadMilestones(e); err != nil {
  264. return
  265. }
  266. if err = issues.loadAssignees(e); err != nil {
  267. return
  268. }
  269. if err = issues.loadPullRequests(e); err != nil {
  270. return
  271. }
  272. if err = issues.loadAttachments(e); err != nil {
  273. return
  274. }
  275. if err = issues.loadComments(e); err != nil {
  276. return
  277. }
  278. return nil
  279. }
  280. // LoadAttributes loads atrributes of the issues
  281. func (issues IssueList) LoadAttributes() error {
  282. return issues.loadAttributes(x)
  283. }