Skip to content

Commit ab81e44

Browse files
authored
Revert "[Backport] Fix issues/pr list broken when there are many repositories (#8409) (#8418)"
This reverts commit 0ea4b78.
1 parent 0ea4b78 commit ab81e44

File tree

5 files changed

+178
-110
lines changed

5 files changed

+178
-110
lines changed

models/issue.go

+22-32
Original file line numberDiff line numberDiff line change
@@ -1291,19 +1291,18 @@ func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
12911291

12921292
// IssuesOptions represents options of an issue.
12931293
type IssuesOptions struct {
1294-
RepoIDs []int64 // include all repos if empty
1295-
RepoSubQuery *builder.Builder
1296-
AssigneeID int64
1297-
PosterID int64
1298-
MentionedID int64
1299-
MilestoneID int64
1300-
Page int
1301-
PageSize int
1302-
IsClosed util.OptionalBool
1303-
IsPull util.OptionalBool
1304-
LabelIDs []int64
1305-
SortType string
1306-
IssueIDs []int64
1294+
RepoIDs []int64 // include all repos if empty
1295+
AssigneeID int64
1296+
PosterID int64
1297+
MentionedID int64
1298+
MilestoneID int64
1299+
Page int
1300+
PageSize int
1301+
IsClosed util.OptionalBool
1302+
IsPull util.OptionalBool
1303+
LabelIDs []int64
1304+
SortType string
1305+
IssueIDs []int64
13071306
}
13081307

13091308
// sortIssuesSession sort an issues-related session based on the provided
@@ -1346,9 +1345,7 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
13461345
sess.In("issue.id", opts.IssueIDs)
13471346
}
13481347

1349-
if opts.RepoSubQuery != nil {
1350-
sess.In("issue.repo_id", opts.RepoSubQuery)
1351-
} else if len(opts.RepoIDs) > 0 {
1348+
if len(opts.RepoIDs) > 0 {
13521349
// In case repository IDs are provided but actually no repository has issue.
13531350
sess.In("issue.repo_id", opts.RepoIDs)
13541351
}
@@ -1615,12 +1612,12 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
16151612

16161613
// UserIssueStatsOptions contains parameters accepted by GetUserIssueStats.
16171614
type UserIssueStatsOptions struct {
1618-
UserID int64
1619-
RepoID int64
1620-
RepoSubQuery *builder.Builder
1621-
FilterMode int
1622-
IsPull bool
1623-
IsClosed bool
1615+
UserID int64
1616+
RepoID int64
1617+
UserRepoIDs []int64
1618+
FilterMode int
1619+
IsPull bool
1620+
IsClosed bool
16241621
}
16251622

16261623
// GetUserIssueStats returns issue statistic information for dashboard by given conditions.
@@ -1634,23 +1631,16 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
16341631
cond = cond.And(builder.Eq{"issue.repo_id": opts.RepoID})
16351632
}
16361633

