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 /routers/api | |
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 'routers/api')
-rw-r--r-- | routers/api/v1/org/team.go | 72 | ||||
-rw-r--r-- | routers/api/v1/repo/teams.go | 19 |
2 files changed, 36 insertions, 55 deletions
diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index d54ed5bb31..f8c37303d6 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -58,14 +58,10 @@ func ListTeams(ctx *context.APIContext) { return } - apiTeams := make([]*api.Team, len(teams)) - for i := range teams { - if err := teams[i].GetUnits(); err != nil { - ctx.Error(http.StatusInternalServerError, "GetUnits", err) - return - } - - apiTeams[i] = convert.ToTeam(teams[i]) + apiTeams, err := convert.ToTeams(teams, false) + if err != nil { + ctx.Error(http.StatusInternalServerError, "ConvertToTeams", err) + return } ctx.SetTotalCountHeader(count) @@ -101,25 +97,10 @@ func ListUserTeams(ctx *context.APIContext) { return } - cache := make(map[int64]*api.Organization) - apiTeams := make([]*api.Team, len(teams)) - for i := range teams { - apiOrg, ok := cache[teams[i].OrgID] - if !ok { - org, err := organization.GetOrgByID(teams[i].OrgID) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetUserByID", err) - return - } - apiOrg = convert.ToOrganization(org) - cache[teams[i].OrgID] = apiOrg - } - if err := teams[i].GetUnits(); err != nil { - ctx.Error(http.StatusInternalServerError, "teams[i].GetUnits()", err) - return - } - apiTeams[i] = convert.ToTeam(teams[i]) - apiTeams[i].Organization = apiOrg + apiTeams, err := convert.ToTeams(teams, true) + if err != nil { + ctx.Error(http.StatusInternalServerError, "ConvertToTeams", err) + return } ctx.SetTotalCountHeader(count) @@ -144,12 +125,13 @@ func GetTeam(ctx *context.APIContext) { // "200": // "$ref": "#/responses/Team" - if err := ctx.Org.Team.GetUnits(); err != nil { - ctx.Error(http.StatusInternalServerError, "team.GetUnits", err) + apiTeam, err := convert.ToTeam(ctx.Org.Team) + if err != nil { + ctx.InternalServerError(err) return } - ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team)) + ctx.JSON(http.StatusOK, apiTeam) } func attachTeamUnits(team *organization.Team, units []string) { @@ -241,7 +223,12 @@ func CreateTeam(ctx *context.APIContext) { return } - ctx.JSON(http.StatusCreated, convert.ToTeam(team)) + apiTeam, err := convert.ToTeam(team) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.JSON(http.StatusCreated, apiTeam) } // EditTeam api for edit a team @@ -318,7 +305,13 @@ func EditTeam(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "EditTeam", err) return } - ctx.JSON(http.StatusOK, convert.ToTeam(team)) + + apiTeam, err := convert.ToTeam(team) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.JSON(http.StatusOK, apiTeam) } // DeleteTeam api for delete a team @@ -782,17 +775,10 @@ func SearchTeam(ctx *context.APIContext) { return } - apiTeams := make([]*api.Team, len(teams)) - for i := range teams { - if err := teams[i].GetUnits(); err != nil { - log.Error("Team GetUnits failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ - "ok": false, - "error": "SearchTeam failed to get units", - }) - return - } - apiTeams[i] = convert.ToTeam(teams[i]) + apiTeams, err := convert.ToTeams(teams, false) + if err != nil { + ctx.InternalServerError(err) + return } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) diff --git a/routers/api/v1/repo/teams.go b/routers/api/v1/repo/teams.go index 1e3ea326d3..e414d8b60e 100644 --- a/routers/api/v1/repo/teams.go +++ b/routers/api/v1/repo/teams.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" - api "code.gitea.io/gitea/modules/structs" ) // ListTeams list a repository's teams @@ -48,14 +47,10 @@ func ListTeams(ctx *context.APIContext) { return } - apiTeams := make([]*api.Team, len(teams)) - for i := range teams { - if err := teams[i].GetUnits(); err != nil { - ctx.Error(http.StatusInternalServerError, "GetUnits", err) - return - } - - apiTeams[i] = convert.ToTeam(teams[i]) + apiTeams, err := convert.ToTeams(teams, false) + if err != nil { + ctx.InternalServerError(err) + return } ctx.JSON(http.StatusOK, apiTeams) @@ -103,11 +98,11 @@ func IsTeam(ctx *context.APIContext) { } if models.HasRepository(team, ctx.Repo.Repository.ID) { - if err := team.GetUnits(); err != nil { - ctx.Error(http.StatusInternalServerError, "GetUnits", err) + apiTeam, err := convert.ToTeam(team) + if err != nil { + ctx.InternalServerError(err) return } - apiTeam := convert.ToTeam(team) ctx.JSON(http.StatusOK, apiTeam) return } |