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.

551 lines
13 KiB

Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
  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. Where("deleted = ?", false).
  386. Select("issue_id, sum(time) as time").
  387. In("issue_id", ids[:limit]).
  388. GroupBy("issue_id").
  389. Rows(new(totalTimesByIssue))
  390. if err != nil {
  391. return err
  392. }
  393. for rows.Next() {
  394. var totalTime totalTimesByIssue
  395. err = rows.Scan(&totalTime)
  396. if err != nil {
  397. if err1 := rows.Close(); err1 != nil {
  398. return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %v", err1)
  399. }
  400. return err
  401. }
  402. trackedTimes[totalTime.IssueID] = totalTime.Time
  403. }
  404. if err1 := rows.Close(); err1 != nil {
  405. return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %v", err1)
  406. }
  407. left -= limit
  408. ids = ids[limit:]
  409. }
  410. for _, issue := range issues {
  411. issue.TotalTrackedTime = trackedTimes[issue.ID]
  412. }
  413. return nil
  414. }
  415. // loadAttributes loads all attributes, expect for attachments and comments
  416. func (issues IssueList) loadAttributes(e Engine) error {
  417. if _, err := issues.loadRepositories(e); err != nil {
  418. return fmt.Errorf("issue.loadAttributes: loadRepositories: %v", err)
  419. }
  420. if err := issues.loadPosters(e); err != nil {
  421. return fmt.Errorf("issue.loadAttributes: loadPosters: %v", err)
  422. }
  423. if err := issues.loadLabels(e); err != nil {
  424. return fmt.Errorf("issue.loadAttributes: loadLabels: %v", err)
  425. }
  426. if err := issues.loadMilestones(e); err != nil {
  427. return fmt.Errorf("issue.loadAttributes: loadMilestones: %v", err)
  428. }
  429. if err := issues.loadAssignees(e); err != nil {
  430. return fmt.Errorf("issue.loadAttributes: loadAssignees: %v", err)
  431. }
  432. if err := issues.loadPullRequests(e); err != nil {
  433. return fmt.Errorf("issue.loadAttributes: loadPullRequests: %v", err)
  434. }
  435. if err := issues.loadTotalTrackedTimes(e); err != nil {
  436. return fmt.Errorf("issue.loadAttributes: loadTotalTrackedTimes: %v", err)
  437. }
  438. return nil
  439. }
  440. // LoadAttributes loads attributes of the issues, except for attachments and
  441. // comments
  442. func (issues IssueList) LoadAttributes() error {
  443. return issues.loadAttributes(x)
  444. }
  445. // LoadAttachments loads attachments
  446. func (issues IssueList) LoadAttachments() error {
  447. return issues.loadAttachments(x)
  448. }
  449. // LoadComments loads comments
  450. func (issues IssueList) LoadComments() error {
  451. return issues.loadComments(x, builder.NewCond())
  452. }
  453. // LoadDiscussComments loads discuss comments
  454. func (issues IssueList) LoadDiscussComments() error {
  455. return issues.loadComments(x, builder.Eq{"comment.type": CommentTypeComment})
  456. }
  457. // GetApprovalCounts returns a map of issue ID to slice of approval counts
  458. // FIXME: only returns official counts due to double counting of non-official approvals
  459. func (issues IssueList) GetApprovalCounts() (map[int64][]*ReviewCount, error) {
  460. return issues.getApprovalCounts(x)
  461. }
  462. func (issues IssueList) getApprovalCounts(e Engine) (map[int64][]*ReviewCount, error) {
  463. rCounts := make([]*ReviewCount, 0, 6*len(issues))
  464. ids := make([]int64, len(issues))
  465. for i, issue := range issues {
  466. ids[i] = issue.ID
  467. }
  468. sess := e.In("issue_id", ids)
  469. err := sess.Select("issue_id, type, count(id) as `count`").Where("official = ?", true).GroupBy("issue_id, type").OrderBy("issue_id").Table("review").Find(&rCounts)
  470. if err != nil {
  471. return nil, err
  472. }
  473. approvalCountMap := make(map[int64][]*ReviewCount, len(issues))
  474. if len(rCounts) > 0 {
  475. start := 0
  476. lastID := rCounts[0].IssueID
  477. for i, current := range rCounts[1:] {
  478. if lastID != current.IssueID {
  479. approvalCountMap[lastID] = rCounts[start:i]
  480. start = i
  481. lastID = current.IssueID
  482. }
  483. }
  484. approvalCountMap[lastID] = rCounts[start:]
  485. }
  486. return approvalCountMap, nil
  487. }