Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Add missing GET teams endpoints #5382

Merged
merged 6 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func removeOrgUser(sess *xorm.Session, orgID, userID int64) error {
}

// Delete member in his/her teams.
teams, err := getUserTeams(sess, org.ID, userID)
teams, err := getUserOrgTeams(sess, org.ID, userID)
if err != nil {
return err
}
Expand Down
20 changes: 16 additions & 4 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,14 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
return getTeamMembers(x, teamID)
}

func getUserTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
func getUserTeams(e Engine, userID int64) (teams []*Team, err error) {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Where("team_user.uid=?", userID).
Find(&teams)
}

func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Where("team.org_id = ?", orgID).
Expand All @@ -561,9 +568,14 @@ func getUserRepoTeams(e Engine, orgID, userID, repoID int64) (teams []*Team, err
Find(&teams)
}

// GetUserTeams returns all teams that user belongs to in given organization.
func GetUserTeams(orgID, userID int64) ([]*Team, error) {
return getUserTeams(x, orgID, userID)
// GetUserOrgTeams returns all teams that user belongs to in given organization.
func GetUserOrgTeams(orgID, userID int64) ([]*Team, error) {
return getUserOrgTeams(x, orgID, userID)
}

// GetUserTeams returns all teams that user belongs across all organizations.
func GetUserTeams(userID int64) ([]*Team, error) {
return getUserTeams(x, userID)
}

// AddTeamMember adds new membership of given team to given organization,
Expand Down
16 changes: 15 additions & 1 deletion models/org_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,23 @@ func TestGetTeamMembers(t *testing.T) {
}

func TestGetUserTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(userID int64) {
teams, err := GetUserTeams(userID)
assert.NoError(t, err)
for _, team := range teams {
AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
}
}
test(2)
test(5)
test(NonexistentID)
}

func TestGetUserOrgTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(orgID, userID int64) {
teams, err := GetUserTeams(orgID, userID)
teams, err := GetUserOrgTeams(orgID, userID)
assert.NoError(t, err)
for _, team := range teams {
assert.EqualValues(t, orgID, team.OrgID)
Expand Down
3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -652,6 +654,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/members", func() {
m.Get("", org.GetTeamMembers)
m.Combo("/:username").
Get(org.GetTeamMember).
HarshitOnGitHub marked this conversation as resolved.
Show resolved Hide resolved
Put(reqOrgOwnership(), org.AddTeamMember).
Delete(reqOrgOwnership(), org.RemoveTeamMember)
})
Expand Down
64 changes: 64 additions & 0 deletions routers/api/v1/org/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
Expand Down Expand Up @@ -221,6 +256,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())
HarshitOnGitHub marked this conversation as resolved.
Show resolved Hide resolved
}

// AddTeamMember api for add a member to a team
func AddTeamMember(ctx *context.APIContext) {
// swagger:operation PUT /teams/{id}/members/{username} organization orgAddTeamMember
Expand Down
52 changes: 52 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4629,6 +4629,38 @@
}
},
"/teams/{id}/members/{username}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"organization"
],
"summary": "List a particular member of team",
"operationId": "orgListTeamMember",
"parameters": [
{
"type": "integer",
"format": "int64",
"description": "id of the team",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "username of the member to list",
"name": "username",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/User"
}
}
},
"put": {
"produces": [
"application/json"
Expand Down Expand Up @@ -5418,6 +5450,23 @@
}
}
},
"/user/teams": {
"get": {
"produces": [
"application/json"
],
"tags": [
"user"
],
"summary": "List all the teams a user belongs to",
"operationId": "userListTeams",
"responses": {
"200": {
"$ref": "#/responses/TeamList"
}
}
}
},
"/user/times": {
"get": {
"produces": [
Expand Down Expand Up @@ -7942,6 +7991,9 @@
"type": "string",
"x-go-name": "Name"
},
"organization": {
"$ref": "#/definitions/Organization"
},
"permission": {
"type": "string",
"enum": [
Expand Down
1 change: 1 addition & 0 deletions vendor/code.gitea.io/sdk/gitea/hook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions vendor/code.gitea.io/sdk/gitea/org_team.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/code.gitea.io/sdk/gitea/release.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.