mirror of
https://github.com/go-gitea/gitea
synced 2024-12-29 00:14:34 +01:00
Introduce OrgList and add LoadTeams, optimaze Load teams for orgs (#32543)
This commit is contained in:
parent
b6ce2d6dc9
commit
f49d82309b
4 changed files with 47 additions and 5 deletions
|
@ -16,6 +16,31 @@ import (
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type OrgList []*Organization
|
||||||
|
|
||||||
|
func (orgs OrgList) LoadTeams(ctx context.Context) (map[int64]TeamList, error) {
|
||||||
|
if len(orgs) == 0 {
|
||||||
|
return map[int64]TeamList{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
orgIDs := make([]int64, len(orgs))
|
||||||
|
for i, org := range orgs {
|
||||||
|
orgIDs[i] = org.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
teams, err := GetTeamsByOrgIDs(ctx, orgIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
teamMap := make(map[int64]TeamList, len(orgs))
|
||||||
|
for _, team := range teams {
|
||||||
|
teamMap[team.OrgID] = append(teamMap[team.OrgID], team)
|
||||||
|
}
|
||||||
|
|
||||||
|
return teamMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SearchOrganizationsOptions options to filter organizations
|
// SearchOrganizationsOptions options to filter organizations
|
||||||
type SearchOrganizationsOptions struct {
|
type SearchOrganizationsOptions struct {
|
||||||
db.ListOptions
|
db.ListOptions
|
||||||
|
|
|
@ -60,3 +60,14 @@ func TestGetUserOrgsList(t *testing.T) {
|
||||||
assert.EqualValues(t, 2, orgs[0].NumRepos)
|
assert.EqualValues(t, 2, orgs[0].NumRepos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadOrgListTeams(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
orgs, err := organization.GetUserOrgsList(db.DefaultContext, &user_model.User{ID: 4})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, orgs, 1)
|
||||||
|
teamsMap, err := organization.OrgList(orgs).LoadTeams(db.DefaultContext)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, teamsMap, 1)
|
||||||
|
assert.Len(t, teamsMap[3], 5)
|
||||||
|
}
|
||||||
|
|
|
@ -126,3 +126,8 @@ func GetUserRepoTeams(ctx context.Context, orgID, userID, repoID int64) (teams T
|
||||||
And("team_repo.repo_id=?", repoID).
|
And("team_repo.repo_id=?", repoID).
|
||||||
Find(&teams)
|
Find(&teams)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {
|
||||||
|
teams := make([]*Team, 0, 10)
|
||||||
|
return teams, db.GetEngine(ctx).Where(builder.In("org_id", orgIDs)).Find(&teams)
|
||||||
|
}
|
||||||
|
|
|
@ -241,14 +241,15 @@ func GetOAuthGroupsForUser(ctx context.Context, user *user_model.User, onlyPubli
|
||||||
return nil, fmt.Errorf("GetUserOrgList: %w", err)
|
return nil, fmt.Errorf("GetUserOrgList: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
orgTeams, err := org_model.OrgList(orgs).LoadTeams(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("LoadTeams: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
var groups []string
|
var groups []string
|
||||||
for _, org := range orgs {
|
for _, org := range orgs {
|
||||||
groups = append(groups, org.Name)
|
groups = append(groups, org.Name)
|
||||||
teams, err := org.LoadTeams(ctx)
|
for _, team := range orgTeams[org.ID] {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("LoadTeams: %w", err)
|
|
||||||
}
|
|
||||||
for _, team := range teams {
|
|
||||||
if team.IsMember(ctx, user.ID) {
|
if team.IsMember(ctx, user.ID) {
|
||||||
groups = append(groups, org.Name+":"+team.LowerName)
|
groups = append(groups, org.Name+":"+team.LowerName)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue