From 5ac6da3c41f628f31b2805bfc422a3abb6b76d6b Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Thu, 17 Jan 2019 06:09:50 +0530 Subject: api: Add missing GET teams endpoints (#5382) * api: Add an endpoint to list a particular member of team. * models: Rename `GetUserTeams()` to `GetUserOrgTeams()` in `org_team` model. `GetUserTeams()` sounds a bit misnomer since it actually returns the teams that user belongs to in a given organization rather than all the teams across all the organization that the user has joined. * models: Add `GetUserTeams()`. Returns all the teams that a user belongs to. * api: Add an endpoint for GET '/user/teams'. A GET request to this endpoint lists all the teams that a user belongs to. --- routers/api/v1/api.go | 3 +++ routers/api/v1/org/team.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) (limited to 'routers/api/v1') diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index dcc77969f1..82c4b78de8 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -463,6 +463,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/times", repo.ListMyTrackedTimes) m.Get("/subscriptions", user.GetMyWatchedRepos) + + m.Get("/teams", org.ListUserTeams) }, reqToken()) // Repositories @@ -652,6 +654,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/members", func() { m.Get("", org.GetTeamMembers) m.Combo("/:username"). + Get(org.GetTeamMember). Put(reqOrgOwnership(), org.AddTeamMember). Delete(reqOrgOwnership(), org.RemoveTeamMember) }) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index a22d25eae3..a1916db00b 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -1,4 +1,5 @@ // Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -42,6 +43,41 @@ func ListTeams(ctx *context.APIContext) { ctx.JSON(200, apiTeams) } +// ListUserTeams list all the teams a user belongs to +func ListUserTeams(ctx *context.APIContext) { + // swagger:operation GET /user/teams user userListTeams + // --- + // summary: List all the teams a user belongs to + // produces: + // - application/json + // responses: + // "200": + // "$ref": "#/responses/TeamList" + teams, err := models.GetUserTeams(ctx.User.ID) + if err != nil { + ctx.Error(500, "GetUserTeams", err) + 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(500, "GetUserByID", err) + return + } + apiOrg = convert.ToOrganization(org) + cache[teams[i].OrgID] = apiOrg + } + apiTeams[i] = convert.ToTeam(teams[i]) + apiTeams[i].Organization = apiOrg + } + ctx.JSON(200, apiTeams) +} + // GetTeam api for get a team func GetTeam(ctx *context.APIContext) { // swagger:operation GET /teams/{id} organization orgGetTeam @@ -221,6 +257,35 @@ func GetTeamMembers(ctx *context.APIContext) { ctx.JSON(200, members) } +// GetTeamMember api for get a particular member of team +func GetTeamMember(ctx *context.APIContext) { + // swagger:operation GET /teams/{id}/members/{username} organization orgListTeamMember + // --- + // summary: List a particular member of team + // produces: + // - application/json + // parameters: + // - name: id + // in: path + // description: id of the team + // type: integer + // format: int64 + // required: true + // - name: username + // in: path + // description: username of the member to list + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/User" + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + ctx.JSON(200, u.APIFormat()) +} + // AddTeamMember api for add a member to a team func AddTeamMember(ctx *context.APIContext) { // swagger:operation PUT /teams/{id}/members/{username} organization orgAddTeamMember -- cgit v1.2.3