2017-02-22 17:03:59 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
2019-02-19 17:39:39 +03:00
import (
"fmt"
2019-06-23 18:22:43 +03:00
"xorm.io/builder"
2019-02-19 17:39:39 +03:00
)
2017-02-22 17:03:59 +03:00
// IssueList defines a list of issues
type IssueList [ ] * Issue
2018-08-02 16:49:05 +03:00
const (
// default variables number on IN () in SQL
defaultMaxInSize = 50
)
2017-02-22 17:03:59 +03:00
func ( issues IssueList ) getRepoIDs ( ) [ ] int64 {
repoIDs := make ( map [ int64 ] struct { } , len ( issues ) )
for _ , issue := range issues {
if _ , ok := repoIDs [ issue . RepoID ] ; ! ok {
repoIDs [ issue . RepoID ] = struct { } { }
}
}
return keysInt64 ( repoIDs )
}
func ( issues IssueList ) loadRepositories ( e Engine ) ( [ ] * Repository , error ) {
if len ( issues ) == 0 {
return nil , nil
}
repoIDs := issues . getRepoIDs ( )
repoMaps := make ( map [ int64 ] * Repository , len ( repoIDs ) )
2018-08-02 16:49:05 +03:00
var left = len ( repoIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
err := e .
In ( "id" , repoIDs [ : limit ] ) .
Find ( & repoMaps )
if err != nil {
return nil , fmt . Errorf ( "find repository: %v" , err )
}
2019-06-12 22:41:28 +03:00
left -= limit
2018-08-02 16:49:05 +03:00
repoIDs = repoIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . Repo = repoMaps [ issue . RepoID ]
}
return valuesRepository ( repoMaps ) , nil
}
// LoadRepositories loads issues' all repositories
func ( issues IssueList ) LoadRepositories ( ) ( [ ] * Repository , error ) {
return issues . loadRepositories ( x )
}
func ( issues IssueList ) getPosterIDs ( ) [ ] int64 {
posterIDs := make ( map [ int64 ] struct { } , len ( issues ) )
for _ , issue := range issues {
if _ , ok := posterIDs [ issue . PosterID ] ; ! ok {
posterIDs [ issue . PosterID ] = struct { } { }
}
}
return keysInt64 ( posterIDs )
}
func ( issues IssueList ) loadPosters ( e Engine ) error {
if len ( issues ) == 0 {
return nil
}
2017-03-15 03:52:01 +03:00
posterIDs := issues . getPosterIDs ( )
posterMaps := make ( map [ int64 ] * User , len ( posterIDs ) )
2018-08-02 16:49:05 +03:00
var left = len ( posterIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
err := e .
In ( "id" , posterIDs [ : limit ] ) .
Find ( & posterMaps )
if err != nil {
return err
}
2019-06-12 22:41:28 +03:00
left -= limit
2018-08-02 16:49:05 +03:00
posterIDs = posterIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
2017-06-09 04:51:31 +03:00
if issue . PosterID <= 0 {
continue
}
2017-05-30 09:08:36 +03:00
var ok bool
if issue . Poster , ok = posterMaps [ issue . PosterID ] ; ! ok {
issue . Poster = NewGhostUser ( )
}
2017-02-22 17:03:59 +03:00
}
return nil
}
func ( issues IssueList ) getIssueIDs ( ) [ ] int64 {
var ids = make ( [ ] int64 , 0 , len ( issues ) )
for _ , issue := range issues {
ids = append ( ids , issue . ID )
}
return ids
}
func ( issues IssueList ) loadLabels ( e Engine ) error {
if len ( issues ) == 0 {
return nil
}
type LabelIssue struct {
Label * Label ` xorm:"extends" `
IssueLabel * IssueLabel ` xorm:"extends" `
}
var issueLabels = make ( map [ int64 ] [ ] * Label , len ( issues ) * 3 )
2018-08-02 16:49:05 +03:00
var issueIDs = issues . getIssueIDs ( )
var left = len ( issueIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
rows , err := e . Table ( "label" ) .
Join ( "LEFT" , "issue_label" , "issue_label.label_id = label.id" ) .
In ( "issue_label.issue_id" , issueIDs [ : limit ] ) .
Asc ( "label.name" ) .
Rows ( new ( LabelIssue ) )
2017-02-22 17:03:59 +03:00
if err != nil {
return err
}
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var labelIssue LabelIssue
err = rows . Scan ( & labelIssue )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadLabels: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
issueLabels [ labelIssue . IssueLabel . IssueID ] = append ( issueLabels [ labelIssue . IssueLabel . IssueID ] , labelIssue . Label )
}
2019-06-23 18:22:43 +03:00
// When there are no rows left and we try to close it.
2019-06-12 22:41:28 +03:00
// Since that is not relevant for us, we can safely ignore it.
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadLabels: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
issueIDs = issueIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . Labels = issueLabels [ issue . ID ]
}
return nil
}
func ( issues IssueList ) getMilestoneIDs ( ) [ ] int64 {
var ids = make ( map [ int64 ] struct { } , len ( issues ) )
for _ , issue := range issues {
if _ , ok := ids [ issue . MilestoneID ] ; ! ok {
ids [ issue . MilestoneID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
func ( issues IssueList ) loadMilestones ( e Engine ) error {
milestoneIDs := issues . getMilestoneIDs ( )
if len ( milestoneIDs ) == 0 {
return nil
}
milestoneMaps := make ( map [ int64 ] * Milestone , len ( milestoneIDs ) )
2018-08-02 16:49:05 +03:00
var left = len ( milestoneIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
err := e .
In ( "id" , milestoneIDs [ : limit ] ) .
Find ( & milestoneMaps )
if err != nil {
return err
}
2019-06-12 22:41:28 +03:00
left -= limit
2018-08-02 16:49:05 +03:00
milestoneIDs = milestoneIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . Milestone = milestoneMaps [ issue . MilestoneID ]
}
return nil
}
func ( issues IssueList ) loadAssignees ( e Engine ) error {
2018-05-09 19:29:04 +03:00
if len ( issues ) == 0 {
2017-02-22 17:03:59 +03:00
return nil
}
2018-05-09 19:29:04 +03:00
type AssigneeIssue struct {
IssueAssignee * IssueAssignees ` xorm:"extends" `
Assignee * User ` xorm:"extends" `
}
var assignees = make ( map [ int64 ] [ ] * User , len ( issues ) )
2018-08-02 16:49:05 +03:00
var issueIDs = issues . getIssueIDs ( )
var left = len ( issueIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
rows , err := e . Table ( "issue_assignees" ) .
Join ( "INNER" , "`user`" , "`user`.id = `issue_assignees`.assignee_id" ) .
In ( "`issue_assignees`.issue_id" , issueIDs [ : limit ] ) .
Rows ( new ( AssigneeIssue ) )
2018-05-09 19:29:04 +03:00
if err != nil {
return err
2017-05-30 09:08:36 +03:00
}
2018-05-09 19:29:04 +03:00
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var assigneeIssue AssigneeIssue
err = rows . Scan ( & assigneeIssue )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadAssignees: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
assignees [ assigneeIssue . IssueAssignee . IssueID ] = append ( assignees [ assigneeIssue . IssueAssignee . IssueID ] , assigneeIssue . Assignee )
}
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadAssignees: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
issueIDs = issueIDs [ limit : ]
2018-05-09 19:29:04 +03:00
}
for _ , issue := range issues {
issue . Assignees = assignees [ issue . ID ]
2017-02-22 17:03:59 +03:00
}
return nil
}
func ( issues IssueList ) getPullIssueIDs ( ) [ ] int64 {
var ids = make ( [ ] int64 , 0 , len ( issues ) )
for _ , issue := range issues {
if issue . IsPull && issue . PullRequest == nil {
ids = append ( ids , issue . ID )
}
}
return ids
}
func ( issues IssueList ) loadPullRequests ( e Engine ) error {
issuesIDs := issues . getPullIssueIDs ( )
if len ( issuesIDs ) == 0 {
return nil
}
pullRequestMaps := make ( map [ int64 ] * PullRequest , len ( issuesIDs ) )
2018-08-02 16:49:05 +03:00
var left = len ( issuesIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
rows , err := e .
In ( "issue_id" , issuesIDs [ : limit ] ) .
Rows ( new ( PullRequest ) )
2017-02-22 17:03:59 +03:00
if err != nil {
return err
}
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var pr PullRequest
err = rows . Scan ( & pr )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadPullRequests: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
pullRequestMaps [ pr . IssueID ] = & pr
}
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadPullRequests: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
issuesIDs = issuesIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . PullRequest = pullRequestMaps [ issue . ID ]
}
return nil
}
func ( issues IssueList ) loadAttachments ( e Engine ) ( err error ) {
if len ( issues ) == 0 {
return nil
}
var attachments = make ( map [ int64 ] [ ] * Attachment , len ( issues ) )
2018-08-02 16:49:05 +03:00
var issuesIDs = issues . getIssueIDs ( )
var left = len ( issuesIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
rows , err := e . Table ( "attachment" ) .
Join ( "INNER" , "issue" , "issue.id = attachment.issue_id" ) .
In ( "issue.id" , issuesIDs [ : limit ] ) .
Rows ( new ( Attachment ) )
2017-02-22 17:03:59 +03:00
if err != nil {
return err
}
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var attachment Attachment
err = rows . Scan ( & attachment )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadAttachments: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
attachments [ attachment . IssueID ] = append ( attachments [ attachment . IssueID ] , & attachment )
}
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadAttachments: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
issuesIDs = issuesIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . Attachments = attachments [ issue . ID ]
}
return nil
}
2019-02-19 17:39:39 +03:00
func ( issues IssueList ) loadComments ( e Engine , cond builder . Cond ) ( err error ) {
2017-02-22 17:03:59 +03:00
if len ( issues ) == 0 {
return nil
}
var comments = make ( map [ int64 ] [ ] * Comment , len ( issues ) )
2018-08-02 16:49:05 +03:00
var issuesIDs = issues . getIssueIDs ( )
var left = len ( issuesIDs )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
rows , err := e . Table ( "comment" ) .
Join ( "INNER" , "issue" , "issue.id = comment.issue_id" ) .
In ( "issue.id" , issuesIDs [ : limit ] ) .
2019-02-19 17:39:39 +03:00
Where ( cond ) .
2018-08-02 16:49:05 +03:00
Rows ( new ( Comment ) )
2017-02-22 17:03:59 +03:00
if err != nil {
return err
}
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var comment Comment
err = rows . Scan ( & comment )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadComments: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
comments [ comment . IssueID ] = append ( comments [ comment . IssueID ] , & comment )
}
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadComments: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
issuesIDs = issuesIDs [ limit : ]
2017-02-22 17:03:59 +03:00
}
for _ , issue := range issues {
issue . Comments = comments [ issue . ID ]
}
return nil
}
2018-04-29 08:58:47 +03:00
func ( issues IssueList ) loadTotalTrackedTimes ( e Engine ) ( err error ) {
type totalTimesByIssue struct {
IssueID int64
Time int64
}
if len ( issues ) == 0 {
return nil
}
var trackedTimes = make ( map [ int64 ] int64 , len ( issues ) )
var ids = make ( [ ] int64 , 0 , len ( issues ) )
for _ , issue := range issues {
if issue . Repo . IsTimetrackerEnabled ( ) {
ids = append ( ids , issue . ID )
}
}
2018-08-02 16:49:05 +03:00
var left = len ( ids )
for left > 0 {
var limit = defaultMaxInSize
if left < limit {
limit = left
}
2018-04-29 08:58:47 +03:00
2018-08-02 16:49:05 +03:00
// select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
rows , err := e . Table ( "tracked_time" ) .
2019-12-27 23:30:58 +03:00
Where ( "deleted = ?" , false ) .
2018-08-02 16:49:05 +03:00
Select ( "issue_id, sum(time) as time" ) .
In ( "issue_id" , ids [ : limit ] ) .
GroupBy ( "issue_id" ) .
Rows ( new ( totalTimesByIssue ) )
2018-04-29 08:58:47 +03:00
if err != nil {
return err
}
2018-08-02 16:49:05 +03:00
for rows . Next ( ) {
var totalTime totalTimesByIssue
err = rows . Scan ( & totalTime )
if err != nil {
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadTotalTrackedTimes: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
2018-08-02 16:49:05 +03:00
return err
}
trackedTimes [ totalTime . IssueID ] = totalTime . Time
}
2019-06-23 18:22:43 +03:00
if err1 := rows . Close ( ) ; err1 != nil {
return fmt . Errorf ( "IssueList.loadTotalTrackedTimes: Close: %v" , err1 )
2019-06-12 22:41:28 +03:00
}
left -= limit
2018-08-02 16:49:05 +03:00
ids = ids [ limit : ]
2018-04-29 08:58:47 +03:00
}
for _ , issue := range issues {
issue . TotalTrackedTime = trackedTimes [ issue . ID ]
}
return nil
}
2017-11-21 08:28:22 +03:00
// loadAttributes loads all attributes, expect for attachments and comments
2019-06-12 22:41:28 +03:00
func ( issues IssueList ) loadAttributes ( e Engine ) error {
if _ , err := issues . loadRepositories ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadRepositories: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadPosters ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadPosters: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadLabels ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadLabels: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadMilestones ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadMilestones: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadAssignees ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadAssignees: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadPullRequests ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadPullRequests: %v" , err )
2017-02-22 17:03:59 +03:00
}
2019-06-12 22:41:28 +03:00
if err := issues . loadTotalTrackedTimes ( e ) ; err != nil {
return fmt . Errorf ( "issue.loadAttributes: loadTotalTrackedTimes: %v" , err )
2018-04-29 08:58:47 +03:00
}
2017-02-22 17:03:59 +03:00
return nil
}
2017-11-21 08:28:22 +03:00
// LoadAttributes loads attributes of the issues, except for attachments and
// comments
2017-02-22 17:03:59 +03:00
func ( issues IssueList ) LoadAttributes ( ) error {
return issues . loadAttributes ( x )
}
2017-11-21 08:28:22 +03:00
// LoadAttachments loads attachments
func ( issues IssueList ) LoadAttachments ( ) error {
return issues . loadAttachments ( x )
}
// LoadComments loads comments
func ( issues IssueList ) LoadComments ( ) error {
2019-02-19 17:39:39 +03:00
return issues . loadComments ( x , builder . NewCond ( ) )
}
// LoadDiscussComments loads discuss comments
func ( issues IssueList ) LoadDiscussComments ( ) error {
return issues . loadComments ( x , builder . Eq { "comment.type" : CommentTypeComment } )
2017-11-21 08:28:22 +03:00
}
2020-03-06 06:44:06 +03:00
// GetApprovalCounts returns a map of issue ID to slice of approval counts
// FIXME: only returns official counts due to double counting of non-official approvals
func ( issues IssueList ) GetApprovalCounts ( ) ( map [ int64 ] [ ] * ReviewCount , error ) {
return issues . getApprovalCounts ( x )
}
func ( issues IssueList ) getApprovalCounts ( e Engine ) ( map [ int64 ] [ ] * ReviewCount , error ) {
2020-04-16 13:44:34 +03:00
rCounts := make ( [ ] * ReviewCount , 0 , 2 * len ( issues ) )
2020-03-06 06:44:06 +03:00
ids := make ( [ ] int64 , len ( issues ) )
for i , issue := range issues {
ids [ i ] = issue . ID
}
sess := e . In ( "issue_id" , ids )
2020-04-16 13:44:34 +03:00
err := sess . Select ( "issue_id, type, count(id) as `count`" ) .
Where ( "official = ?" , true ) .
GroupBy ( "issue_id, type" ) .
OrderBy ( "issue_id" ) .
Table ( "review" ) .
Find ( & rCounts )
2020-03-06 06:44:06 +03:00
if err != nil {
return nil , err
}
approvalCountMap := make ( map [ int64 ] [ ] * ReviewCount , len ( issues ) )
2020-04-16 13:44:34 +03:00
for _ , c := range rCounts {
approvalCountMap [ c . IssueID ] = append ( approvalCountMap [ c . IssueID ] , c )
2020-03-06 06:44:06 +03:00
}
2020-04-16 13:44:34 +03:00
2020-03-06 06:44:06 +03:00
return approvalCountMap , nil
}