本站源代码
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.

517 lines
12KB

  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 (
  6. "fmt"
  7. "xorm.io/builder"
  8. )
  9. // IssueList defines a list of issues
  10. type IssueList []*Issue
  11. const (
  12. // default variables number on IN () in SQL
  13. defaultMaxInSize = 50
  14. )
  15. func (issues IssueList) getRepoIDs() []int64 {
  16. repoIDs := make(map[int64]struct{}, len(issues))
  17. for _, issue := range issues {
  18. if _, ok := repoIDs[issue.RepoID]; !ok {
  19. repoIDs[issue.RepoID] = struct{}{}
  20. }
  21. }
  22. return keysInt64(repoIDs)
  23. }
  24. func (issues IssueList) loadRepositories(e Engine) ([]*Repository, error) {
  25. if len(issues) == 0 {
  26. return nil, nil
  27. }
  28. repoIDs := issues.getRepoIDs()
  29. repoMaps := make(map[int64]*Repository, len(repoIDs))
  30. var left = len(repoIDs)
  31. for left > 0 {
  32. var limit = defaultMaxInSize
  33. if left < limit {
  34. limit = left
  35. }
  36. err := e.
  37. In("id", repoIDs[:limit]).
  38. Find(&repoMaps)
  39. if err != nil {
  40. return nil, fmt.Errorf("find repository: %v", err)
  41. }
  42. left -= limit
  43. repoIDs = repoIDs[limit:]
  44. }
  45. for _, issue := range issues {
  46. issue.Repo = repoMaps[issue.RepoID]
  47. }
  48. return valuesRepository(repoMaps), nil
  49. }
  50. // LoadRepositories loads issues' all repositories
  51. func (issues IssueList) LoadRepositories() ([]*Repository, error) {
  52. return issues.loadRepositories(x)
  53. }
  54. func (issues IssueList) getPosterIDs() []int64 {
  55. posterIDs := make(map[int64]struct{}, len(issues))
  56. for _, issue := range issues {
  57. if _, ok := posterIDs[issue.PosterID]; !ok {
  58. posterIDs[issue.PosterID] = struct{}{}
  59. }
  60. }
  61. return keysInt64(posterIDs)
  62. }
  63. func (issues IssueList) loadPosters(e Engine) error {
  64. if len(issues) == 0 {
  65. return nil
  66. }
  67. posterIDs := issues.getPosterIDs()
  68. posterMaps := make(map[int64]*User, len(posterIDs))
  69. var left = len(posterIDs)
  70. for left > 0 {
  71. var limit = defaultMaxInSize
  72. if left < limit {
  73. limit = left
  74. }
  75. err := e.
  76. In("id", posterIDs[:limit]).
  77. Find(&posterMaps)
  78. if err != nil {
  79. return err
  80. }
  81. left -= limit
  82. posterIDs = posterIDs[limit:]
  83. }
  84. for _, issue := range issues {
  85. if issue.PosterID <= 0 {
  86. continue
  87. }
  88. var ok bool
  89. if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
  90. issue.Poster = NewGhostUser()
  91. }
  92. }
  93. return nil
  94. }
  95. func (issues IssueList) getIssueIDs() []int64 {
  96. var ids = make([]int64, 0, len(issues))
  97. for _, issue := range issues {
  98. ids = append(ids, issue.ID)
  99. }
  100. return ids
  101. }
  102. func (issues IssueList) loadLabels(e Engine) error {
  103. if len(issues) == 0 {
  104. return nil
  105. }
  106. type LabelIssue struct {
  107. Label *Label `xorm:"extends"`
  108. IssueLabel *IssueLabel `xorm:"extends"`
  109. }
  110. var issueLabels = make(map[int64][]*Label, len(issues)*3)
  111. var issueIDs = issues.getIssueIDs()
  112. var left = len(issueIDs)
  113. for left > 0 {
  114. var limit = defaultMaxInSize
  115. if left < limit {
  116. limit = left
  117. }
  118. rows, err := e.Table("label").
  119. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  120. In("issue_label.issue_id", issueIDs[:limit]).
  121. Asc("label.name").
  122. Rows(new(LabelIssue))
  123. if err != nil {
  124. return err
  125. }
  126. for rows.Next() {
  127. var labelIssue LabelIssue
  128. err = rows.Scan(&labelIssue)
  129. if err != nil {
  130. if err1 := rows.Close(); err1 != nil {
  131. return fmt.Errorf("IssueList.loadLabels: Close: %v", err1)
  132. }
  133. return err
  134. }
  135. issueLabels[labelIssue.IssueLabel.IssueID] = append(issueLabels[labelIssue.IssueLabel.IssueID], labelIssue.Label)
  136. }
  137. // When there are no rows left and we try to close it.
  138. // Since that is not relevant for us, we can safely ignore it.
  139. if err1 := rows.Close(); err1 != nil {
  140. return fmt.Errorf("IssueList.loadLabels: Close: %v", err1)
  141. }
  142. left -= limit
  143. issueIDs = issueIDs[limit:]
  144. }
  145. for _, issue := range issues {
  146. issue.Labels = issueLabels[issue.ID]
  147. }
  148. return nil
  149. }
  150. func (issues IssueList) getMilestoneIDs() []int64 {
  151. var ids = make(map[int64]struct{}, len(issues))
  152. for _, issue := range issues {
  153. if _, ok := ids[issue.MilestoneID]; !ok {
  154. ids[issue.MilestoneID] = struct{}{}
  155. }
  156. }
  157. return keysInt64(ids)
  158. }
  159. func (issues IssueList) loadMilestones(e Engine) error {
  160. milestoneIDs := issues.getMilestoneIDs()
  161. if len(milestoneIDs) == 0 {
  162. return nil
  163. }
  164. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  165. var left = len(milestoneIDs)
  166. for left > 0 {
  167. var limit = defaultMaxInSize
  168. if left < limit {
  169. limit = left
  170. }
  171. err := e.
  172. In("id", milestoneIDs[:limit]).
  173. Find(&milestoneMaps)
  174. if err != nil {
  175. return err
  176. }
  177. left -= limit
  178. milestoneIDs = milestoneIDs[limit:]
  179. }
  180. for _, issue := range issues {
  181. issue.Milestone = milestoneMaps[issue.MilestoneID]
  182. }
  183. return nil
  184. }
  185. func (issues IssueList) loadAssignees(e Engine) error {
  186. if len(issues) == 0 {
  187. return nil
  188. }
  189. type AssigneeIssue struct {
  190. IssueAssignee *IssueAssignees `xorm:"extends"`
  191. Assignee *User `xorm:"extends"`
  192. }
  193. var assignees = make(map[int64][]*User, len(issues))
  194. var issueIDs = issues.getIssueIDs()
  195. var left = len(issueIDs)
  196. for left > 0 {
  197. var limit = defaultMaxInSize
  198. if left < limit {
  199. limit = left
  200. }
  201. rows, err := e.Table("issue_assignees").
  202. Join("INNER", "`user`", "`user`.id = `issue_assignees`.assignee_id").
  203. In("`issue_assignees`.issue_id", issueIDs[:limit]).
  204. Rows(new(AssigneeIssue))
  205. if err != nil {
  206. return err
  207. }
  208. for rows.Next() {
  209. var assigneeIssue AssigneeIssue
  210. err = rows.Scan(&assigneeIssue)
  211. if err != nil {
  212. if err1 := rows.Close(); err1 != nil {
  213. return fmt.Errorf("IssueList.loadAssignees: Close: %v", err1)
  214. }
  215. return err
  216. }
  217. assignees[assigneeIssue.IssueAssignee.IssueID] = append(assignees[assigneeIssue.IssueAssignee.IssueID], assigneeIssue.Assignee)
  218. }
  219. if err1 := rows.Close(); err1 != nil {
  220. return fmt.Errorf("IssueList.loadAssignees: Close: %v", err1)
  221. }
  222. left -= limit
  223. issueIDs = issueIDs[limit:]
  224. }
  225. for _, issue := range issues {
  226. issue.Assignees = assignees[issue.ID]
  227. }
  228. return nil
  229. }
  230. func (issues IssueList) getPullIssueIDs() []int64 {
  231. var ids = make([]int64, 0, len(issues))
  232. for _, issue := range issues {
  233. if issue.IsPull && issue.PullRequest == nil {
  234. ids = append(ids, issue.ID)
  235. }
  236. }
  237. return ids
  238. }
  239. func (issues IssueList) loadPullRequests(e Engine) error {
  240. issuesIDs := issues.getPullIssueIDs()
  241. if len(issuesIDs) == 0 {
  242. return nil
  243. }
  244. pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
  245. var left = len(issuesIDs)
  246. for left > 0 {
  247. var limit = defaultMaxInSize
  248. if left < limit {
  249. limit = left
  250. }
  251. rows, err := e.
  252. In("issue_id", issuesIDs[:limit]).
  253. Rows(new(PullRequest))
  254. if err != nil {
  255. return err
  256. }
  257. for rows.Next() {
  258. var pr PullRequest
  259. err = rows.Scan(&pr)
  260. if err != nil {
  261. if err1 := rows.Close(); err1 != nil {
  262. return fmt.Errorf("IssueList.loadPullRequests: Close: %v", err1)
  263. }
  264. return err
  265. }
  266. pullRequestMaps[pr.IssueID] = &pr
  267. }
  268. if err1 := rows.Close(); err1 != nil {
  269. return fmt.Errorf("IssueList.loadPullRequests: Close: %v", err1)
  270. }
  271. left -= limit
  272. issuesIDs = issuesIDs[limit:]
  273. }
  274. for _, issue := range issues {
  275. issue.PullRequest = pullRequestMaps[issue.ID]
  276. }
  277. return nil
  278. }
  279. func (issues IssueList) loadAttachments(e Engine) (err error) {
  280. if len(issues) == 0 {
  281. return nil
  282. }
  283. var attachments = make(map[int64][]*Attachment, len(issues))
  284. var issuesIDs = issues.getIssueIDs()
  285. var left = len(issuesIDs)
  286. for left > 0 {
  287. var limit = defaultMaxInSize
  288. if left < limit {
  289. limit = left
  290. }
  291. rows, err := e.Table("attachment").
  292. Join("INNER", "issue", "issue.id = attachment.issue_id").
  293. In("issue.id", issuesIDs[:limit]).
  294. Rows(new(Attachment))
  295. if err != nil {
  296. return err
  297. }
  298. for rows.Next() {
  299. var attachment Attachment
  300. err = rows.Scan(&attachment)
  301. if err != nil {
  302. if err1 := rows.Close(); err1 != nil {
  303. return fmt.Errorf("IssueList.loadAttachments: Close: %v", err1)
  304. }
  305. return err
  306. }
  307. attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
  308. }
  309. if err1 := rows.Close(); err1 != nil {
  310. return fmt.Errorf("IssueList.loadAttachments: Close: %v", err1)
  311. }
  312. left -= limit
  313. issuesIDs = issuesIDs[limit:]
  314. }
  315. for _, issue := range issues {
  316. issue.Attachments = attachments[issue.ID]
  317. }
  318. return nil
  319. }
  320. func (issues IssueList) loadComments(e Engine, cond builder.Cond) (err error) {
  321. if len(issues) == 0 {
  322. return nil
  323. }
  324. var comments = make(map[int64][]*Comment, len(issues))
  325. var issuesIDs = issues.getIssueIDs()
  326. var left = len(issuesIDs)
  327. for left > 0 {
  328. var limit = defaultMaxInSize
  329. if left < limit {
  330. limit = left
  331. }
  332. rows, err := e.Table("comment").
  333. Join("INNER", "issue", "issue.id = comment.issue_id").
  334. In("issue.id", issuesIDs[:limit]).
  335. Where(cond).
  336. Rows(new(Comment))
  337. if err != nil {
  338. return err
  339. }
  340. for rows.Next() {
  341. var comment Comment
  342. err = rows.Scan(&comment)
  343. if err != nil {
  344. if err1 := rows.Close(); err1 != nil {
  345. return fmt.Errorf("IssueList.loadComments: Close: %v", err1)
  346. }
  347. return err
  348. }
  349. comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
  350. }
  351. if err1 := rows.Close(); err1 != nil {
  352. return fmt.Errorf("IssueList.loadComments: Close: %v", err1)
  353. }
  354. left -= limit
  355. issuesIDs = issuesIDs[limit:]
  356. }
  357. for _, issue := range issues {
  358. issue.Comments = comments[issue.ID]
  359. }
  360. return nil
  361. }
  362. func (issues IssueList) loadTotalTrackedTimes(e Engine) (err error) {
  363. type totalTimesByIssue struct {
  364. IssueID int64
  365. Time int64
  366. }
  367. if len(issues) == 0 {
  368. return nil
  369. }
  370. var trackedTimes = make(map[int64]int64, len(issues))
  371. var ids = make([]int64, 0, len(issues))
  372. for _, issue := range issues {
  373. if issue.Repo.IsTimetrackerEnabled() {
  374. ids = append(ids, issue.ID)
  375. }
  376. }
  377. var left = len(ids)
  378. for left > 0 {
  379. var limit = defaultMaxInSize
  380. if left < limit {
  381. limit = left
  382. }
  383. // select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
  384. rows, err := e.Table("tracked_time").
  385. Select("issue_id, sum(time) as time").
  386. In("issue_id", ids[:limit]).
  387. GroupBy("issue_id").
  388. Rows(new(totalTimesByIssue))
  389. if err != nil {
  390. return err
  391. }
  392. for rows.Next() {
  393. var totalTime totalTimesByIssue
  394. err = rows.Scan(&totalTime)
  395. if err != nil {
  396. if err1 := rows.Close(); err1 != nil {
  397. return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %v", err1)
  398. }
  399. return err
  400. }
  401. trackedTimes[totalTime.IssueID] = totalTime.Time
  402. }
  403. if err1 := rows.Close(); err1 != nil {
  404. return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %v", err1)
  405. }
  406. left -= limit
  407. ids = ids[limit:]
  408. }
  409. for _, issue := range issues {
  410. issue.TotalTrackedTime = trackedTimes[issue.ID]
  411. }
  412. return nil
  413. }
  414. // loadAttributes loads all attributes, expect for attachments and comments
  415. func (issues IssueList) loadAttributes(e Engine) error {
  416. if _, err := issues.loadRepositories(e); err != nil {
  417. return fmt.Errorf("issue.loadAttributes: loadRepositories: %v", err)
  418. }
  419. if err := issues.loadPosters(e); err != nil {
  420. return fmt.Errorf("issue.loadAttributes: loadPosters: %v", err)
  421. }
  422. if err := issues.loadLabels(e); err != nil {
  423. return fmt.Errorf("issue.loadAttributes: loadLabels: %v", err)
  424. }
  425. if err := issues.loadMilestones(e); err != nil {
  426. return fmt.Errorf("issue.loadAttributes: loadMilestones: %v", err)
  427. }
  428. if err := issues.loadAssignees(e); err != nil {
  429. return fmt.Errorf("issue.loadAttributes: loadAssignees: %v", err)
  430. }
  431. if err := issues.loadPullRequests(e); err != nil {
  432. return fmt.Errorf("issue.loadAttributes: loadPullRequests: %v", err)
  433. }
  434. if err := issues.loadTotalTrackedTimes(e); err != nil {
  435. return fmt.Errorf("issue.loadAttributes: loadTotalTrackedTimes: %v", err)
  436. }
  437. return nil
  438. }
  439. // LoadAttributes loads attributes of the issues, except for attachments and
  440. // comments
  441. func (issues IssueList) LoadAttributes() error {
  442. return issues.loadAttributes(x)
  443. }
  444. // LoadAttachments loads attachments
  445. func (issues IssueList) LoadAttachments() error {
  446. return issues.loadAttachments(x)
  447. }
  448. // LoadComments loads comments
  449. func (issues IssueList) LoadComments() error {
  450. return issues.loadComments(x, builder.NewCond())
  451. }
  452. // LoadDiscussComments loads discuss comments
  453. func (issues IssueList) LoadDiscussComments() error {
  454. return issues.loadComments(x, builder.Eq{"comment.type": CommentTypeComment})
  455. }
上海开阖软件有限公司 沪ICP备12045867号-1