Rework api/user/repos for pagination (#11827)
* Add count to `GetUserRepositories` so that pagination can be supported for `/user/{username}/repos` * Rework ListMyRepos to use models.SearchRepository ListMyRepos was an odd one. It first fetched all user repositories and then tried to supplement them with accessible map. The end result was that: * Limit for pagination did not work because accessible repos would always be appended * The amount of pages was incorrect if one were to calculate it * When paginating, all accessible repos would be shown on every page Hopefully it should now work properly. Fixes #11800 and does not require any change on Drone-side as it can properly interpret and act on Link header which we now set. Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
2447ffc74a
commit
0159851cc3
3 changed files with 48 additions and 29 deletions
|
@ -6,6 +6,7 @@ package user
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
|
@ -15,10 +16,12 @@ import (
|
|||
|
||||
// listUserRepos - List the repositories owned by the given user.
|
||||
func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
|
||||
repos, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
||||
opts := utils.GetListOptions(ctx)
|
||||
|
||||
repos, count, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
||||
Actor: u,
|
||||
Private: private,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
ListOptions: opts,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
||||
|
@ -36,6 +39,9 @@ func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
|
|||
apiRepos = append(apiRepos, repos[i].APIFormat(access))
|
||||
}
|
||||
}
|
||||
|
||||
ctx.SetLinkHeader(int(count), opts.PageSize)
|
||||
ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10))
|
||||
ctx.JSON(http.StatusOK, &apiRepos)
|
||||
}
|
||||
|
||||
|
@ -92,31 +98,37 @@ func ListMyRepos(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
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
|
||||
opts := &models.SearchRepoOptions{
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
Actor: ctx.User,
|
||||
OwnerID: ctx.User.ID,
|
||||
Private: ctx.IsSigned,
|
||||
IncludeDescription: true,
|
||||
}
|
||||
accessibleReposMap, err := ctx.User.GetRepositoryAccesses()
|
||||
|
||||
var err error
|
||||
repos, count, err := models.SearchRepository(opts)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetRepositoryAccesses", err)
|
||||
ctx.Error(http.StatusInternalServerError, "SearchRepository", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap))
|
||||
for i := range ownRepos {
|
||||
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
|
||||
results := make([]*api.Repository, len(repos))
|
||||
for i, repo := range repos {
|
||||
if err = repo.GetOwner(); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetOwner", err)
|
||||
return
|
||||
}
|
||||
accessMode, err := models.AccessLevel(ctx.User, repo)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
|
||||
}
|
||||
results[i] = repo.APIFormat(accessMode)
|
||||
}
|
||||
i := len(ownRepos)
|
||||
for repo, access := range accessibleReposMap {
|
||||
apiRepos[i] = repo.APIFormat(access)
|
||||
i++
|
||||
}
|
||||
ctx.JSON(http.StatusOK, &apiRepos)
|
||||
|
||||
ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize)
|
||||
ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10))
|
||||
ctx.JSON(http.StatusOK, &results)
|
||||
}
|
||||
|
||||
// ListOrgRepos - list the repositories of an organization.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue