diff options
Diffstat (limited to 'routers')
48 files changed, 316 insertions, 154 deletions
diff --git a/routers/api/v1/admin/adopt.go b/routers/api/v1/admin/adopt.go index 062cee6283..9c1b9fc0f8 100644 --- a/routers/api/v1/admin/adopt.go +++ b/routers/api/v1/admin/adopt.go @@ -5,7 +5,6 @@ package admin import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -47,8 +46,7 @@ func ListUnadoptedRepositories(ctx *context.APIContext) { ctx.InternalServerError(err) } - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count") + ctx.SetTotalCountHeader(int64(count)) ctx.JSON(http.StatusOK, repoNames) } diff --git a/routers/api/v1/admin/cron.go b/routers/api/v1/admin/cron.go index 2531346fcb..970fad8388 100644 --- a/routers/api/v1/admin/cron.go +++ b/routers/api/v1/admin/cron.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -36,12 +37,10 @@ func ListCronTasks(ctx *context.APIContext) { // "403": // "$ref": "#/responses/forbidden" tasks := cron.ListTasks() - listOpts := utils.GetListOptions(ctx) - start, end := listOpts.GetStartEnd() + count := len(tasks) - if len(tasks) > listOpts.PageSize { - tasks = tasks[start:end] - } + listOpts := utils.GetListOptions(ctx) + tasks = util.PaginateSlice(tasks, listOpts.Page, listOpts.PageSize).(cron.TaskTable) res := make([]structs.Cron, len(tasks)) for i, task := range tasks { @@ -53,6 +52,8 @@ func ListCronTasks(ctx *context.APIContext) { ExecTimes: task.ExecTimes, } } + + ctx.SetTotalCountHeader(int64(count)) ctx.JSON(http.StatusOK, res) } diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go index 1356276f07..f1a766d544 100644 --- a/routers/api/v1/admin/org.go +++ b/routers/api/v1/admin/org.go @@ -6,7 +6,6 @@ package admin import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -121,7 +120,6 @@ func GetAllOrgs(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, &orgs) } diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 6bc9b849b1..e5a75da759 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -423,7 +423,6 @@ func GetAllUsers(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, &results) } diff --git a/routers/api/v1/notify/repo.go b/routers/api/v1/notify/repo.go index af4507f896..1a36642b6a 100644 --- a/routers/api/v1/notify/repo.go +++ b/routers/api/v1/notify/repo.go @@ -108,6 +108,12 @@ func ListRepoNotifications(ctx *context.APIContext) { } opts.RepoID = ctx.Repo.Repository.ID + totalCount, err := models.CountNotifications(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + nl, err := models.GetNotifications(opts) if err != nil { ctx.InternalServerError(err) @@ -119,6 +125,8 @@ func ListRepoNotifications(ctx *context.APIContext) { return } + ctx.SetTotalCountHeader(totalCount) + ctx.JSON(http.StatusOK, convert.ToNotifications(nl)) } diff --git a/routers/api/v1/notify/user.go b/routers/api/v1/notify/user.go index c2178b4dee..e4626cb719 100644 --- a/routers/api/v1/notify/user.go +++ b/routers/api/v1/notify/user.go @@ -68,6 +68,12 @@ func ListNotifications(ctx *context.APIContext) { return } + totalCount, err := models.CountNotifications(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + nl, err := models.GetNotifications(opts) if err != nil { ctx.InternalServerError(err) @@ -79,6 +85,7 @@ func ListNotifications(ctx *context.APIContext) { return } + ctx.SetTotalCountHeader(totalCount) ctx.JSON(http.StatusOK, convert.ToNotifications(nl)) } diff --git a/routers/api/v1/org/hook.go b/routers/api/v1/org/hook.go index ed827c48d4..c5982300eb 100644 --- a/routers/api/v1/org/hook.go +++ b/routers/api/v1/org/hook.go @@ -40,16 +40,29 @@ func ListHooks(ctx *context.APIContext) { // "200": // "$ref": "#/responses/HookList" - org := ctx.Org.Organization - orgHooks, err := models.GetWebhooksByOrgID(org.ID, utils.GetListOptions(ctx)) + opts := &models.ListWebhookOptions{ + ListOptions: utils.GetListOptions(ctx), + OrgID: ctx.Org.Organization.ID, + } + + count, err := models.CountWebhooksByOpts(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetWebhooksByOrgID", err) + ctx.InternalServerError(err) return } + + orgHooks, err := models.ListWebhooksByOpts(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + hooks := make([]*api.Hook, len(orgHooks)) for i, hook := range orgHooks { - hooks[i] = convert.ToHook(org.HomeLink(), hook) + hooks[i] = convert.ToHook(ctx.Org.Organization.HomeLink(), hook) } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, hooks) } diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index c70252158e..09acb0bf04 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -49,6 +49,13 @@ func ListLabels(ctx *context.APIContext) { return } + count, err := models.CountLabelsByOrgID(ctx.Org.Organization.ID) + if err != nil { + ctx.InternalServerError(err) + return + } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) } diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 09abad2557..97940d5925 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -18,15 +18,21 @@ import ( // listMembers list an organization's members func listMembers(ctx *context.APIContext, publicOnly bool) { - var members []*models.User - - members, _, err := models.FindOrgMembers(&models.FindOrgMembersOpts{ + opts := &models.FindOrgMembersOpts{ OrgID: ctx.Org.Organization.ID, PublicOnly: publicOnly, ListOptions: utils.GetListOptions(ctx), - }) + } + + count, err := models.CountOrgMembers(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + + members, _, err := models.FindOrgMembers(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err) + ctx.InternalServerError(err) return } @@ -35,6 +41,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) { apiMembers[i] = convert.ToUser(member, ctx.User) } + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, apiMembers) } diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 5c16594f89..39ce896cd6 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -6,7 +6,6 @@ package org import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -38,9 +37,8 @@ func listUserOrgs(ctx *context.APIContext, u *models.User) { apiOrgs[i] = convert.ToOrganization(orgs[i]) } - ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetLinkHeader(maxResults, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(maxResults)) ctx.JSON(http.StatusOK, &apiOrgs) } @@ -145,8 +143,7 @@ func GetAll(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, &orgs) } diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 7998bb249f..a84763d6f6 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -6,7 +6,6 @@ package org import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -44,23 +43,27 @@ func ListTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - org := ctx.Org.Organization - if err := org.GetTeams(&models.SearchTeamOptions{ + teams, count, err := models.SearchTeam(&models.SearchTeamOptions{ ListOptions: utils.GetListOptions(ctx), - }); err != nil { - ctx.Error(http.StatusInternalServerError, "GetTeams", err) + OrgID: ctx.Org.Organization.ID, + }) + + if err != nil { + ctx.Error(http.StatusInternalServerError, "LoadTeams", err) return } - apiTeams := make([]*api.Team, len(org.Teams)) - for i := range org.Teams { - if err := org.Teams[i].GetUnits(); err != nil { + apiTeams := make([]*api.Team, len(teams)) + for i := range teams { + if err := teams[i].GetUnits(); err != nil { ctx.Error(http.StatusInternalServerError, "GetUnits", err) return } - apiTeams[i] = convert.ToTeam(org.Teams[i]) + apiTeams[i] = convert.ToTeam(teams[i]) } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, apiTeams) } @@ -84,7 +87,10 @@ func ListUserTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - teams, err := models.GetUserTeams(ctx.User.ID, utils.GetListOptions(ctx)) + teams, count, err := models.SearchTeam(&models.SearchTeamOptions{ + ListOptions: utils.GetListOptions(ctx), + UserID: ctx.User.ID, + }) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserTeams", err) return @@ -106,6 +112,8 @@ func ListUserTeams(ctx *context.APIContext) { apiTeams[i] = convert.ToTeam(teams[i]) apiTeams[i].Organization = apiOrg } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, apiTeams) } @@ -327,17 +335,19 @@ func GetTeamMembers(ctx *context.APIContext) { ctx.NotFound() return } - team := ctx.Org.Team - if err := team.GetMembers(&models.SearchMembersOptions{ + + if err := ctx.Org.Team.GetMembers(&models.SearchMembersOptions{ ListOptions: utils.GetListOptions(ctx), }); err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamMembers", err) return } - members := make([]*api.User, len(team.Members)) - for i, member := range team.Members { + members := make([]*api.User, len(ctx.Org.Team.Members)) + for i, member := range ctx.Org.Team.Members { members[i] = convert.ToUser(member, ctx.User) } + + ctx.SetTotalCountHeader(int64(ctx.Org.Team.NumMembers)) ctx.JSON(http.StatusOK, members) } @@ -687,8 +697,7 @@ func SearchTeam(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, map[string]interface{}{ "ok": true, "data": apiTeams, diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 85c1681dfe..8653b0bc80 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -282,9 +282,8 @@ func ListBranches(ctx *context.APIContext) { } } - ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalNumOfBranches)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(totalNumOfBranches)) ctx.JSON(http.StatusOK, &apiBranches) } diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index 078af1f6ff..d636220f62 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -47,15 +47,24 @@ func ListCollaborators(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" + count, err := ctx.Repo.Repository.CountCollaborators() + if err != nil { + ctx.InternalServerError(err) + return + } + collaborators, err := ctx.Repo.Repository.GetCollaborators(utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "ListCollaborators", err) return } + users := make([]*api.User, len(collaborators)) for i, collaborator := range collaborators { users[i] = convert.ToUser(collaborator.User, ctx.User) } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index f2fd901efa..975b9cab2a 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -200,16 +200,16 @@ func GetAllCommits(ctx *context.APIContext) { } } + ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize) + ctx.SetTotalCountHeader(commitsCountTotal) + // kept for backwards compatibility 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(listOptions.Page < pageCount)) - - ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link") + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-Total", "X-PageCount", "X-HasMore") ctx.JSON(http.StatusOK, &apiCommits) } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 55fcefaccc..a3f9aa14f9 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -62,6 +62,8 @@ func ListForks(ctx *context.APIContext) { } apiForks[i] = convert.ToRepo(fork, access) } + + ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumForks)) ctx.JSON(http.StatusOK, apiForks) } diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index da0a2c501c..74860fd72f 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -48,9 +48,20 @@ func ListHooks(ctx *context.APIContext) { // "200": // "$ref": "#/responses/HookList" - hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) + opts := &models.ListWebhookOptions{ + ListOptions: utils.GetListOptions(ctx), + RepoID: ctx.Repo.Repository.ID, + } + + count, err := models.CountWebhooksByOpts(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetWebhooksByRepoID", err) + ctx.InternalServerError(err) + return + } + + hooks, err := models.ListWebhooksByOpts(opts) + if err != nil { + ctx.InternalServerError(err) return } @@ -58,6 +69,8 @@ func ListHooks(ctx *context.APIContext) { for i := range hooks { apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &apiHooks) } diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 7395d4fdd4..ff003b840b 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -232,8 +232,7 @@ func SearchIssues(ctx *context.APIContext) { } ctx.SetLinkHeader(int(filteredCount), setting.UI.IssuePagingNum) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", filteredCount)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(filteredCount) ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues)) } @@ -442,8 +441,7 @@ func ListIssues(ctx *context.APIContext) { } ctx.SetLinkHeader(int(filteredCount), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", filteredCount)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(filteredCount) ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues)) } diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index d62ca81314..13e7de46b1 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -68,17 +68,25 @@ func ListIssueComments(ctx *context.APIContext) { } issue.Repo = ctx.Repo.Repository - comments, err := models.FindComments(models.FindCommentsOptions{ + opts := &models.FindCommentsOptions{ IssueID: issue.ID, Since: since, Before: before, Type: models.CommentTypeComment, - }) + } + + comments, err := models.FindComments(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "FindComments", err) return } + totalCount, err := models.CountComments(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + if err := models.CommentList(comments).LoadPosters(); err != nil { ctx.Error(http.StatusInternalServerError, "LoadPosters", err) return @@ -89,6 +97,8 @@ func ListIssueComments(ctx *context.APIContext) { comment.Issue = issue apiComments[i] = convert.ToComment(comments[i]) } + + ctx.SetTotalCountHeader(totalCount) ctx.JSON(http.StatusOK, &apiComments) } @@ -138,18 +148,26 @@ func ListRepoIssueComments(ctx *context.APIContext) { return } - comments, err := models.FindComments(models.FindCommentsOptions{ + opts := &models.FindCommentsOptions{ ListOptions: utils.GetListOptions(ctx), RepoID: ctx.Repo.Repository.ID, Type: models.CommentTypeComment, Since: since, Before: before, - }) + } + + comments, err := models.FindComments(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "FindComments", err) return } + totalCount, err := models.CountComments(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + if err = models.CommentList(comments).LoadPosters(); err != nil { ctx.Error(http.StatusInternalServerError, "LoadPosters", err) return @@ -171,6 +189,8 @@ func ListRepoIssueComments(ctx *context.APIContext) { for i := range comments { apiComments[i] = convert.ToComment(comments[i]) } + + ctx.SetTotalCountHeader(totalCount) ctx.JSON(http.StatusOK, &apiComments) } diff --git a/routers/api/v1/repo/issue_stopwatch.go b/routers/api/v1/repo/issue_stopwatch.go index a4a2261b9a..82a9ffe10b 100644 --- a/routers/api/v1/repo/issue_stopwatch.go +++ b/routers/api/v1/repo/issue_stopwatch.go @@ -225,11 +225,18 @@ func GetStopwatches(ctx *context.APIContext) { return } + count, err := models.CountUserStopwatches(ctx.User.ID) + if err != nil { + ctx.InternalServerError(err) + return + } + apiSWs, err := convert.ToStopWatches(sws) if err != nil { ctx.Error(http.StatusInternalServerError, "APIFormat", err) return } + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, apiSWs) } diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go index b27b746eb2..e9d8fbab26 100644 --- a/routers/api/v1/repo/issue_tracked_time.go +++ b/routers/api/v1/repo/issue_tracked_time.go @@ -83,7 +83,7 @@ func ListTrackedTimes(ctx *context.APIContext) { return } - opts := models.FindTrackedTimesOptions{ + opts := &models.FindTrackedTimesOptions{ ListOptions: utils.GetListOptions(ctx), RepositoryID: ctx.Repo.Repository.ID, IssueID: issue.ID, @@ -119,6 +119,12 @@ func ListTrackedTimes(ctx *context.APIContext) { } } + count, err := models.CountTrackedTimes(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + trackedTimes, err := models.GetTrackedTimes(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err) @@ -128,6 +134,8 @@ func ListTrackedTimes(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } @@ -423,7 +431,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) { return } - opts := models.FindTrackedTimesOptions{ + opts := &models.FindTrackedTimesOptions{ UserID: user.ID, RepositoryID: ctx.Repo.Repository.ID, } @@ -493,7 +501,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { return } - opts := models.FindTrackedTimesOptions{ + opts := &models.FindTrackedTimesOptions{ ListOptions: utils.GetListOptions(ctx), RepositoryID: ctx.Repo.Repository.ID, } @@ -530,6 +538,12 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { } } + count, err := models.CountTrackedTimes(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + trackedTimes, err := models.GetTrackedTimes(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err) @@ -539,6 +553,8 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } @@ -573,7 +589,7 @@ func ListMyTrackedTimes(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TrackedTimeList" - opts := models.FindTrackedTimesOptions{ + opts := &models.FindTrackedTimesOptions{ ListOptions: utils.GetListOptions(ctx), UserID: ctx.User.ID, } @@ -584,6 +600,12 @@ func ListMyTrackedTimes(ctx *context.APIContext) { return } + count, err := models.CountTrackedTimes(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + trackedTimes, err := models.GetTrackedTimes(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByUser", err) @@ -595,5 +617,6 @@ func ListMyTrackedTimes(ctx *context.APIContext) { return } + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index 903cef7104..98ee2b4de5 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -75,26 +75,29 @@ func ListDeployKeys(ctx *context.APIContext) { // "200": // "$ref": "#/responses/DeployKeyList" - var keys []*models.DeployKey - var err error + opts := &models.ListDeployKeysOptions{ + ListOptions: utils.GetListOptions(ctx), + RepoID: ctx.Repo.Repository.ID, + KeyID: ctx.FormInt64("key_id"), + Fingerprint: ctx.FormString("fingerprint"), + } - fingerprint := ctx.FormString("fingerprint") - keyID := ctx.FormInt64("key_id") - if fingerprint != "" || keyID != 0 { - keys, err = models.SearchDeployKeys(ctx.Repo.Repository.ID, keyID, fingerprint) - } else { - keys, err = models.ListDeployKeys(ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) + keys, err := models.ListDeployKeys(opts) + if err != nil { + ctx.InternalServerError(err) + return } + count, err := models.CountDeployKeys(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "ListDeployKeys", err) + ctx.InternalServerError(err) return } apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) apiKeys := make([]*api.DeployKey, len(keys)) for i := range keys { - if err = keys[i].GetContent(); err != nil { + if err := keys[i].GetContent(); err != nil { ctx.Error(http.StatusInternalServerError, "GetContent", err) return } @@ -104,6 +107,7 @@ func ListDeployKeys(ctx *context.APIContext) { } } + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &apiKeys) } diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index ca0d8392b8..1de5705aa2 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -55,6 +55,13 @@ func ListLabels(ctx *context.APIContext) { return } + count, err := models.CountLabelsByRepoID(ctx.Repo.Repository.ID) + if err != nil { + ctx.InternalServerError(err) + return + } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) } diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index 07b3897bc1..be1da18c5d 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -57,7 +57,7 @@ func ListMilestones(ctx *context.APIContext) { // "200": // "$ref": "#/responses/MilestoneList" - milestones, err := models.GetMilestones(models.GetMilestonesOption{ + milestones, total, err := models.GetMilestones(models.GetMilestonesOption{ ListOptions: utils.GetListOptions(ctx), RepoID: ctx.Repo.Repository.ID, State: api.StateType(ctx.FormString("state")), @@ -72,6 +72,8 @@ func ListMilestones(ctx *context.APIContext) { for i := range milestones { apiMilestones[i] = convert.ToAPIMilestone(milestones[i]) } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, &apiMilestones) } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 9be6228bfd..0a903101c7 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -119,8 +119,7 @@ func ListPullRequests(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, &apiPrs) } @@ -1232,13 +1231,14 @@ func GetPullRequestCommits(ctx *context.APIContext) { apiCommits = append(apiCommits, apiCommit) } - ctx.SetLinkHeader(int(totalNumberOfCommits), listOptions.PageSize) + ctx.SetLinkHeader(totalNumberOfCommits, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(totalNumberOfCommits)) ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page)) ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalNumberOfCommits)) ctx.Header().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link") + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") + ctx.JSON(http.StatusOK, &apiCommits) } diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 323904f45c..55b5178305 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -78,14 +78,21 @@ func ListPullReviews(ctx *context.APIContext) { return } - allReviews, err := models.FindReviews(models.FindReviewOptions{ + opts := models.FindReviewOptions{ ListOptions: utils.GetListOptions(ctx), Type: models.ReviewTypeUnknown, IssueID: pr.IssueID, - }) + } + + allReviews, err := models.FindReviews(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + count, err := models.CountReviews(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "FindReviews", err) + ctx.InternalServerError(err) return } @@ -95,6 +102,7 @@ func ListPullReviews(ctx *context.APIContext) { return } + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &apiReviews) } diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 97b90079f2..6438c6e9b4 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -5,7 +5,6 @@ package repo import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -142,8 +141,7 @@ func ListReleases(ctx *context.APIContext) { } ctx.SetLinkHeader(int(filteredCount), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprint(filteredCount)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(filteredCount) ctx.JSON(http.StatusOK, rels) } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index d222c9b080..0f85d917d1 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -230,8 +230,7 @@ func Search(ctx *context.APIContext) { } ctx.SetLinkHeader(int(count), opts.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, api.SearchResults{ OK: true, Data: results, diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 3af0a4ac12..5fa42c3244 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -52,5 +52,7 @@ func ListStargazers(ctx *context.APIContext) { for i, stargazer := range stargazers { users[i] = convert.ToUser(stargazer, ctx.User) } + + ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumStars)) ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 841f60bb56..b884432f73 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -204,8 +204,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, apiStatuses) } @@ -267,5 +266,6 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { combiStatus := convert.ToCombinedStatus(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode)) + // TODO: ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, combiStatus) } diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index 37bf3c29d4..dae92969ba 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -52,5 +52,7 @@ func ListSubscribers(ctx *context.APIContext) { for i, subscriber := range subscribers { users[i] = convert.ToUser(subscriber, ctx.User) } + + ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumWatches)) ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index c95fb63f85..8d42e63a48 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -50,7 +50,7 @@ func ListTags(ctx *context.APIContext) { listOpts := utils.GetListOptions(ctx) - tags, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize) + tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTags", err) return @@ -61,6 +61,7 @@ func ListTags(ctx *context.APIContext) { apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i]) } + ctx.SetTotalCountHeader(int64(total)) ctx.JSON(http.StatusOK, &apiTags) } diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index a7c52e0bcc..fc277cb3fe 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -47,12 +47,13 @@ func ListTopics(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TopicNames" - topics, err := models.FindTopics(&models.FindTopicOptions{ + opts := &models.FindTopicOptions{ ListOptions: utils.GetListOptions(ctx), RepoID: ctx.Repo.Repository.ID, - }) + } + + topics, total, err := models.FindTopics(opts) if err != nil { - log.Error("ListTopics failed: %v", err) ctx.InternalServerError(err) return } @@ -61,6 +62,8 @@ func ListTopics(ctx *context.APIContext) { for i, topic := range topics { topicNames[i] = topic.Name } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, map[string]interface{}{ "topics": topicNames, }) @@ -164,15 +167,15 @@ func AddTopic(ctx *context.APIContext) { } // Prevent adding more topics than allowed to repo - topics, err := models.FindTopics(&models.FindTopicOptions{ + count, err := models.CountTopics(&models.FindTopicOptions{ RepoID: ctx.Repo.Repository.ID, }) if err != nil { - log.Error("AddTopic failed: %v", err) + log.Error("CountTopics failed: %v", err) ctx.InternalServerError(err) return } - if len(topics) >= 25 { + if count >= 25 { ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ "message": "Exceeding maximum allowed topics per repo.", }) @@ -269,21 +272,13 @@ func TopicSearch(ctx *context.APIContext) { // "403": // "$ref": "#/responses/forbidden" - if ctx.User == nil { - ctx.Error(http.StatusForbidden, "UserIsNil", "Only owners could change the topics.") - return + opts := &models.FindTopicOptions{ + Keyword: ctx.FormString("q"), + ListOptions: utils.GetListOptions(ctx), } - kw := ctx.FormString("q") - - listOptions := utils.GetListOptions(ctx) - - topics, err := models.FindTopics(&models.FindTopicOptions{ - Keyword: kw, - ListOptions: listOptions, - }) + topics, total, err := models.FindTopics(opts) if err != nil { - log.Error("SearchTopics failed: %v", err) ctx.InternalServerError(err) return } @@ -292,6 +287,8 @@ func TopicSearch(ctx *context.APIContext) { for i, topic := range topics { topicResponses[i] = convert.ToTopicResponse(topic) } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, map[string]interface{}{ "topics": topicResponses, }) diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index afd209f2f0..f0f7cb4159 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -44,9 +44,16 @@ func ListAccessTokens(ctx *context.APIContext) { // "200": // "$ref": "#/responses/AccessTokenList" - tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID, ListOptions: utils.GetListOptions(ctx)}) + opts := models.ListAccessTokensOptions{UserID: ctx.User.ID, ListOptions: utils.GetListOptions(ctx)} + + count, err := models.CountAccessTokens(opts) + if err != nil { + ctx.InternalServerError(err) + return + } + tokens, err := models.ListAccessTokens(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) + ctx.InternalServerError(err) return } @@ -58,6 +65,8 @@ func ListAccessTokens(ctx *context.APIContext) { TokenLastEight: tokens[i].TokenLastEight, } } + + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &apiTokens) } @@ -242,7 +251,7 @@ func ListOauth2Applications(ctx *context.APIContext) { // "200": // "$ref": "#/responses/OAuth2ApplicationList" - apps, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx)) + apps, total, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err) return @@ -253,6 +262,8 @@ func ListOauth2Applications(ctx *context.APIContext) { apiApps[i] = convert.ToOAuth2Application(apps[i]) apiApps[i].ClientSecret = "" // Hide secret on application list } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, &apiApps) } diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index 4d316425cd..e273ac6a02 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -29,6 +29,8 @@ func listUserFollowers(ctx *context.APIContext, u *models.User) { ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err) return } + + ctx.SetTotalCountHeader(int64(u.NumFollowers)) responseAPIUsers(ctx, users) } @@ -93,6 +95,8 @@ func listUserFollowing(ctx *context.APIContext, u *models.User) { ctx.Error(http.StatusInternalServerError, "GetFollowing", err) return } + + ctx.SetTotalCountHeader(int64(u.NumFollowing)) responseAPIUsers(ctx, users) } diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index ec03e305ba..f32d60d038 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -28,6 +28,13 @@ func listGPGKeys(ctx *context.APIContext, uid int64, listOptions models.ListOpti apiKeys[i] = convert.ToGPGKey(keys[i]) } + total, err := models.CountUserGPGKeys(uid) + if err != nil { + ctx.InternalServerError(err) + return + } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, &apiKeys) } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 04252524b7..36b1c17b89 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -47,6 +47,7 @@ func composePublicKeysAPILink() string { func listPublicKeys(ctx *context.APIContext, user *models.User) { var keys []*models.PublicKey var err error + var count int fingerprint := ctx.FormString("fingerprint") username := ctx.Params("username") @@ -60,7 +61,15 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { // Unrestricted keys, err = models.SearchPublicKey(0, fingerprint) } + count = len(keys) } else { + total, err2 := models.CountPublicKeys(user.ID) + if err2 != nil { + ctx.InternalServerError(err) + return + } + count = int(total) + // Use ListPublicKeys keys, err = models.ListPublicKeys(user.ID, utils.GetListOptions(ctx)) } @@ -79,6 +88,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { } } + ctx.SetTotalCountHeader(int64(count)) ctx.JSON(http.StatusOK, &apiKeys) } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 0331abef10..5116c17ab2 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -6,7 +6,6 @@ package user import ( "net/http" - "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -43,8 +42,7 @@ func listUserRepos(ctx *context.APIContext, u *models.User, private bool) { } ctx.SetLinkHeader(int(count), opts.PageSize) - ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &apiRepos) } @@ -130,8 +128,7 @@ func ListMyRepos(ctx *context.APIContext) { } ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize) - ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, &results) } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 937dcb477f..8ee1676856 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -92,6 +92,8 @@ func GetMyStarredRepos(ctx *context.APIContext) { if err != nil { ctx.Error(http.StatusInternalServerError, "getStarredRepos", err) } + + ctx.SetTotalCountHeader(int64(ctx.User.NumStars)) ctx.JSON(http.StatusOK, &repos) } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index e00c8d476d..a5e70de548 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -6,7 +6,6 @@ package user import ( - "fmt" "net/http" "code.gitea.io/gitea/models" @@ -73,8 +72,7 @@ func Search(ctx *context.APIContext) { } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults)) - ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") + ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, map[string]interface{}{ "ok": true, diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index ab656f3229..f32ce73598 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -14,23 +14,22 @@ import ( "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, listOptions models.ListOptions) ([]*api.Repository, error) { - watchedRepos, err := models.GetWatchedRepos(user.ID, private, listOptions) +// getWatchedRepos returns the repos that the user with the specified userID is watching +func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, int64, error) { + watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions) if err != nil { - return nil, err + return nil, 0, err } repos := make([]*api.Repository, len(watchedRepos)) for i, watched := range watchedRepos { access, err := models.AccessLevel(user, watched) if err != nil { - return nil, err + return nil, 0, err } repos[i] = convert.ToRepo(watched, access) } - return repos, nil + return repos, total, nil } // GetWatchedRepos returns the repos that the user specified in ctx is watching @@ -60,10 +59,12 @@ func GetWatchedRepos(ctx *context.APIContext) { user := GetUserByParams(ctx) private := user.ID == ctx.User.ID - repos, err := getWatchedRepos(user, private, utils.GetListOptions(ctx)) + repos, total, err := getWatchedRepos(user, private, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err) } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, &repos) } @@ -87,10 +88,12 @@ func GetMyWatchedRepos(ctx *context.APIContext) { // "200": // "$ref": "#/responses/RepositoryList" - repos, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx)) + repos, total, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err) } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, &repos) } diff --git a/routers/web/org/home.go b/routers/web/org/home.go index da53053c7f..0c1f381d30 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -107,7 +107,7 @@ func Home(ctx *context.Context) { return } - var opts = models.FindOrgMembersOpts{ + var opts = &models.FindOrgMembersOpts{ OrgID: org.ID, PublicOnly: true, ListOptions: models.ListOptions{Page: 1, PageSize: 25}, @@ -122,7 +122,7 @@ func Home(ctx *context.Context) { opts.PublicOnly = !isMember && !ctx.User.IsAdmin } - members, _, err := models.FindOrgMembers(&opts) + members, _, err := models.FindOrgMembers(opts) if err != nil { ctx.ServerError("FindOrgMembers", err) return diff --git a/routers/web/org/members.go b/routers/web/org/members.go index 2989637a61..84aaa28f60 100644 --- a/routers/web/org/members.go +++ b/routers/web/org/members.go @@ -31,7 +31,7 @@ func Members(ctx *context.Context) { page = 1 } - var opts = models.FindOrgMembersOpts{ + var opts = &models.FindOrgMembersOpts{ OrgID: org.ID, PublicOnly: true, } @@ -54,7 +54,7 @@ func Members(ctx *context.Context) { pager := context.NewPagination(int(total), setting.UI.MembersPagingNum, page, 5) opts.ListOptions.Page = page opts.ListOptions.PageSize = setting.UI.MembersPagingNum - members, membersIsPublic, err := models.FindOrgMembers(&opts) + members, membersIsPublic, err := models.FindOrgMembers(opts) if err != nil { ctx.ServerError("GetMembers", err) return diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go index b21b94d64d..7e6fc5bf4c 100644 --- a/routers/web/org/setting.go +++ b/routers/web/org/setting.go @@ -186,7 +186,7 @@ func Webhooks(ctx *context.Context) { ctx.Data["BaseLinkNew"] = ctx.Org.OrgLink + "/settings/hooks" ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc") - ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID, models.ListOptions{}) + ws, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{OrgID: ctx.Org.Organization.ID}) if err != nil { ctx.ServerError("GetWebhooksByOrgId", err) return diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 6050bb5c23..ff83be4410 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -378,7 +378,7 @@ func Issues(ctx *context.Context) { var err error // Get milestones - ctx.Data["Milestones"], err = models.GetMilestones(models.GetMilestonesOption{ + ctx.Data["Milestones"], _, err = models.GetMilestones(models.GetMilestonesOption{ RepoID: ctx.Repo.Repository.ID, State: api.StateType(ctx.FormString("state")), }) @@ -395,7 +395,7 @@ func Issues(ctx *context.Context) { // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) { var err error - ctx.Data["OpenMilestones"], err = models.GetMilestones(models.GetMilestonesOption{ + ctx.Data["OpenMilestones"], _, err = models.GetMilestones(models.GetMilestonesOption{ RepoID: repo.ID, State: api.StateOpen, }) @@ -403,7 +403,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos ctx.ServerError("GetMilestones", err) return } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(models.GetMilestonesOption{ + ctx.Data["ClosedMilestones"], _, err = models.GetMilestones(models.GetMilestonesOption{ RepoID: repo.ID, State: api.StateClosed, }) @@ -1265,7 +1265,7 @@ func ViewIssue(ctx *context.Context) { } else { ctx.Data["CanUseTimetracker"] = false } - if ctx.Data["WorkingUsers"], err = models.TotalTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil { + if ctx.Data["WorkingUsers"], err = models.TotalTimes(&models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil { ctx.ServerError("TotalTimes", err) return } @@ -2584,8 +2584,8 @@ func handleTeamMentions(ctx *context.Context) { } if isAdmin { - if err := ctx.Repo.Owner.GetTeams(&models.SearchTeamOptions{}); err != nil { - ctx.ServerError("GetTeams", err) + if err := ctx.Repo.Owner.LoadTeams(); err != nil { + ctx.ServerError("LoadTeams", err) return } } else { diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index ca77e0976e..3ba27dd19a 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -53,17 +53,12 @@ func Milestones(ctx *context.Context) { page = 1 } - var total int - var state structs.StateType - if !isShowClosed { - total = int(stats.OpenCount) - state = structs.StateOpen - } else { - total = int(stats.ClosedCount) + state := structs.StateOpen + if isShowClosed { state = structs.StateClosed } - miles, err := models.GetMilestones(models.GetMilestonesOption{ + miles, total, err := models.GetMilestones(models.GetMilestonesOption{ ListOptions: models.ListOptions{ Page: page, PageSize: setting.UI.IssuePagingNum, @@ -106,7 +101,7 @@ func Milestones(ctx *context.Context) { ctx.Data["Keyword"] = keyword ctx.Data["IsShowClosed"] = isShowClosed - pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5) + pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5) pager.AddParam(ctx, "state", "State") pager.AddParam(ctx, "q", "Keyword") ctx.Data["Page"] = pager diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 2f15610737..20253a7cae 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -1002,7 +1002,7 @@ func DeployKeys(ctx *context.Context) { ctx.Data["PageIsSettingsKeys"] = true ctx.Data["DisableSSH"] = setting.SSH.Disabled - keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID, models.ListOptions{}) + keys, err := models.ListDeployKeys(&models.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) if err != nil { ctx.ServerError("ListDeployKeys", err) return @@ -1018,7 +1018,7 @@ func DeployKeysPost(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys") ctx.Data["PageIsSettingsKeys"] = true - keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID, models.ListOptions{}) + keys, err := models.ListDeployKeys(&models.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) if err != nil { ctx.ServerError("ListDeployKeys", err) return diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 6c8645226f..2c703fc1af 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -674,7 +674,7 @@ func renderLanguageStats(ctx *context.Context) { } func renderRepoTopics(ctx *context.Context) { - topics, err := models.FindTopics(&models.FindTopicOptions{ + topics, _, err := models.FindTopics(&models.FindTopicOptions{ RepoID: ctx.Repo.Repository.ID, }) if err != nil { diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 79f47470f2..0ceb9bc1dc 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -41,7 +41,7 @@ func Webhooks(ctx *context.Context) { ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks" ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/") - ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, models.ListOptions{}) + ws, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID}) if err != nil { ctx.ServerError("GetWebhooksByRepoID", err) return |