diff options
author | 6543 <6543@obermui.de> | 2022-05-13 19:27:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-13 19:27:58 +0200 |
commit | b135313c478a241ce994d19e685f2a39d066fb92 (patch) | |
tree | edb831054b034ba01f35afbe40ec5b1ced351a4e /modules | |
parent | 61f939359d0b5b478962d7afdc053af23836461f (diff) | |
download | gitea-b135313c478a241ce994d19e685f2a39d066fb92.tar.gz gitea-b135313c478a241ce994d19e685f2a39d066fb92.zip |
[Refactor] convert team(s) to apiTeam(s) (#13745)
* Refactor: teams to api convert
* make org load optional
* more info in tests
Diffstat (limited to 'modules')
-rw-r--r-- | modules/convert/convert.go | 57 | ||||
-rw-r--r-- | modules/convert/issue_comment.go | 2 | ||||
-rw-r--r-- | modules/convert/pull_review.go | 7 | ||||
-rw-r--r-- | modules/convert/repository.go | 5 |
4 files changed, 52 insertions, 19 deletions
diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 74ede47cef..53357e7505 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -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 diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go index caba2b506e..6d72849bca 100644 --- a/modules/convert/issue_comment.go +++ b/modules/convert/issue_comment.go @@ -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 { diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go index 962aae58bb..907ccafb66 100644 --- a/modules/convert/pull_review.go +++ b/modules/convert/pull_review.go @@ -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, diff --git a/modules/convert/repository.go b/modules/convert/repository.go index 1f11fda7ac..b813d69699 100644 --- a/modules/convert/repository.go +++ b/modules/convert/repository.go @@ -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), |