Skip to content

Commit 799d0c2

Browse files
authored
slight optimization for GetUserRepositories (#498)
1 parent 22e1bd3 commit 799d0c2

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

models/org.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,24 +536,28 @@ func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
536536
// that the user with the given userID has access to,
537537
// and total number of records based on given condition.
538538
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
539+
var cond builder.Cond = builder.Eq{
540+
"`repository`.owner_id": org.ID,
541+
"`repository`.is_private": false,
542+
}
543+
539544
teamIDs, err := org.GetUserTeamIDs(userID)
540545
if err != nil {
541546
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
542547
}
543-
if len(teamIDs) == 0 {
544-
// user has no team but "IN ()" is invalid SQL
545-
teamIDs = []int64{-1} // there is no repo with id=-1
548+
549+
if len(teamIDs) > 0 {
550+
cond = cond.Or(builder.In("team_repo.team_id", teamIDs))
546551
}
547552

548553
if page <= 0 {
549554
page = 1
550555
}
551556
repos := make([]*Repository, 0, pageSize)
557+
552558
if err := x.
553-
Select("`repository`.*").
554559
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
555-
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
556-
Or(builder.In("team_repo.team_id", teamIDs)).
560+
Where(cond).
557561
GroupBy("`repository`.id").
558562
OrderBy("updated_unix DESC").
559563
Limit(pageSize, (page-1)*pageSize).
@@ -563,8 +567,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
563567

564568
repoCount, err := x.
565569
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
566-
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
567-
Or(builder.In("team_repo.team_id", teamIDs)).
570+
Where(cond).
568571
GroupBy("`repository`.id").
569572
Count(&Repository{})
570573
if err != nil {

0 commit comments

Comments
 (0)