[Refactor] convert team(s) to apiTeam(s) (#13745)

* Refactor: teams to api convert

* make org load optional

* more info in tests
This commit is contained in:
6543 2022-05-13 19:27:58 +02:00 committed by GitHub
parent 61f939359d
commit b135313c47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 110 additions and 98 deletions

View file

@ -304,22 +304,53 @@ func ToOrganization(org *organization.Organization) *api.Organization {
}
}
// ToTeam convert organization.Team to api.Team
func ToTeam(team *organization.Team) *api.Team {
if team == nil {
return nil
// ToTeam convert models.Team to api.Team
func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
teams, err := ToTeams([]*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
if err != nil || len(teams) == 0 {
return nil, err
}
return teams[0], nil
}
// ToTeams convert models.Team list to api.Team list
func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
if len(teams) == 0 || teams[0] == nil {
return nil, nil
}
return &api.Team{
ID: team.ID,
Name: team.Name,
Description: team.Description,
IncludesAllRepositories: team.IncludesAllRepositories,
CanCreateOrgRepo: team.CanCreateOrgRepo,
Permission: team.AccessMode.String(),
Units: team.GetUnitNames(),
UnitsMap: team.GetUnitsMap(),
cache := make(map[int64]*api.Organization)
apiTeams := make([]*api.Team, len(teams))
for i := range teams {
if err := teams[i].GetUnits(); err != nil {
return nil, err
}
apiTeams[i] = &api.Team{
ID: teams[i].ID,
Name: teams[i].Name,
Description: teams[i].Description,
IncludesAllRepositories: teams[i].IncludesAllRepositories,
CanCreateOrgRepo: teams[i].CanCreateOrgRepo,
Permission: teams[i].AccessMode.String(),
Units: teams[i].GetUnitNames(),
UnitsMap: teams[i].GetUnitsMap(),
}
if loadOrgs {
apiOrg, ok := cache[teams[i].OrgID]
if !ok {
org, err := organization.GetOrgByID(teams[i].OrgID)
if err != nil {
return nil, err
}
apiOrg = ToOrganization(org)
cache[teams[i].OrgID] = apiOrg
}
apiTeams[i].Organization = apiOrg
}
}
return apiTeams, nil
}
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag