mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
[API] generalize list header (#16551)
* Add info about list endpoints to CONTRIBUTING.md * Let all list endpoints return X-Total-Count header * Add TODOs for GetCombinedCommitStatusByRef * Fix models/issue_stopwatch.go * Rrefactor models.ListDeployKeys * Introduce helper func and use them for SetLinkHeader related func
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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))
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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))
|
||||
}
|
||||
|
@@ -75,26 +75,29 @@ func ListDeployKeys(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/DeployKeyList"
|
||||
|
||||
var keys []*models.DeployKey
|
||||
var err error
|
||||
|
||||
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))
|
||||
opts := &models.ListDeployKeysOptions{
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
KeyID: ctx.FormInt64("key_id"),
|
||||
Fingerprint: ctx.FormString("fingerprint"),
|
||||
}
|
||||
|
||||
keys, err := models.ListDeployKeys(opts)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ListDeployKeys", err)
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
count, err := models.CountDeployKeys(opts)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
|
@@ -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))
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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.Error(http.StatusInternalServerError, "FindReviews", err)
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
count, err := models.CountReviews(opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -95,6 +102,7 @@ func ListPullReviews(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(count)
|
||||
ctx.JSON(http.StatusOK, &apiReviews)
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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,
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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,
|
||||
})
|
||||
|
Reference in New Issue
Block a user