1637-
var repoCond = builder.NewCond()
1638-
if opts.RepoSubQuery != nil {
1639-
repoCond = builder.In("issue.repo_id", opts.RepoSubQuery)
1640-
} else {
1641-
repoCond = builder.Expr("0=1")
1642-
}
1643-
16441634
switch opts.FilterMode {
16451635
case FilterModeAll:
16461636
stats.OpenCount, err = x.Where(cond).And("is_closed = ?", false).
1647-
And(repoCond).
1637+
And(builder.In("issue.repo_id", opts.UserRepoIDs)).
16481638
Count(new(Issue))
16491639
if err != nil {
16501640
return nil, err
16511641
}
16521642
stats.ClosedCount, err = x.Where(cond).And("is_closed = ?", true).
1653-
And(repoCond).
1643+
And(builder.In("issue.repo_id", opts.UserRepoIDs)).
16541644
Count(new(Issue))
16551645
if err != nil {
16561646
return nil, err
@@ -1702,7 +1692,7 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
17021692
}
17031693

17041694
stats.YourRepositoriesCount, err = x.Where(cond).
1705-
And(repoCond).
1695+
And(builder.In("issue.repo_id", opts.UserRepoIDs)).
17061696
Count(new(Issue))
17071697
if err != nil {
17081698
return nil, err

models/issue_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/stretchr/testify/assert"
13-
"xorm.io/builder"
1413
)
1514

1615
func TestIssue_ReplaceLabels(t *testing.T) {
@@ -267,12 +266,10 @@ func TestGetUserIssueStats(t *testing.T) {
267266
},
268267
{
269268
UserIssueStatsOptions{
270-
UserID: 2,
271-
RepoSubQuery: builder.Select("repository.id").
272-
From("repository").
273-
Where(builder.In("repository.id", []int64{1, 2})),
274-
FilterMode: FilterModeAll,
275-
IsClosed: true,
269+
UserID: 2,
270+
UserRepoIDs: []int64{1, 2},
271+
FilterMode: FilterModeAll,
272+
IsClosed: true,
276273
},
277274
IssueStats{
278275
YourRepositoriesCount: 2,

models/user.go

+38-23
Original file line numberDiff line numberDiff line change
@@ -561,35 +561,50 @@ func (u *User) GetRepositories(page, pageSize int) (err error) {
561561
return err
562562
}
563563

564-
// UnitRepositoriesSubQuery returns repositories query builder according units
565-
func (u *User) UnitRepositoriesSubQuery(units ...UnitType) *builder.Builder {
566-
b := builder.Select("repository.id").From("repository")
564+
// GetRepositoryIDs returns repositories IDs where user owned and has unittypes
565+
func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) {
566+
var ids []int64
567+
568+
sess := x.Table("repository").Cols("repository.id")
567569

568570
if len(units) > 0 {
569-
b.Join("INNER", "repo_unit", builder.Expr("repository.id = repo_unit.repo_id").
570-
And(builder.In("repo_unit.type", units)),
571-
)
571+
sess = sess.Join("INNER", "repo_unit", "repository.id = repo_unit.repo_id")
572+
sess = sess.In("repo_unit.type", units)
572573
}
573-
return b.Where(builder.Eq{"repository.owner_id": u.ID})
574+
575+
return ids, sess.Where("owner_id = ?", u.ID).Find(&ids)
574576
}
575577

576-
// OrgUnitRepositoriesSubQuery returns repositories query builder according orgnizations and units
577-
func (u *User) OrgUnitRepositoriesSubQuery(userID int64, units ...UnitType) *builder.Builder {
578-
b := builder.
579-
Select("team_repo.repo_id").
580-
From("team_repo").
581-
Join("INNER", "team_user", builder.Eq{"team_user.uid": userID}.And(
582-
builder.Expr("team_user.team_id = team_repo.team_id"),
583-
))
578+
// GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes
579+
func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
580+
var ids []int64
581+
582+
sess := x.Table("repository").
583+
Cols("repository.id").
584+
Join("INNER", "team_user", "repository.owner_id = team_user.org_id").
585+
Join("INNER", "team_repo", "repository.is_private != ? OR (team_user.team_id = team_repo.team_id AND repository.id = team_repo.repo_id)", true)
586+
584587
if len(units) > 0 {
585-
b.Join("INNER", "team_unit", builder.Eq{"team_unit.org_id": u.ID}.And(
586-
builder.Expr("team_unit.team_id = team_repo.team_id").And(
587-
builder.In("`type`", units),
588-
),
589-
))
590-
}
591-
return b.Where(builder.Eq{"team_repo.org_id": u.ID}).
592-
GroupBy("team_repo.repo_id")
588+
sess = sess.Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id")
589+
sess = sess.In("team_unit.type", units)
590+
}
591+
592+
return ids, sess.
593+
Where("team_user.uid = ?", u.ID).
594+
GroupBy("repository.id").Find(&ids)
595+
}
596+
597+
// GetAccessRepoIDs returns all repositories IDs where user's or user is a team member organizations
598+
func (u *User) GetAccessRepoIDs(units ...UnitType) ([]int64, error) {
599+
ids, err := u.GetRepositoryIDs(units...)
600+
if err != nil {
601+
return nil, err
602+
}
603+
ids2, err := u.GetOrgRepositoryIDs(units...)
604+
if err != nil {
605+
return nil, err
606+
}
607+
return append(ids, ids2...), nil
593608
}
594609

595610
// GetMirrorRepositories returns mirror repositories that user owns, including private repositories.

models/user_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ func BenchmarkHashPassword(b *testing.B) {
178178
}
179179
}
180180

181+
func TestGetOrgRepositoryIDs(t *testing.T) {
182+
assert.NoError(t, PrepareTestDatabase())
183+
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
184+
user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
185+
user5 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
186+
187+
accessibleRepos, err := user2.GetOrgRepositoryIDs()
188+
assert.NoError(t, err)
189+
// User 2's team has access to private repos 3, 5, repo 32 is a public repo of the organization
190+
assert.Equal(t, []int64{3, 5, 23, 24, 32}, accessibleRepos)
191+
192+
accessibleRepos, err = user4.GetOrgRepositoryIDs()
193+
assert.NoError(t, err)
194+
// User 4's team has access to private repo 3, repo 32 is a public repo of the organization
195+
assert.Equal(t, []int64{3, 32}, accessibleRepos)
196+
197+
accessibleRepos, err = user5.GetOrgRepositoryIDs()
198+
assert.NoError(t, err)
199+
// User 5's team has no access to any repo
200+
assert.Len(t, accessibleRepos, 0)
201+
}
202+
181203
func TestNewGitSig(t *testing.T) {
182204
users := make([]*User, 0, 20)
183205
sess := x.NewSession()

0 commit comments

Comments
 (0)