mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
API add/generalize pagination (#9452)
* paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
committed by
techknowlogick
parent
333401e0fd
commit
1f01f53c53
@@ -11,6 +11,7 @@ import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// ListAccessTokens list all the access tokens
|
||||
@@ -26,11 +27,19 @@ func ListAccessTokens(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/AccessTokenList"
|
||||
|
||||
tokens, err := models.ListAccessTokens(ctx.User.ID)
|
||||
tokens, err := models.ListAccessTokens(ctx.User.ID, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err)
|
||||
return
|
||||
|
@@ -1,4 +1,5 @@
|
||||
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2020 The Gitea Authors.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
func responseAPIUsers(ctx *context.APIContext, users []*models.User) {
|
||||
@@ -22,7 +24,7 @@ func responseAPIUsers(ctx *context.APIContext, users []*models.User) {
|
||||
}
|
||||
|
||||
func listUserFollowers(ctx *context.APIContext, u *models.User) {
|
||||
users, err := u.GetFollowers(ctx.QueryInt("page"))
|
||||
users, err := u.GetFollowers(utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
|
||||
return
|
||||
@@ -35,6 +37,15 @@ func ListMyFollowers(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/followers user userCurrentListFollowers
|
||||
// ---
|
||||
// summary: List the authenticated user's followers
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
@@ -57,6 +68,14 @@ func ListFollowers(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
@@ -69,7 +88,7 @@ func ListFollowers(ctx *context.APIContext) {
|
||||
}
|
||||
|
||||
func listUserFollowing(ctx *context.APIContext, u *models.User) {
|
||||
users, err := u.GetFollowing(ctx.QueryInt("page"))
|
||||
users, err := u.GetFollowing(utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetFollowing", err)
|
||||
return
|
||||
@@ -82,6 +101,15 @@ func ListMyFollowing(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/following user userCurrentListFollowing
|
||||
// ---
|
||||
// summary: List the users that the authenticated user is following
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
@@ -104,6 +132,14 @@ func ListFollowing(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
|
@@ -11,10 +11,11 @@ import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
func listGPGKeys(ctx *context.APIContext, uid int64) {
|
||||
keys, err := models.ListGPGKeys(uid)
|
||||
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions models.ListOptions) {
|
||||
keys, err := models.ListGPGKeys(uid, listOptions)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
|
||||
return
|
||||
@@ -41,6 +42,14 @@ func ListGPGKeys(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/GPGKeyList"
|
||||
@@ -49,7 +58,7 @@ func ListGPGKeys(ctx *context.APIContext) {
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
listGPGKeys(ctx, user.ID)
|
||||
listGPGKeys(ctx, user.ID, utils.GetListOptions(ctx))
|
||||
}
|
||||
|
||||
//ListMyGPGKeys get the GPG key list of the authenticated user
|
||||
@@ -57,13 +66,22 @@ func ListMyGPGKeys(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
|
||||
// ---
|
||||
// summary: List the authenticated user's GPG keys
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/GPGKeyList"
|
||||
|
||||
listGPGKeys(ctx, ctx.User.ID)
|
||||
listGPGKeys(ctx, ctx.User.ID, utils.GetListOptions(ctx))
|
||||
}
|
||||
|
||||
//GetGPGKey get the GPG key based on a id
|
||||
|
@@ -13,6 +13,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/repo"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// appendPrivateInformation appends the owner and key type information to api.PublicKey
|
||||
@@ -79,7 +80,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) {
|
||||
}
|
||||
} else {
|
||||
// Use ListPublicKeys
|
||||
keys, err = models.ListPublicKeys(user.ID)
|
||||
keys, err = models.ListPublicKeys(user.ID, utils.GetListOptions(ctx))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -109,6 +110,14 @@ func ListMyPublicKeys(ctx *context.APIContext) {
|
||||
// in: query
|
||||
// description: fingerprint of the key
|
||||
// type: string
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
@@ -135,6 +144,14 @@ func ListPublicKeys(ctx *context.APIContext) {
|
||||
// in: query
|
||||
// description: fingerprint of the key
|
||||
// type: string
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/PublicKeyList"
|
||||
|
@@ -10,11 +10,16 @@ import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// listUserRepos - List the repositories owned by the given user.
|
||||
func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
|
||||
repos, err := models.GetUserRepositories(u.ID, private, 1, u.NumRepos, "")
|
||||
repos, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
||||
Actor: u,
|
||||
Private: private,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
||||
return
|
||||
@@ -47,6 +52,14 @@ func ListUserRepos(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
@@ -66,11 +79,24 @@ func ListMyRepos(ctx *context.APIContext) {
|
||||
// summary: List the repos that the authenticated user owns or has access to
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
|
||||
ownRepos, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
||||
Actor: ctx.User,
|
||||
Private: true,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
||||
return
|
||||
@@ -106,6 +132,14 @@ func ListOrgRepos(ctx *context.APIContext) {
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
@@ -1,4 +1,5 @@
|
||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2020 The Gitea Authors.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@@ -10,12 +11,13 @@ import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// getStarredRepos returns the repos that the user with the specified userID has
|
||||
// starred
|
||||
func getStarredRepos(user *models.User, private bool) ([]*api.Repository, error) {
|
||||
starredRepos, err := models.GetStarredRepos(user.ID, private)
|
||||
func getStarredRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, error) {
|
||||
starredRepos, err := models.GetStarredRepos(user.ID, private, listOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -44,13 +46,21 @@ func GetStarredRepos(ctx *context.APIContext) {
|
||||
// description: username of user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
private := user.ID == ctx.User.ID
|
||||
repos, err := getStarredRepos(user, private)
|
||||
repos, err := getStarredRepos(user, private, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getStarredRepos", err)
|
||||
}
|
||||
@@ -62,13 +72,22 @@ func GetMyStarredRepos(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/starred user userCurrentListStarred
|
||||
// ---
|
||||
// summary: The repos that the authenticated user has starred
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
repos, err := getStarredRepos(ctx.User, true)
|
||||
repos, err := getStarredRepos(ctx.User, true, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getStarredRepos", err)
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2020 The Gitea Authors.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
@@ -33,9 +35,13 @@ func Search(ctx *context.APIContext) {
|
||||
// description: ID of the user to search for
|
||||
// type: integer
|
||||
// format: int64
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: maximum number of users to return
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
@@ -51,10 +57,10 @@ func Search(ctx *context.APIContext) {
|
||||
// "$ref": "#/definitions/User"
|
||||
|
||||
opts := &models.SearchUserOptions{
|
||||
Keyword: strings.Trim(ctx.Query("q"), " "),
|
||||
UID: com.StrTo(ctx.Query("uid")).MustInt64(),
|
||||
Type: models.UserTypeIndividual,
|
||||
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
|
||||
Keyword: strings.Trim(ctx.Query("q"), " "),
|
||||
UID: com.StrTo(ctx.Query("uid")).MustInt64(),
|
||||
Type: models.UserTypeIndividual,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
}
|
||||
|
||||
users, _, err := models.SearchUsers(opts)
|
||||
|
@@ -11,12 +11,13 @@ import (
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// getWatchedRepos returns the repos that the user with the specified userID is
|
||||
// watching
|
||||
func getWatchedRepos(user *models.User, private bool) ([]*api.Repository, error) {
|
||||
watchedRepos, err := models.GetWatchedRepos(user.ID, private)
|
||||
func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, error) {
|
||||
watchedRepos, err := models.GetWatchedRepos(user.ID, private, listOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -45,13 +46,21 @@ func GetWatchedRepos(ctx *context.APIContext) {
|
||||
// in: path
|
||||
// description: username of the user
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
private := user.ID == ctx.User.ID
|
||||
repos, err := getWatchedRepos(user, private)
|
||||
repos, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
||||
}
|
||||
@@ -65,11 +74,20 @@ func GetMyWatchedRepos(ctx *context.APIContext) {
|
||||
// summary: List repositories watched by the authenticated user
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
repos, err := getWatchedRepos(ctx.User, true)
|
||||
repos, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user