diff options
Diffstat (limited to 'routers/api')
37 files changed, 630 insertions, 151 deletions
diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go index ca2ef574f3..01c2c49734 100644 --- a/routers/api/v1/admin/org.go +++ b/routers/api/v1/admin/org.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/routers/api/v1/utils" ) // CreateOrg api for create organization @@ -100,11 +101,10 @@ func GetAllOrgs(ctx *context.APIContext) { // "$ref": "#/responses/forbidden" users, _, err := models.SearchUsers(&models.SearchUserOptions{ - Type: models.UserTypeOrganization, - OrderBy: models.SearchOrderByAlphabetically, - Page: ctx.QueryInt("page"), - PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), - Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate}, + Type: models.UserTypeOrganization, + OrderBy: models.SearchOrderByAlphabetically, + ListOptions: utils.GetListOptions(ctx), + Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate}, }) if err != nil { ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err) diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index ebc651516a..28acc062cb 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/password" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/mailer" ) @@ -328,6 +329,15 @@ func GetAllUsers(ctx *context.APIContext) { // summary: List all users // 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/UserList" @@ -335,9 +345,9 @@ func GetAllUsers(ctx *context.APIContext) { // "$ref": "#/responses/forbidden" users, _, err := models.SearchUsers(&models.SearchUserOptions{ - Type: models.UserTypeIndividual, - OrderBy: models.SearchOrderByAlphabetically, - PageSize: -1, + Type: models.UserTypeIndividual, + OrderBy: models.SearchOrderByAlphabetically, + ListOptions: utils.GetListOptions(ctx), }) if err != nil { ctx.Error(http.StatusInternalServerError, "GetAllUsers", err) diff --git a/routers/api/v1/notify/repo.go b/routers/api/v1/notify/repo.go index b939d90f06..10c00c5467 100644 --- a/routers/api/v1/notify/repo.go +++ b/routers/api/v1/notify/repo.go @@ -51,6 +51,14 @@ func ListRepoNotifications(ctx *context.APIContext) { // type: string // format: date-time // 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/NotificationThreadList" @@ -61,6 +69,7 @@ func ListRepoNotifications(ctx *context.APIContext) { return } opts := models.FindNotificationOptions{ + ListOptions: utils.GetListOptions(ctx), UserID: ctx.User.ID, RepoID: ctx.Repo.Repository.ID, UpdatedBeforeUnix: before, diff --git a/routers/api/v1/notify/user.go b/routers/api/v1/notify/user.go index d16e4da0e0..7f731e25d4 100644 --- a/routers/api/v1/notify/user.go +++ b/routers/api/v1/notify/user.go @@ -41,6 +41,14 @@ func ListNotifications(ctx *context.APIContext) { // type: string // format: date-time // 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/NotificationThreadList" @@ -51,6 +59,7 @@ func ListNotifications(ctx *context.APIContext) { return } opts := models.FindNotificationOptions{ + ListOptions: utils.GetListOptions(ctx), UserID: ctx.User.ID, UpdatedBeforeUnix: before, UpdatedAfterUnix: since, diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index b3faac7b51..2fd084a13a 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -27,12 +27,20 @@ func ListHooks(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/HookList" org := ctx.Org.Organization - orgHooks, err := models.GetWebhooksByOrgID(org.ID) + orgHooks, err := models.GetWebhooksByOrgID(org.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetWebhooksByOrgID", err) return diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 1575600a65..6450577f4c 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -14,14 +14,16 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/routers/api/v1/utils" ) // listMembers list an organization's members func listMembers(ctx *context.APIContext, publicOnly bool) { var members []*models.User - members, _, err := models.FindOrgMembers(models.FindOrgMembersOpts{ - OrgID: ctx.Org.Organization.ID, - PublicOnly: publicOnly, + members, _, err := models.FindOrgMembers(&models.FindOrgMembersOpts{ + OrgID: ctx.Org.Organization.ID, + PublicOnly: publicOnly, + ListOptions: utils.GetListOptions(ctx), }) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err) @@ -48,6 +50,14 @@ func ListMembers(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/UserList" @@ -75,6 +85,14 @@ func ListPublicMembers(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 // produces: // - application/json // responses: diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 4bcd60a679..fde58bd5c1 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -13,10 +13,14 @@ import ( "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/routers/api/v1/utils" ) func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) { - if err := u.GetOrganizations(all); err != nil { + if err := u.GetOrganizations(&models.SearchOrganizationsOptions{ + ListOptions: utils.GetListOptions(ctx), + All: all, + }); err != nil { ctx.Error(http.StatusInternalServerError, "GetOrganizations", err) return } @@ -35,6 +39,15 @@ func ListMyOrgs(ctx *context.APIContext) { // summary: List the current user's organizations // 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/OrganizationList" @@ -55,6 +68,14 @@ func ListUserOrgs(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/OrganizationList" @@ -95,11 +116,10 @@ func GetAll(ctx *context.APIContext) { } publicOrgs, _, err := models.SearchUsers(&models.SearchUserOptions{ - Type: models.UserTypeOrganization, - OrderBy: models.SearchOrderByAlphabetically, - Page: ctx.QueryInt("page"), - PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), - Visible: vMode, + ListOptions: utils.GetListOptions(ctx), + Type: models.UserTypeOrganization, + OrderBy: models.SearchOrderByAlphabetically, + Visible: vMode, }) if err != nil { ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 446287a343..1bf984b17f 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/routers/api/v1/utils" ) // ListTeams list all the teams of an organization @@ -30,12 +31,22 @@ func ListTeams(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/TeamList" org := ctx.Org.Organization - if err := org.GetTeams(); err != nil { + if err := org.GetTeams(&models.SearchTeamOptions{ + ListOptions: utils.GetListOptions(ctx), + }); err != nil { ctx.Error(http.StatusInternalServerError, "GetTeams", err) return } @@ -59,11 +70,20 @@ func ListUserTeams(ctx *context.APIContext) { // summary: List all the teams a user belongs 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/TeamList" - teams, err := models.GetUserTeams(ctx.User.ID) + teams, err := models.GetUserTeams(ctx.User.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserTeams", err) return @@ -284,6 +304,14 @@ func GetTeamMembers(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" @@ -297,7 +325,9 @@ func GetTeamMembers(ctx *context.APIContext) { return } team := ctx.Org.Team - if err := team.GetMembers(); err != nil { + if err := team.GetMembers(&models.SearchMembersOptions{ + ListOptions: utils.GetListOptions(ctx), + }); err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamMembers", err) return } @@ -436,12 +466,22 @@ func GetTeamRepos(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/RepositoryList" team := ctx.Org.Team - if err := team.GetRepositories(); err != nil { + if err := team.GetRepositories(&models.SearchTeamOptions{ + ListOptions: utils.GetListOptions(ctx), + }); err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) } repos := make([]*api.Repository, len(team.Repos)) @@ -589,14 +629,14 @@ func SearchTeam(ctx *context.APIContext) { // in: query // description: include search within team description (defaults to true) // type: boolean - // - name: limit - // in: query - // description: limit size of results - // type: integer // - 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": // description: "SearchResults of a successful search" @@ -615,8 +655,7 @@ func SearchTeam(ctx *context.APIContext) { Keyword: strings.TrimSpace(ctx.Query("q")), OrgID: ctx.Org.Organization.ID, IncludeDesc: (ctx.Query("include_desc") == "" || ctx.QueryBool("include_desc")), - PageSize: ctx.QueryInt("limit"), - Page: ctx.QueryInt("page"), + ListOptions: utils.GetListOptions(ctx), } teams, _, err := models.SearchTeam(opts) diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index aec389ab31..e99bc7b621 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -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 diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index d8777eaf3a..f7da1698dc 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -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) } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 0bf7fc6cef..3536b7f43a 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -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 diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 7fd7cd1be3..7fc5680c4f 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -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 diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index c480f2a46f..c400ec6f3c 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -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) } diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index 6b0e388194..6b7c2beac4 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -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) diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index 9c1322b3fe..5e49ea4aae 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -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 diff --git a/routers/api/v1/repo/issue_stopwatch.go b/routers/api/v1/repo/issue_stopwatch.go index 3b7c20d4d3..01faa0d6b1 100644 --- a/routers/api/v1/repo/issue_stopwatch.go +++ b/routers/api/v1/repo/issue_stopwatch.go @@ -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 diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index 153b01de61..274da966fd 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -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 diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go index 323bf67d8e..9bad19a2e7 100644 --- a/routers/api/v1/repo/issue_tracked_time.go +++ b/routers/api/v1/repo/issue_tracked_time.go @@ -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 { diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index 4ced8ec42f..4039100e1f 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -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 { diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 16c905878d..6d438610be 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -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 diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index a979e2b69b..f3bb4386cb 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -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 diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index e2e6c2799b..1abc2806f8 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -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) } diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index f94d6ba635..5a04fbeec2 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -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: per_page + // in: query + // description: items count every page wants to load + // type: integer + // deprecated: true // - name: page // in: query - // description: page wants to load + // description: page number of results to return (1-based) // type: integer - // - name: per_page + // - name: limit // in: query - // description: items count every page wants to load + // 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 diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index a13f6ebe0d..61c522bc05 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -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, diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 65a99d442a..c68f8f2744 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -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 diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index b6b3d495ca..6ce71cc911 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -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)) diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index 352f842884..d3cd8ccc3b 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -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 diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index 0c56f2a769..530b92a10d 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -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) diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index ec52f02d38..a65bdc51c1 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -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 diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index bd68070265..6e180b37f1 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -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" diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index b6133ca7be..a4f6d0e924 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -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 diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 7cf6fa383d..58705dd2f1 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -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" diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 90518f95e5..4024bf96cf 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -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" diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index e5d3a8f0a0..0e0875f04e 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -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) } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 3f17a6f91b..c0b0f1170c 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -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) diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index ec8543dcf0..e54709b599 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -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) } diff --git a/routers/api/v1/utils/utils.go b/routers/api/v1/utils/utils.go index 35f4873964..e02406d137 100644 --- a/routers/api/v1/utils/utils.go +++ b/routers/api/v1/utils/utils.go @@ -8,7 +8,9 @@ import ( "strings" "time" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" ) // UserID user ID of authenticated user, or 0 if not authenticated @@ -44,3 +46,11 @@ func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err erro } return before, since, nil } + +// GetListOptions returns list options using the page and limit parameters +func GetListOptions(ctx *context.APIContext) models.ListOptions { + return models.ListOptions{ + Page: ctx.QueryInt("page"), + PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), + } +} |