Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_list.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. postgerIDs := issues.getPosterIDs()
  52. posterMaps := make(map[int64]*User, len(postgerIDs))
  53. err := e.
  54. In("id", postgerIDs).
  55. Find(&posterMaps)
  56. if err != nil {
  57. return err
  58. }
  59. for _, issue := range issues {
  60. issue.Poster = posterMaps[issue.PosterID]
  61. }
  62. return nil
  63. }
  64. func (issues IssueList) getIssueIDs() []int64 {
  65. var ids = make([]int64, 0, len(issues))
  66. for _, issue := range issues {
  67. ids = append(ids, issue.ID)
  68. }
  69. return ids
  70. }
  71. func (issues IssueList) loadLabels(e Engine) error {
  72. if len(issues) == 0 {
  73. return nil
  74. }
  75. type LabelIssue struct {
  76. Label *Label `xorm:"extends"`
  77. IssueLabel *IssueLabel `xorm:"extends"`
  78. }
  79. var issueLabels = make(map[int64][]*Label, len(issues)*3)
  80. rows, err := e.Table("label").
  81. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  82. In("issue_label.issue_id", issues.getIssueIDs()).
  83. Asc("label.name").
  84. Rows(new(LabelIssue))
  85. if err != nil {
  86. return err
  87. }
  88. defer rows.Close()
  89. for rows.Next() {
  90. var labelIssue LabelIssue
  91. err = rows.Scan(&labelIssue)
  92. if err != nil {
  93. return err
  94. }
  95. issueLabels[labelIssue.IssueLabel.IssueID] = append(issueLabels[labelIssue.IssueLabel.IssueID], labelIssue.Label)
  96. }
  97. for _, issue := range issues {
  98. issue.Labels = issueLabels[issue.ID]
  99. }
  100. return nil
  101. }
  102. func (issues IssueList) getMilestoneIDs() []int64 {
  103. var ids = make(map[int64]struct{}, len(issues))
  104. for _, issue := range issues {
  105. if _, ok := ids[issue.MilestoneID]; !ok {
  106. ids[issue.MilestoneID] = struct{}{}
  107. }
  108. }
  109. return keysInt64(ids)
  110. }
  111. func (issues IssueList) loadMilestones(e Engine) error {
  112. milestoneIDs := issues.getMilestoneIDs()
  113. if len(milestoneIDs) == 0 {
  114. return nil
  115. }
  116. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  117. err := e.
  118. In("id", milestoneIDs).
  119. Find(&milestoneMaps)
  120. if err != nil {
  121. return err
  122. }
  123. for _, issue := range issues {
  124. issue.Milestone = milestoneMaps[issue.MilestoneID]
  125. }
  126. return nil
  127. }
  128. func (issues IssueList) getAssigneeIDs() []int64 {
  129. var ids = make(map[int64]struct{}, len(issues))
  130. for _, issue := range issues {
  131. if _, ok := ids[issue.AssigneeID]; !ok {
  132. ids[issue.AssigneeID] = struct{}{}
  133. }
  134. }
  135. return keysInt64(ids)
  136. }
  137. func (issues IssueList) loadAssignees(e Engine) error {
  138. assigneeIDs := issues.getAssigneeIDs()
  139. if len(assigneeIDs) == 0 {
  140. return nil
  141. }
  142. assigneeMaps := make(map[int64]*User, len(assigneeIDs))
  143. err := e.
  144. In("id", assigneeIDs).
  145. Find(&assigneeMaps)
  146. if err != nil {
  147. return err
  148. }
  149. for _, issue := range issues {
  150. issue.Assignee = assigneeMaps[issue.AssigneeID]
  151. }
  152. return nil
  153. }
  154. func (issues IssueList) getPullIssueIDs() []int64 {
  155. var ids = make([]int64, 0, len(issues))
  156. for _, issue := range issues {
  157. if issue.IsPull && issue.PullRequest == nil {
  158. ids = append(ids, issue.ID)
  159. }
  160. }
  161. return ids
  162. }
  163. func (issues IssueList) loadPullRequests(e Engine) error {
  164. issuesIDs := issues.getPullIssueIDs()
  165. if len(issuesIDs) == 0 {
  166. return nil
  167. }
  168. pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
  169. rows, err := e.
  170. In("issue_id", issuesIDs).
  171. Rows(new(PullRequest))
  172. if err != nil {
  173. return err
  174. }
  175. defer rows.Close()
  176. for rows.Next() {
  177. var pr PullRequest
  178. err = rows.Scan(&pr)
  179. if err != nil {
  180. return err
  181. }
  182. pullRequestMaps[pr.IssueID] = &pr
  183. }
  184. for _, issue := range issues {
  185. issue.PullRequest = pullRequestMaps[issue.ID]
  186. }
  187. return nil
  188. }
  189. func (issues IssueList) loadAttachments(e Engine) (err error) {
  190. if len(issues) == 0 {
  191. return nil
  192. }
  193. var attachments = make(map[int64][]*Attachment, len(issues))
  194. rows, err := e.Table("attachment").
  195. Join("INNER", "issue", "issue.id = attachment.issue_id").
  196. In("issue.id", issues.getIssueIDs()).
  197. Rows(new(Attachment))
  198. if err != nil {
  199. return err
  200. }
  201. defer rows.Close()
  202. for rows.Next() {
  203. var attachment Attachment
  204. err = rows.Scan(&attachment)
  205. if err != nil {
  206. return err
  207. }
  208. attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
  209. }
  210. for _, issue := range issues {
  211. issue.Attachments = attachments[issue.ID]
  212. }
  213. return nil
  214. }
  215. func (issues IssueList) loadComments(e Engine) (err error) {
  216. if len(issues) == 0 {
  217. return nil
  218. }
  219. var comments = make(map[int64][]*Comment, len(issues))
  220. rows, err := e.Table("comment").
  221. Join("INNER", "issue", "issue.id = comment.issue_id").
  222. In("issue.id", issues.getIssueIDs()).
  223. Rows(new(Comment))
  224. if err != nil {
  225. return err
  226. }
  227. defer rows.Close()
  228. for rows.Next() {
  229. var comment Comment
  230. err = rows.Scan(&comment)
  231. if err != nil {
  232. return err
  233. }
  234. comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
  235. }
  236. for _, issue := range issues {
  237. issue.Comments = comments[issue.ID]
  238. }
  239. return nil
  240. }
  241. func (issues IssueList) loadAttributes(e Engine) (err error) {
  242. if _, err = issues.loadRepositories(e); err != nil {
  243. return
  244. }
  245. if err = issues.loadPosters(e); err != nil {
  246. return
  247. }
  248. if err = issues.loadLabels(e); err != nil {
  249. return
  250. }
  251. if err = issues.loadMilestones(e); err != nil {
  252. return
  253. }
  254. if err = issues.loadAssignees(e); err != nil {
  255. return
  256. }
  257. if err = issues.loadPullRequests(e); err != nil {
  258. return
  259. }
  260. if err = issues.loadAttachments(e); err != nil {
  261. return
  262. }
  263. if err = issues.loadComments(e); err != nil {
  264. return
  265. }
  266. return nil
  267. }
  268. // LoadAttributes loads atrributes of the issues
  269. func (issues IssueList) LoadAttributes() error {
  270. return issues.loadAttributes(x)
  271. }