1
0
Fork 0
forked from forgejo/forgejo

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:
SpaWn2KiLl 2020-01-24 19:00:29 +00:00 committed by techknowlogick
parent 333401e0fd
commit 1f01f53c53
113 changed files with 1885 additions and 564 deletions

View file

@ -13,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"
)
// ListCollaborators list a repository's collaborators
@ -33,11 +34,19 @@ func ListCollaborators(ctx *context.APIContext) {
// description: name of the repo
// 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"
collaborators, err := ctx.Repo.Repository.GetCollaborators()
collaborators, err := ctx.Repo.Repository.GetCollaborators(utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
return

View file

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// GetSingleCommit get a commit via
@ -92,7 +93,11 @@ func GetAllCommits(ctx *context.APIContext) {
// type: string
// - name: page
// in: query
// description: page number of requested commits
// 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":
@ -117,9 +122,13 @@ func GetAllCommits(ctx *context.APIContext) {
}
defer gitRepo.Close()
page := ctx.QueryInt("page")
if page <= 0 {
page = 1
listOptions := utils.GetListOptions(ctx)
if listOptions.Page <= 0 {
listOptions.Page = 1
}
if listOptions.PageSize > git.CommitsRangeSize {
listOptions.PageSize = git.CommitsRangeSize
}
sha := ctx.Query("sha")
@ -154,10 +163,10 @@ func GetAllCommits(ctx *context.APIContext) {
return
}
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(git.CommitsRangeSize)))
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
// Query commits
commits, err := baseCommit.CommitsByRange(page)
commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.ServerError("CommitsByRange", err)
return
@ -181,13 +190,13 @@ func GetAllCommits(ctx *context.APIContext) {
i++
}
ctx.SetLinkHeader(int(commitsCountTotal), git.CommitsRangeSize)
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
ctx.Header().Set("X-Page", strconv.Itoa(page))
ctx.Header().Set("X-PerPage", strconv.Itoa(git.CommitsRangeSize))
ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount))
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
ctx.JSON(http.StatusOK, &apiCommits)
}

View file

