Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion integrations/api_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string
func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string) {
team := models.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team)
assert.NoError(t, team.GetUnits(), "GetUnits")
checkTeamResponse(t, convert.ToTeam(team), name, description, includesAllRepositories, permission, units)
apiTeam, err := convert.ToTeam(team)
assert.NoError(t, err)
checkTeamResponse(t, apiTeam, name, description, includesAllRepositories, permission, units)
}

type TeamSearchResults struct {
Expand Down
49 changes: 38 additions & 11 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,47 @@ func ToOrganization(org *models.User) *api.Organization {
}

// ToTeam convert models.Team to api.Team
func ToTeam(team *models.Team) *api.Team {
if team == nil {
return nil
func ToTeam(team *models.Team) (*api.Team, error) {
teams, err := ToTeams([]*models.Team{team})
if err != nil {
return nil, err
}
return teams[0], nil
}

// ToTeams convert models.Team list to api.Team list
func ToTeams(teams []*models.Team) ([]*api.Team, error) {
if len(teams) == 0 {
return nil, nil
}

return &api.Team{
ID: team.ID,
Name: team.Name,
Description: team.Description,
IncludesAllRepositories: team.IncludesAllRepositories,
CanCreateOrgRepo: team.CanCreateOrgRepo,
Permission: team.Authorize.String(),
Units: team.GetUnitNames(),
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 := models.GetUserByID(teams[i].OrgID)
if err != nil {
return nil, err
}
apiOrg = ToOrganization(org)
cache[teams[i].OrgID] = apiOrg
}
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].Authorize.String(),
Units: teams[i].GetUnitNames(),
}
apiTeams[i].Organization = apiOrg
}
return apiTeams, nil
}

// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
Expand Down
7 changes: 6 additions & 1 deletion modules/convert/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error)
r.Reviewer = models.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,
Expand Down
71 changes: 34 additions & 37 deletions routers/api/v1/org/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ func ListTeams(ctx *context.APIContext) {
return
}

apiTeams := make([]*api.Team, len(org.Teams))
for i := range org.Teams {
if err := org.Teams[i].GetUnits(); err != nil {
ctx.Error(http.StatusInternalServerError, "GetUnits", err)
return
}

apiTeams[i] = convert.ToTeam(org.Teams[i])
apiTeams, err := convert.ToTeams(org.Teams)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ConvertToTeams", err)
return
}

ctx.JSON(http.StatusOK, apiTeams)
}

Expand Down Expand Up @@ -91,22 +88,12 @@ 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 := models.GetUserByID(teams[i].OrgID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
return
}
apiOrg = convert.ToOrganization(org)
cache[teams[i].OrgID] = apiOrg
}
apiTeams[i] = convert.ToTeam(teams[i])
apiTeams[i].Organization = apiOrg
apiTeams, err := convert.ToTeams(teams)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ConvertToTeams", err)
return
}

ctx.JSON(http.StatusOK, apiTeams)
}

Expand All @@ -128,7 +115,13 @@ func GetTeam(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/Team"

ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team))
apiTeam, err := convert.ToTeam(ctx.Org.Team)
if err != nil {
ctx.InternalServerError(err)
return
}

ctx.JSON(http.StatusOK, apiTeam)
}

// CreateTeam api for create a team
Expand Down Expand Up @@ -187,7 +180,12 @@ func CreateTeam(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusCreated, convert.ToTeam(team))
apiTeam, err := convert.ToTeam(ctx.Org.Team)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.JSON(http.StatusCreated, apiTeam)
}

// EditTeam api for edit a team
Expand Down Expand Up @@ -268,7 +266,13 @@ func EditTeam(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "EditTeam", err)
return
}
ctx.JSON(http.StatusOK, convert.ToTeam(team))

apiTeam, err := convert.ToTeam(ctx.Org.Team)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.JSON(http.StatusOK, apiTeam)
}

// DeleteTeam api for delete a team
Expand Down Expand Up @@ -674,17 +678,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)
if err != nil {
ctx.InternalServerError(err)
return
}

ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
Expand Down
19 changes: 7 additions & 12 deletions routers/api/v1/repo/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"code.gitea.io/gitea/models"
"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
Expand Down Expand Up @@ -47,14 +46,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)
if err != nil {
ctx.InternalServerError(err)
return
}

ctx.JSON(http.StatusOK, apiTeams)
Expand Down Expand Up @@ -102,11 +97,11 @@ func IsTeam(ctx *context.APIContext) {
}

if team.HasRepository(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
}
Expand Down