@@ -1151,8 +1151,7 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1151
1151
}
1152
1152
1153
1153
if len (opts .RepoIDs ) > 0 {
1154
- // In case repository IDs are provided but actually no repository has issue.
1155
- sess .In ("issue.repo_id" , opts .RepoIDs )
1154
+ applyReposCondition (sess , opts .RepoIDs )
1156
1155
}
1157
1156
1158
1157
switch opts .IsClosed {
@@ -1163,26 +1162,19 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1163
1162
}
1164
1163
1165
1164
if opts .AssigneeID > 0 {
1166
- sess .Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1167
- And ("issue_assignees.assignee_id = ?" , opts .AssigneeID )
1165
+ applyAssigneeCondition (sess , opts .AssigneeID )
1168
1166
}
1169
1167
1170
1168
if opts .PosterID > 0 {
1171
- sess . And ( "issue.poster_id=?" , opts .PosterID )
1169
+ applyPosterCondition ( sess , opts .PosterID )
1172
1170
}
1173
1171
1174
1172
if opts .MentionedID > 0 {
1175
- sess .Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id" ).
1176
- And ("issue_user.is_mentioned = ?" , true ).
1177
- And ("issue_user.uid = ?" , opts .MentionedID )
1173
+ applyMentionedCondition (sess , opts .MentionedID )
1178
1174
}
1179
1175
1180
1176
if opts .ReviewRequestedID > 0 {
1181
- sess .Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1182
- And ("r.type = ?" , ReviewTypeRequest ).
1183
- And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1184
- " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1185
- opts .ReviewRequestedID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , opts .ReviewRequestedID )
1177
+ applyReviewRequestedCondition (sess , opts .ReviewRequestedID )
1186
1178
}
1187
1179
1188
1180
if len (opts .MilestoneIDs ) > 0 {
@@ -1236,6 +1228,33 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1236
1228
}
1237
1229
}
1238
1230
1231
+ func applyReposCondition (sess * xorm.Session , repoIDs []int64 ) * xorm.Session {
1232
+ return sess .In ("issue.repo_id" , repoIDs )
1233
+ }
1234
+
1235
+ func applyAssigneeCondition (sess * xorm.Session , assigneeID int64 ) * xorm.Session {
1236
+ return sess .Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1237
+ And ("issue_assignees.assignee_id = ?" , assigneeID )
1238
+ }
1239
+
1240
+ func applyPosterCondition (sess * xorm.Session , posterID int64 ) * xorm.Session {
1241
+ return sess .And ("issue.poster_id=?" , posterID )
1242
+ }
1243
+
1244
+ func applyMentionedCondition (sess * xorm.Session , mentionedID int64 ) * xorm.Session {
1245
+ return sess .Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id" ).
1246
+ And ("issue_user.is_mentioned = ?" , true ).
1247
+ And ("issue_user.uid = ?" , mentionedID )
1248
+ }
1249
+
1250
+ func applyReviewRequestedCondition (sess * xorm.Session , reviewRequestedID int64 ) * xorm.Session {
1251
+ return sess .Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1252
+ And ("r.type = ?" , ReviewTypeRequest ).
1253
+ And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1254
+ " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1255
+ reviewRequestedID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , reviewRequestedID )
1256
+ }
1257
+
1239
1258
// CountIssuesByRepo map from repoID to number of issues matching the options
1240
1259
func CountIssuesByRepo (opts * IssuesOptions ) (map [int64 ]int64 , error ) {
1241
1260
sess := x .NewSession ()
@@ -1468,26 +1487,19 @@ func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats,
1468
1487
}
1469
1488
1470
1489
if opts .AssigneeID > 0 {
1471
- sess .Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1472
- And ("issue_assignees.assignee_id = ?" , opts .AssigneeID )
1490
+ applyAssigneeCondition (sess , opts .AssigneeID )
1473
1491
}
1474
1492
1475
1493
if opts .PosterID > 0 {
1476
- sess . And ( "issue.poster_id = ?" , opts .PosterID )
1494
+ applyPosterCondition ( sess , opts .PosterID )
1477
1495
}
1478
1496
1479
1497
if opts .MentionedID > 0 {
1480
- sess .Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id" ).
1481
- And ("issue_user.uid = ?" , opts .MentionedID ).
1482
- And ("issue_user.is_mentioned = ?" , true )
1498
+ applyMentionedCondition (sess , opts .MentionedID )
1483
1499
}
1484
1500
1485
1501
if opts .ReviewRequestedID > 0 {
1486
- sess .Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1487
- And ("r.type = ?" , ReviewTypeRequest ).
1488
- And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1489
- " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1490
- opts .ReviewRequestedID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , opts .ReviewRequestedID )
1502
+ applyReviewRequestedCondition (sess , opts .ReviewRequestedID )
1491
1503
}
1492
1504
1493
1505
switch opts .IsPull {
@@ -1550,122 +1562,94 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
1550
1562
1551
1563
switch opts .FilterMode {
1552
1564
case FilterModeAll :
1553
- stats .OpenCount , err = sess (cond ). And ( "issue.is_closed = ?" , false ).
1554
- And (builder . In ( "issue.repo_id " , opts . UserRepoIDs ) ).
1565
+ stats .OpenCount , err = applyReposCondition ( sess (cond ), opts . UserRepoIDs ).
1566
+ And ("issue.is_closed = ? " , false ).
1555
1567
Count (new (Issue ))
1556
1568
if err != nil {
1557
1569
return nil , err
1558
1570
}
1559
- stats .ClosedCount , err = sess (cond ). And ( "issue.is_closed = ?" , true ).
1560
- And (builder . In ( "issue.repo_id " , opts . UserRepoIDs ) ).
1571
+ stats .ClosedCount , err = applyReposCondition ( sess (cond ), opts . UserRepoIDs ).
1572
+ And ("issue.is_closed = ? " , true ).
1561
1573
Count (new (Issue ))
1562
1574
if err != nil {
1563
1575
return nil , err
1564
1576
}
1565
1577
case FilterModeAssign :
1566
- stats .OpenCount , err = sess (cond ).And ("issue.is_closed = ?" , false ).
1567
- Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1568
- And ("issue_assignees.assignee_id = ?" , opts .UserID ).
1578
+ stats .OpenCount , err = applyAssigneeCondition (sess (cond ), opts .UserID ).
1579
+ And ("issue.is_closed = ?" , false ).
1569
1580
Count (new (Issue ))
1570
1581
if err != nil {
1571
1582
return nil , err
1572
1583
}
1573
- stats .ClosedCount , err = sess (cond ).And ("issue.is_closed = ?" , true ).
1574
- Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1575
- And ("issue_assignees.assignee_id = ?" , opts .UserID ).
1584
+ stats .ClosedCount , err = applyAssigneeCondition (sess (cond ), opts .UserID ).
1585
+ And ("issue.is_closed = ?" , true ).
1576
1586
Count (new (Issue ))
1577
1587
if err != nil {
1578
1588
return nil , err
1579
1589
}
1580
1590
case FilterModeCreate :
1581
- stats .OpenCount , err = sess (cond ). And ( "issue.is_closed = ?" , false ).
1582
- And ("issue.poster_id = ?" , opts . UserID ).
1591
+ stats .OpenCount , err = applyPosterCondition ( sess (cond ), opts . UserID ).
1592
+ And ("issue.is_closed = ?" , false ).
1583
1593
Count (new (Issue ))
1584
1594
if err != nil {
1585
1595
return nil , err
1586
1596
}
1587
- stats .ClosedCount , err = sess (cond ). And ( "issue.is_closed = ?" , true ).
1588
- And ("issue.poster_id = ?" , opts . UserID ).
1597
+ stats .ClosedCount , err = applyPosterCondition ( sess (cond ), opts . UserID ).
1598
+ And ("issue.is_closed = ?" , true ).
1589
1599
Count (new (Issue ))
1590
1600
if err != nil {
1591
1601
return nil , err
1592
1602
}
1593
1603
case FilterModeMention :
1594
- stats .OpenCount , err = sess (cond ).And ("issue.is_closed = ?" , false ).
1595
- Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?" , true ).
1596
- And ("issue_user.uid = ?" , opts .UserID ).
1604
+ stats .OpenCount , err = applyMentionedCondition (sess (cond ), opts .UserID ).
1605
+ And ("issue.is_closed = ?" , false ).
1597
1606
Count (new (Issue ))
1598
1607
if err != nil {
1599
1608
return nil , err
1600
1609
}
1601
- stats .ClosedCount , err = sess (cond ).And ("issue.is_closed = ?" , true ).
1602
- Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?" , true ).
1603
- And ("issue_user.uid = ?" , opts .UserID ).
1610
+ stats .ClosedCount , err = applyMentionedCondition (sess (cond ), opts .UserID ).
1611
+ And ("issue.is_closed = ?" , true ).
1604
1612
Count (new (Issue ))
1605
1613
if err != nil {
1606
1614
return nil , err
1607
1615
}
1608
1616
case FilterModeReviewRequested :
1609
- stats .OpenCount , err = x .Where (cond ).And ("issue.is_closed = ?" , false ).
1610
- Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1611
- And ("r.type = ?" , ReviewTypeRequest ).
1612
- And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1613
- " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1614
- opts .UserID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , opts .UserID ).
1617
+ stats .OpenCount , err = applyReviewRequestedCondition (sess (cond ), opts .UserID ).
1618
+ And ("issue.is_closed = ?" , false ).
1615
1619
Count (new (Issue ))
1616
1620
if err != nil {
1617
1621
return nil , err
1618
1622
}
1619
- stats .ClosedCount , err = x .Where (cond ).And ("issue.is_closed = ?" , true ).
1620
- Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1621
- And ("r.type = ?" , ReviewTypeRequest ).
1622
- And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1623
- " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1624
- opts .UserID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , opts .UserID ).
1623
+ stats .ClosedCount , err = applyReviewRequestedCondition (sess (cond ), opts .UserID ).
1624
+ And ("issue.is_closed = ?" , true ).
1625
1625
Count (new (Issue ))
1626
1626
if err != nil {
1627
1627
return nil , err
1628
1628
}
1629
1629
}
1630
1630
1631
1631
cond = cond .And (builder.Eq {"issue.is_closed" : opts .IsClosed })
1632
- stats .AssignCount , err = sess (cond ).
1633
- Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1634
- And ("issue_assignees.assignee_id = ?" , opts .UserID ).
1635
- Count (new (Issue ))
1632
+ stats .AssignCount , err = applyAssigneeCondition (sess (cond ), opts .UserID ).Count (new (Issue ))
1636
1633
if err != nil {
1637
1634
return nil , err
1638
1635
}
1639
1636
1640
- stats .CreateCount , err = sess (cond ).
1641
- And ("poster_id = ?" , opts .UserID ).
1642
- Count (new (Issue ))
1637
+ stats .CreateCount , err = applyPosterCondition (sess (cond ), opts .UserID ).Count (new (Issue ))
1643
1638
if err != nil {
1644
1639
return nil , err
1645
1640
}
1646
1641
1647
- stats .MentionCount , err = sess (cond ).
1648
- Join ("INNER" , "issue_user" , "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?" , true ).
1649
- And ("issue_user.uid = ?" , opts .UserID ).
1650
- Count (new (Issue ))
1642
+ stats .MentionCount , err = applyMentionedCondition (sess (cond ), opts .UserID ).Count (new (Issue ))
1651
1643
if err != nil {
1652
1644
return nil , err
1653
1645
}
1654
1646
1655
- stats .YourRepositoriesCount , err = sess (cond ).
1656
- And (builder .In ("issue.repo_id" , opts .UserRepoIDs )).
1657
- Count (new (Issue ))
1647
+ stats .YourRepositoriesCount , err = applyReposCondition (sess (cond ), opts .UserRepoIDs ).Count (new (Issue ))
1658
1648
if err != nil {
1659
1649
return nil , err
1660
1650
}
1661
1651
1662
- stats .ReviewRequestedCount , err = x .Where (cond ).
1663
- Join ("INNER" , []string {"review" , "r" }, "issue.id = r.issue_id" ).
1664
- And ("r.type = ?" , ReviewTypeRequest ).
1665
- And ("r.reviewer_id = ? and r.id in (select max(id) from review where issue_id = r.issue_id and reviewer_id = r.reviewer_id and type in (?, ?, ?))" +
1666
- " or r.reviewer_team_id in (select team_id from team_user where uid = ?)" ,
1667
- opts .UserID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , opts .UserID ).
1668
- Count (new (Issue ))
1652
+ stats .ReviewRequestedCount , err = applyReviewRequestedCondition (sess (cond ), opts .UserID ).Count (new (Issue ))
1669
1653
if err != nil {
1670
1654
return nil , err
1671
1655
}
@@ -1689,13 +1673,11 @@ func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen
1689
1673
1690
1674
switch filterMode {
1691
1675
case FilterModeAssign :
1692
- openCountSession .Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1693
- And ("issue_assignees.assignee_id = ?" , uid )
1694
- closedCountSession .Join ("INNER" , "issue_assignees" , "issue.id = issue_assignees.issue_id" ).
1695
- And ("issue_assignees.assignee_id = ?" , uid )
1676
+ applyAssigneeCondition (openCountSession , uid )
1677
+ applyAssigneeCondition (closedCountSession , uid )
1696
1678
case FilterModeCreate :
1697
- openCountSession . And ( "poster_id = ?" , uid )
1698
- closedCountSession . And ( "poster_id = ?" , uid )
1679
+ applyPosterCondition ( openCountSession , uid )
1680
+ applyPosterCondition ( closedCountSession , uid )
1699
1681
}
1700
1682
1701
1683
openResult , _ := openCountSession .Count (new (Issue ))
0 commit comments