@ -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.
@ -11,6 +12,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"
repo_service "code.gitea.io/gitea/services/repository"
)
@ -32,11 +34,19 @@ func ListForks(ctx *context.APIContext) {
// description: name of the repo
// 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"
forks, err := ctx.Repo.Repository.GetForks()
forks, err := ctx.Repo.Repository.GetForks(utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetForks", err)
return

View file

@ -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.
@ -34,11 +35,19 @@ func ListHooks(ctx *context.APIContext) {
// description: name of the repo
// 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/HookList"
hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetWebhooksByRepoID", err)
return

View file

@ -19,6 +19,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/v1/utils"
issue_service "code.gitea.io/gitea/services/issue"
)
@ -38,10 +39,6 @@ func SearchIssues(ctx *context.APIContext) {
// in: query
// description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
// type: string
// - name: page
// in: query
// description: page number of requested issues
// type: integer
// - name: q
// in: query
// description: search string
@ -55,6 +52,10 @@ func SearchIssues(ctx *context.APIContext) {
// in: query
// description: filter by type (issues / pulls) if set
// type: string
// - name: page
// in: query
// description: page number of requested issues
// type: integer
// responses:
// "200":
// "$ref": "#/responses/IssueList"
@ -72,7 +73,9 @@ func SearchIssues(ctx *context.APIContext) {
// find repos user can access (for issue search)
repoIDs := make([]int64, 0)
opts := &models.SearchRepoOptions{
PageSize: 15,
ListOptions: models.ListOptions{
PageSize: 15,
},
Private: false,
AllPublic: true,
TopicOnly: false,
@ -146,9 +149,11 @@ func SearchIssues(ctx *context.APIContext) {
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
issues, err = models.Issues(&models.IssuesOptions{
ListOptions: models.ListOptions{
Page: ctx.QueryInt("page"),
PageSize: setting.UI.IssuePagingNum,
},
RepoIDs: repoIDs,
Page: ctx.QueryInt("page"),
PageSize: setting.UI.IssuePagingNum,
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
@ -198,10 +203,6 @@ func ListIssues(ctx *context.APIContext) {
// in: query
// description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
// type: string
// - name: page
// in: query
// description: page number of requested issues
// type: integer
// - name: q
// in: query
// description: search string
@ -210,6 +211,14 @@ func ListIssues(ctx *context.APIContext) {
// in: query
// description: filter by type (issues / pulls) if set
// 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/IssueList"
@ -245,6 +254,11 @@ func ListIssues(ctx *context.APIContext) {
}
}
listOptions := utils.GetListOptions(ctx)
if ctx.QueryInt("limit") == 0 {
listOptions.PageSize = setting.UI.IssuePagingNum
}
var isPull util.OptionalBool
switch ctx.Query("type") {
case "pulls":
@ -259,13 +273,12 @@ func ListIssues(ctx *context.APIContext) {
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
issues, err = models.Issues(&models.IssuesOptions{
RepoIDs: []int64{ctx.Repo.Repository.ID},
Page: ctx.QueryInt("page"),
PageSize: setting.UI.IssuePagingNum,
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
IsPull: isPull,
ListOptions: listOptions,
RepoIDs: []int64{ctx.Repo.Repository.ID},
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
IsPull: isPull,
})
}
@ -279,7 +292,7 @@ func ListIssues(ctx *context.APIContext) {
apiIssues[i] = issues[i].APIFormat()
}
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, listOptions.PageSize)
ctx.JSON(http.StatusOK, &apiIssues)
}

View file

@ -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.
@ -117,6 +118,14 @@ func ListRepoIssueComments(ctx *context.APIContext) {
// description: if provided, only comments updated before the provided time are returned.
// type: string
// format: date-time
// - 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/CommentList"
@ -128,10 +137,11 @@ func ListRepoIssueComments(ctx *context.APIContext) {
}
comments, err := models.FindComments(models.FindCommentsOptions{
RepoID: ctx.Repo.Repository.ID,
Since: since,
Before: before,
Type: models.CommentTypeComment,
ListOptions: utils.GetListOptions(ctx),
RepoID: ctx.Repo.Repository.ID,
Type: models.CommentTypeComment,
Since: since,
Before: before,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "FindComments", err)

View file

@ -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"
)
// GetIssueCommentReactions list reactions of a comment from an issue
@ -245,6 +246,14 @@ func GetIssueReactions(ctx *context.APIContext) {
// type: integer
// format: int64
// 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/ReactionList"
@ -266,7 +275,7 @@ func GetIssueReactions(ctx *context.APIContext) {
return
}
reactions, err := models.FindIssueReactions(issue)
reactions, err := models.FindIssueReactions(issue, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "FindIssueReactions", err)
return

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// StartIssueStopwatch creates a stopwatch for the given issue.
@ -197,6 +198,15 @@ func GetStopwatches(ctx *context.APIContext) {
// swagger:operation GET /user/stopwatches user userGetStopWatches
// ---
// summary: Get list of all existing stopwatches
// 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
// consumes:
// - application/json
// produces:
@ -205,7 +215,7 @@ func GetStopwatches(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/StopWatchList"
sws, err := models.GetUserStopwatches(ctx.User.ID)
sws, err := models.GetUserStopwatches(ctx.User.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserStopwatches", err)
return

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// AddIssueSubscription Subscribe user to issue
@ -158,6 +159,14 @@ func GetIssueSubscribers(ctx *context.APIContext) {
// type: integer
// format: int64
// 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"
@ -175,7 +184,7 @@ func GetIssueSubscribers(ctx *context.APIContext) {
return
}
iwl, err := models.GetIssueWatchers(issue.ID)
iwl, err := models.GetIssueWatchers(issue.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetIssueWatchers", err)
return

View file

@ -51,6 +51,14 @@ func ListTrackedTimes(ctx *context.APIContext) {
// description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// - 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/TrackedTimeList"
@ -72,6 +80,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
}
opts := models.FindTrackedTimesOptions{
ListOptions: utils.GetListOptions(ctx),
RepositoryID: ctx.Repo.Repository.ID,
IssueID: issue.ID,
}
@ -435,6 +444,14 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
// description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// - 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/TrackedTimeList"
@ -449,6 +466,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
}
opts := models.FindTrackedTimesOptions{
ListOptions: utils.GetListOptions(ctx),
RepositoryID: ctx.Repo.Repository.ID,
}
@ -495,6 +513,15 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
// swagger:operation GET /user/times user userCurrentTrackedTimes
// ---
// summary: List the current user's tracked times
// 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
// parameters:
@ -512,7 +539,10 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/TrackedTimeList"
opts := models.FindTrackedTimesOptions{UserID: ctx.User.ID}
opts := models.FindTrackedTimesOptions{
ListOptions: utils.GetListOptions(ctx),
UserID: ctx.User.ID,
}
var err error
if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {

View file

@ -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.
@ -13,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// appendPrivateInformation appends the owner and key type information to api.PublicKey
@ -60,6 +62,14 @@ func ListDeployKeys(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/DeployKeyList"
@ -72,7 +82,7 @@ func ListDeployKeys(ctx *context.APIContext) {
if fingerprint != "" || keyID != 0 {
keys, err = models.SearchDeployKeys(ctx.Repo.Repository.ID, keyID, fingerprint)
} else {
keys, err = models.ListDeployKeys(ctx.Repo.Repository.ID)
keys, err = models.ListDeployKeys(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
}
if err != nil {

View file

@ -12,6 +12,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"
)
// ListLabels list all the labels of a repository
@ -32,11 +33,19 @@ func ListLabels(ctx *context.APIContext) {
// description: name of the repo
// 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/LabelList"
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsByRepoID", err)
return

View file

@ -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.
@ -12,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// ListMilestones list milestones for a repository
@ -36,11 +38,19 @@ func ListMilestones(ctx *context.APIContext) {
// in: query
// description: Milestone state, Recognised values are open, closed and all. Defaults to "open"
// 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/MilestoneList"
milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")))
milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")), utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetMilestonesByRepoID", err)
return

View file

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/notification"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/routers/api/v1/utils"
issue_service "code.gitea.io/gitea/services/issue"
pull_service "code.gitea.io/gitea/services/pull"
)
@ -41,10 +42,6 @@ func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions)
// description: name of the repo
// type: string
// required: true
// - name: page
// in: query
// description: Page number
// type: integer
// - name: state
// in: query
// description: "State of pull request: open or closed (optional)"
@ -68,12 +65,22 @@ func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions)
// items:
// type: integer
// format: int64
// - 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/PullRequestList"
listOptions := utils.GetListOptions(ctx)
prs, maxResults, err := models.PullRequests(ctx.Repo.Repository.ID, &models.PullRequestsOptions{
Page: ctx.QueryInt("page"),
ListOptions: listOptions,
State: ctx.QueryTrim("state"),
SortType: ctx.QueryTrim("sort"),
Labels: ctx.QueryStrings("labels"),
@ -106,7 +113,7 @@ func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions)
apiPrs[i] = convert.ToAPIPullRequest(prs[i])
}
ctx.SetLinkHeader(int(maxResults), models.ItemsPerPage)
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.JSON(http.StatusOK, &apiPrs)
}

View file

@ -9,8 +9,8 @@ import (
"code.gitea.io/gitea/models"
"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"
releaseservice "code.gitea.io/gitea/services/release"
)
@ -59,20 +59,6 @@ func GetRelease(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, release.APIFormat())
}
func getPagesInfo(ctx *context.APIContext) (int, int) {
page := ctx.QueryInt("page")
if page == 0 {
page = 1
}
perPage := ctx.QueryInt("per_page")
if perPage == 0 {
perPage = setting.API.DefaultPagingNum
} else if perPage > setting.API.MaxResponseItems {
perPage = setting.API.MaxResponseItems
}
return page, perPage
}
// ListReleases list a repository's releases
func ListReleases(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases repository repoListReleases
@ -91,23 +77,32 @@ func ListReleases(ctx *context.APIContext) {
// description: name of the repo
// type: string
// required: true
// - name: page
// in: query
// description: page wants to load
// type: integer
// - name: per_page
// in: query
// description: items count every page wants to load
// type: integer
// deprecated: 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/ReleaseList"
listOptions := utils.GetListOptions(ctx)
if ctx.QueryInt("per_page") != 0 {
listOptions.PageSize = ctx.QueryInt("per_page")
}
page, limit := getPagesInfo(ctx)
releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
ListOptions: listOptions,
IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite,
IncludeTags: false,
}, page, limit)
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetReleasesByRepoID", err)
return

View file

@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
@ -27,6 +26,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/routers/api/v1/utils"
mirror_service "code.gitea.io/gitea/services/mirror"
repo_service "code.gitea.io/gitea/services/repository"
)
@ -91,14 +91,6 @@ func Search(ctx *context.APIContext) {
// in: query
// description: include template repositories this user has access to (defaults to true)
// type: boolean
// - 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
// - name: mode
// in: query
// description: type of repository to search for. Supported values are
@ -119,6 +111,14 @@ func Search(ctx *context.APIContext) {
// description: sort order, either "asc" (ascending) or "desc" (descending).
// Default is "asc", ignored if "sort" is not specified.
// 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/SearchResults"
@ -126,12 +126,11 @@ func Search(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
opts := &models.SearchRepoOptions{
ListOptions: utils.GetListOptions(ctx),
Actor: ctx.User,
Keyword: strings.Trim(ctx.Query("q"), " "),
OwnerID: ctx.QueryInt64("uid"),
PriorityOwnerID: ctx.QueryInt64("priority_owner_id"),
Page: ctx.QueryInt("page"),
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
TopicOnly: ctx.QueryBool("topic"),
Collaborate: util.OptionalBoolNone,
Private: ctx.IsSigned && (ctx.Query("private") == "" || ctx.QueryBool("private")),
@ -214,7 +213,7 @@ func Search(ctx *context.APIContext) {
results[i] = repo.APIFormat(accessMode)
}
ctx.SetLinkHeader(int(count), setting.API.MaxResponseItems)
ctx.SetLinkHeader(int(count), opts.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, api.SearchResults{
OK: true,

View file

@ -10,6 +10,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"
)
// ListStargazers list a repository's stargazers
@ -30,11 +31,19 @@ func ListStargazers(ctx *context.APIContext) {
// description: name of the repo
// 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"
stargazers, err := ctx.Repo.Repository.GetStargazers(-1)
stargazers, err := ctx.Repo.Repository.GetStargazers(utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetStargazers", err)
return

View file

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// NewCommitStatus creates a new CommitStatus
@ -89,11 +90,6 @@ func GetCommitStatuses(ctx *context.APIContext) {
// description: sha of the commit
// type: string
// required: true
// - name: page
// in: query
// description: page number of results
// type: integer
// required: false
// - name: sort
// in: query
// description: type of sort
@ -106,6 +102,14 @@ func GetCommitStatuses(ctx *context.APIContext) {
// type: string
// enum: [pending, success, error, failure, warning]
// required: false
// - 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/StatusList"
@ -138,11 +142,6 @@ func GetCommitStatusesByRef(ctx *context.APIContext) {
// description: name of branch/tag/commit
// type: string
// required: true
// - name: page
// in: query
// description: page number of results
// type: integer
// required: false
// - name: sort
// in: query
// description: type of sort
@ -155,6 +154,14 @@ func GetCommitStatusesByRef(ctx *context.APIContext) {
// type: string
// enum: [pending, success, error, failure, warning]
// required: false
// - 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/StatusList"
@ -202,9 +209,9 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
repo := ctx.Repo.Repository
statuses, _, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{
Page: ctx.QueryInt("page"),
SortType: ctx.QueryTrim("sort"),
State: ctx.QueryTrim("state"),
ListOptions: utils.GetListOptions(ctx),
SortType: ctx.QueryTrim("sort"),
State: ctx.QueryTrim("state"),
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, ctx.QueryInt("page"), err))

View file

@ -10,6 +10,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"
)
// ListSubscribers list a repo's subscribers (i.e. watchers)
@ -30,11 +31,19 @@ func ListSubscribers(ctx *context.APIContext) {
// description: name of the repo
// 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"
subscribers, err := ctx.Repo.Repository.GetWatchers(0)
subscribers, err := ctx.Repo.Repository.GetWatchers(utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetWatchers", err)
return

View file

@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
)
// ListTopics returns list of current topics for repo
@ -33,12 +34,21 @@ func ListTopics(ctx *context.APIContext) {
// description: name of the repo
// 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/TopicNames"
topics, err := models.FindTopics(&models.FindTopicOptions{
RepoID: ctx.Repo.Repository.ID,
ListOptions: utils.GetListOptions(ctx),
RepoID: ctx.Repo.Repository.ID,
})
if err != nil {
log.Error("ListTopics failed: %v", err)
@ -231,7 +241,7 @@ func DeleteTopic(ctx *context.APIContext) {
}
// TopicSearch search for creating topic
func TopicSearch(ctx *context.Context) {
func TopicSearch(ctx *context.APIContext) {
// swagger:operation GET /topics/search repository topicSearch
// ---
// summary: search topics via keyword
@ -243,6 +253,14 @@ func TopicSearch(ctx *context.Context) {
// description: keywords to search
// required: true
// 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/TopicListResponse"
@ -256,9 +274,17 @@ func TopicSearch(ctx *context.Context) {
kw := ctx.Query("q")
listOptions := utils.GetListOptions(ctx)
if listOptions.Page < 1 {
listOptions.Page = 1
}
if listOptions.PageSize < 1 {
listOptions.PageSize = 10
}
topics, err := models.FindTopics(&models.FindTopicOptions{
Keyword: kw,
Limit: 10,
Keyword: kw,
ListOptions: listOptions,
})
if err != nil {
log.Error("SearchTopics failed: %v", err)