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 13KB

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