Skip to content

Commit

Permalink
Fix performance regression when user has many organization (go-gitea#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Dec 30, 2021
1 parent e0b41f8 commit 626b107
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,33 +294,39 @@ func CanUserForkRepo(user *user_model.User, repo *repo_model.Repository) (bool,
return false, nil
}

// FindUserOrgForks returns the forked repositories for one user from a repository
func FindUserOrgForks(repoID, userID int64) ([]*repo_model.Repository, error) {
var cond builder.Cond = builder.And(
builder.Eq{"fork_id": repoID},
builder.In("owner_id",
builder.Select("org_id").
From("org_user").
Where(builder.Eq{"uid": userID}),
),
)

var repos []*repo_model.Repository
return repos, db.GetEngine(db.DefaultContext).Table("repository").Where(cond).Find(&repos)
}

// GetForksByUserAndOrgs return forked repos of the user and owned orgs
func GetForksByUserAndOrgs(user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) {
var repoList []*repo_model.Repository
if user == nil {
return repoList, nil
}
var forkedRepo *repo_model.Repository
forkedRepo, err := repo_model.GetUserFork(repo.ID, user.ID)
if err != nil {
return repoList, err
}
if forkedRepo != nil {
repoList = append(repoList, forkedRepo)
}
canCreateRepos, err := GetOrgsCanCreateRepoByUserID(user.ID)
orgForks, err := FindUserOrgForks(repo.ID, user.ID)
if err != nil {
return repoList, err
}
for _, org := range canCreateRepos {
forkedRepo, err := repo_model.GetUserFork(repo.ID, org.ID)
if err != nil {
return repoList, err
}
if forkedRepo != nil {
repoList = append(repoList, forkedRepo)
}
return nil, err
}
repoList = append(repoList, orgForks...)
return repoList, nil
}

Expand Down

0 comments on commit 626b107

Please sign in to comment.