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_list.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // loadAttributes loads all attributes, expect for attachments and comments
  254. func (issues IssueList) loadAttributes(e Engine) (err error) {
  255. if _, err = issues.loadRepositories(e); err != nil {
  256. return
  257. }
  258. if err = issues.loadPosters(e); err != nil {
  259. return
  260. }
  261. if err = issues.loadLabels(e); err != nil {
  262. return
  263. }
  264. if err = issues.loadMilestones(e); err != nil {
  265. return
  266. }
  267. if err = issues.loadAssignees(e); err != nil {
  268. return
  269. }
  270. if err = issues.loadPullRequests(e); err != nil {
  271. return
  272. }
  273. return nil
  274. }
  275. // LoadAttributes loads attributes of the issues, except for attachments and
  276. // comments
  277. func (issues IssueList) LoadAttributes() error {
  278. return issues.loadAttributes(x)
  279. }
  280. // LoadAttachments loads attachments
  281. func (issues IssueList) LoadAttachments() error {
  282. return issues.loadAttachments(x)
  283. }
  284. // LoadComments loads comments
  285. func (issues IssueList) LoadComments() error {
  286. return issues.loadComments(x)
  287. }