From f686a32eac75460d5b1fcf16b6f6c2ed57f5b03e Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Tue, 27 Dec 2016 20:36:04 -0500 Subject: [PATCH] API endpoints for organization teams (#370) --- routers/api/v1/admin/org_team.go | 25 +++++++++++++++++++++ routers/api/v1/api.go | 7 ++++++ routers/api/v1/org/team.go | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/routers/api/v1/admin/org_team.go b/routers/api/v1/admin/org_team.go index 93af1ec427..716bd675fa 100644 --- a/routers/api/v1/admin/org_team.go +++ b/routers/api/v1/admin/org_team.go @@ -33,6 +33,31 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { ctx.JSON(201, convert.ToTeam(team)) } +// EditTeam api for edit a team +func EditTeam(ctx *context.APIContext, form api.EditTeamOption) { + team := &models.Team{ + ID: ctx.Org.Team.ID, + OrgID: ctx.Org.Team.OrgID, + Name: form.Name, + Description: form.Description, + Authorize: models.ParseAccessMode(form.Permission), + } + if err := models.UpdateTeam(team, true); err != nil { + ctx.Error(500, "EditTeam", err) + return + } + ctx.JSON(200, convert.ToTeam(team)) +} + +// DeleteTeam api for delete a team +func DeleteTeam(ctx *context.APIContext) { + if err := models.DeleteTeam(ctx.Org.Team); err != nil { + ctx.Error(500, "DeleteTeam", err) + return + } + ctx.Status(204) +} + // AddTeamMember api for add a member to a team func AddTeamMember(ctx *context.APIContext) { u := user.GetUserByParams(ctx) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 17e1f9bfc2..281180a18c 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -357,6 +357,11 @@ func RegisterRoutes(m *macaron.Macaron) { Delete(org.DeleteHook) }) }, orgAssignment(true)) + m.Group("/teams/:teamid", func() { + m.Get("", org.GetTeam) + m.Get("/members", org.GetTeamMembers) + m.Get("/repos", org.GetTeamRepos) + }, orgAssignment(false, true)) m.Any("/*", func(ctx *context.Context) { ctx.Error(404) @@ -382,6 +387,8 @@ func RegisterRoutes(m *macaron.Macaron) { }) m.Group("/teams", func() { m.Group("/:teamid", func() { + m.Combo("").Patch(bind(api.EditTeamOption{}), admin.EditTeam). + Delete(admin.DeleteTeam) m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember) m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository) }, orgAssignment(false, true)) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index f5b2e9dc55..2f51518875 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -7,6 +7,7 @@ package org import ( api "code.gitea.io/sdk/gitea" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/routers/api/v1/convert" ) @@ -25,3 +26,40 @@ func ListTeams(ctx *context.APIContext) { } ctx.JSON(200, apiTeams) } + +// GetTeam api for get a team +func GetTeam(ctx *context.APIContext) { + ctx.JSON(200, convert.ToTeam(ctx.Org.Team)) +} + +// GetTeamMembers api for get a team's members +func GetTeamMembers(ctx *context.APIContext) { + team := ctx.Org.Team + if err := team.GetMembers(); err != nil { + ctx.Error(500, "GetTeamMembers", err) + return + } + members := make([]*api.User, len(team.Members)) + for i, member := range team.Members { + members[i] = member.APIFormat() + } + ctx.JSON(200, members) +} + +// GetTeamRepos api for get a team's repos +func GetTeamRepos(ctx *context.APIContext) { + team := ctx.Org.Team + if err := team.GetRepositories(); err != nil { + ctx.Error(500, "GetTeamRepos", err) + } + repos := make([]*api.Repository, len(team.Repos)) + for i, repo := range team.Repos { + access, err := models.AccessLevel(ctx.User, repo) + if err != nil { + ctx.Error(500, "GetTeamRepos", err) + return + } + repos[i] = repo.APIFormat(access) + } + ctx.JSON(200, repos) +}