[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:
parent
61f939359d
commit
b135313c47
9 changed files with 110 additions and 98 deletions
|
@ -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
|
||||
|
|
|
@ -152,7 +152,7 @@ func ToTimelineComment(c *models.Comment, doer *user_model.User) *api.TimelineCo
|
|||
comment.Assignee = ToUser(c.Assignee, nil)
|
||||
}
|
||||
if c.AssigneeTeam != nil {
|
||||
comment.AssigneeTeam = ToTeam(c.AssigneeTeam)
|
||||
comment.AssigneeTeam, _ = ToTeam(c.AssigneeTeam)
|
||||
}
|
||||
|
||||
if c.ResolveDoer != nil {
|
||||
|
|
|
@ -22,10 +22,15 @@ func ToPullReview(ctx context.Context, r *models.Review, doer *user_model.User)
|
|||
r.Reviewer = user_model.NewGhostUser()
|
||||
}
|
||||
|
||||
apiTeam, err := ToTeam(r.ReviewerTeam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &api.PullReview{
|
||||
ID: r.ID,
|
||||
Reviewer: ToUser(r.Reviewer, doer),
|
||||
ReviewerTeam: ToTeam(r.ReviewerTeam),
|
||||
ReviewerTeam: apiTeam,
|
||||
State: api.ReviewStateUnknown,
|
||||
Body: r.Content,
|
||||
CommitID: r.CommitID,
|
||||
|
|
|
@ -186,10 +186,7 @@ func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent boo
|
|||
|
||||
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
|
||||
func ToRepoTransfer(t *models.RepoTransfer) *api.RepoTransfer {
|
||||
var teams []*api.Team
|
||||
for _, v := range t.Teams {
|
||||
teams = append(teams, ToTeam(v))
|
||||
}
|
||||
teams, _ := ToTeams(t.Teams, false)
|
||||
|
||||
return &api.RepoTransfer{
|
||||
Doer: ToUser(t.Doer, nil),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue