1
1
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:
6543
2021-08-12 14:43:08 +02:00
committed by GitHub
parent ca13e1d56c
commit 2289580bb7
88 changed files with 637 additions and 329 deletions

View File

@@ -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.Error(http.StatusInternalServerError, "ListAccessTokens", err)
ctx.InternalServerError(err)
return
}
tokens, err := models.ListAccessTokens(opts)
if err != nil {
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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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,

View File

@@ -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)
}