Skip to content

Commit

Permalink
New API routes added (go-gitea#5594)
Browse files Browse the repository at this point in the history
* New API routes added

* Comments added

* Build fix

* swagger_v1_json.tmpl without new line character

* Typo fix

* Code review changes

* Code review changes

* Add copyright

* Add copyright

* Add copyright

* Update per @lafriks feedback

* Update org.go

* Update user.go

* Update user.go

* make fmt
  • Loading branch information
shashvat-kedia authored and bmackinney committed Jan 27, 2019
1 parent df619cf commit 5bbddb9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
13 changes: 8 additions & 5 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 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.

Expand Down Expand Up @@ -1358,7 +1359,7 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
return nil, 0, fmt.Errorf("Count: %v", err)
}

if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum {
if opts.PageSize == 0 || opts.PageSize > setting.UI.ExplorePagingNum {

This comment has been minimized.

Copy link
@jgoldring

jgoldring Feb 15, 2019

Not sure if this was intended, but this change of <= to == allows the default PageSize of -1 (from the new admin calls getAllOrgs() and getAllUsers()) to pass all the way through to the make, leading to a "cap out of range" error. Posting this comment here, and opening a new issue asap.

opts.PageSize = setting.UI.ExplorePagingNum
}
if opts.Page <= 0 {
Expand All @@ -1368,11 +1369,13 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
opts.OrderBy = SearchOrderByAlphabetically
}

sess := x.Where(cond)
if opts.PageSize > 0 {
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}

users = make([]*User, 0, opts.PageSize)
return users, count, x.Where(cond).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy.String()).
Find(&users)
return users, count, sess.OrderBy(opts.OrderBy.String()).Find(&users)
}

// GetStarredRepos returns the repos starred by a particular user
Expand Down
29 changes: 29 additions & 0 deletions routers/api/v1/admin/org.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 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.

Expand Down Expand Up @@ -66,3 +67,31 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {

ctx.JSON(201, convert.ToOrganization(org))
}

//GetAllOrgs API for getting information of all the organizations
func GetAllOrgs(ctx *context.APIContext) {
// swagger:operation GET /admin/orgs admin adminGetAllOrgs
// ---
// summary: List all organizations
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/OrganizationList"
// "403":
// "$ref": "#/responses/forbidden"
users, _, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeOrganization,
OrderBy: models.SearchOrderByAlphabetically,
PageSize: -1,
})
if err != nil {
ctx.Error(500, "SearchOrganizations", err)
return
}
orgs := make([]*api.Organization, len(users))
for i := range users {
orgs[i] = convert.ToOrganization(users[i])
}
ctx.JSON(200, &orgs)
}
25 changes: 25 additions & 0 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 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.

Expand Down Expand Up @@ -291,3 +292,27 @@ func DeleteUserPublicKey(ctx *context.APIContext) {

ctx.Status(204)
}

//GetAllUsers API for getting information of all the users
func GetAllUsers(ctx *context.APIContext) {
// swagger:operation GET /admin/users admin adminGetAllUsers
// ---
// summary: List all users
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/UserList"
// "403":
// "$ref": "#/responses/forbidden"
users, _, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeIndividual,
OrderBy: models.SearchOrderByAlphabetically,
PageSize: -1,
})
if err != nil {
ctx.Error(500, "SearchUsers", err)
return
}
ctx.JSON(200, &users)
}
3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,9 @@ func RegisterRoutes(m *macaron.Macaron) {
})

m.Group("/admin", func() {
m.Get("/orgs", admin.GetAllOrgs)
m.Group("/users", func() {
m.Get("", admin.GetAllUsers)
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
m.Group("/:username", func() {
m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
Expand All @@ -680,6 +682,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
m.Delete("/:id", admin.DeleteUserPublicKey)
})
m.Get("/orgs", org.ListUserOrgs)
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
})
Expand Down
38 changes: 38 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,45 @@
},
"basePath": "{{AppSubUrl}}/api/v1",
"paths": {
"/admin/orgs": {
"get": {
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "List all organizations",
"operationId": "adminGetAllOrgs",
"responses": {
"200": {
"$ref": "#/responses/OrganizationList"
},
"403": {
"$ref": "#/responses/forbidden"
}
}
}
},
"/admin/users": {
"get": {
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "List all users",
"operationId": "adminGetAllUsers",
"responses": {
"200": {
"$ref": "#/responses/UserList"
},
"403": {
"$ref": "#/responses/forbidden"
}
}
},
"post": {
"consumes": [
"application/json"
Expand Down

0 comments on commit 5bbddb9

Please sign in to comment.