Skip to content

Commit 0159851

Browse files
CirnoTlafriks
andauthored
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>
1 parent 2447ffc commit 0159851

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

Diff for: models/repo.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"code.gitea.io/gitea/modules/util"
3636

3737
"github.com/unknwon/com"
38+
"xorm.io/builder"
3839
)
3940

4041
var (
@@ -1774,22 +1775,28 @@ func GetRepositoriesMapByIDs(ids []int64) (map[int64]*Repository, error) {
17741775
}
17751776

17761777
// GetUserRepositories returns a list of repositories of given user.
1777-
func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, error) {
1778+
func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error) {
17781779
if len(opts.OrderBy) == 0 {
17791780
opts.OrderBy = "updated_unix DESC"
17801781
}
17811782

1782-
sess := x.
1783-
Where("owner_id = ?", opts.Actor.ID).
1784-
OrderBy(opts.OrderBy.String())
1783+
var cond = builder.NewCond()
1784+
cond = cond.And(builder.Eq{"owner_id": opts.Actor.ID})
17851785
if !opts.Private {
1786-
sess.And("is_private=?", false)
1786+
cond = cond.And(builder.Eq{"is_private": false})
17871787
}
17881788

1789-
sess = opts.setSessionPagination(sess)
1789+
sess := x.NewSession()
1790+
defer sess.Close()
1791+
1792+
count, err := sess.Where(cond).Count(new(Repository))
1793+
if err != nil {
1794+
return nil, 0, fmt.Errorf("Count: %v", err)
1795+
}
17901796

1797+
sess.Where(cond).OrderBy(opts.OrderBy.String())
17911798
repos := make([]*Repository, 0, opts.PageSize)
1792-
return repos, opts.setSessionPagination(sess).Find(&repos)
1799+
return repos, count, opts.setSessionPagination(sess).Find(&repos)
17931800
}
17941801

17951802
// GetUserMirrorRepositories returns a list of mirror repositories of given user.

Diff for: models/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ func (u *User) GetOrganizationCount() (int64, error) {
646646

647647
// GetRepositories returns repositories that user owns, including private repositories.
648648
func (u *User) GetRepositories(listOpts ListOptions) (err error) {
649-
u.Repos, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts})
649+
u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts})
650650
return err
651651
}
652652

Diff for: routers/api/v1/user/repo.go

+33-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66

77
import (
88
"net/http"
9+
"strconv"
910

1011
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/context"
@@ -15,10 +16,12 @@ import (
1516

1617
// listUserRepos - List the repositories owned by the given user.
1718
func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
18-
repos, err := models.GetUserRepositories(&models.SearchRepoOptions{
19+
opts := utils.GetListOptions(ctx)
20+
21+
repos, count, err := models.GetUserRepositories(&models.SearchRepoOptions{
1922
Actor: u,
2023
Private: private,
21-
ListOptions: utils.GetListOptions(ctx),
24+
ListOptions: opts,
2225
})
2326
if err != nil {
2427
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
@@ -36,6 +39,9 @@ func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
3639
apiRepos = append(apiRepos, repos[i].APIFormat(access))
3740
}
3841
}
42+
43+
ctx.SetLinkHeader(int(count), opts.PageSize)
44+
ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10))
3945
ctx.JSON(http.StatusOK, &apiRepos)
4046
}
4147

@@ -92,31 +98,37 @@ func ListMyRepos(ctx *context.APIContext) {
9298
// "200":
9399
// "$ref": "#/responses/RepositoryList"
94100

95-
ownRepos, err := models.GetUserRepositories(&models.SearchRepoOptions{
96-
Actor: ctx.User,
97-
Private: true,
98-
ListOptions: utils.GetListOptions(ctx),
99-
})
100-
if err != nil {
101-
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
102-
return
101+
opts := &models.SearchRepoOptions{
102+
ListOptions: utils.GetListOptions(ctx),
103+
Actor: ctx.User,
104+
OwnerID: ctx.User.ID,
105+
Private: ctx.IsSigned,
106+
IncludeDescription: true,
103107
}
104-
accessibleReposMap, err := ctx.User.GetRepositoryAccesses()
108+
109+
var err error
110+
repos, count, err := models.SearchRepository(opts)
105111
if err != nil {
106-
ctx.Error(http.StatusInternalServerError, "GetRepositoryAccesses", err)
112+
ctx.Error(http.StatusInternalServerError, "SearchRepository", err)
107113
return
108114
}
109115

110-
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap))
111-
for i := range ownRepos {
112-
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
113-
}
114-
i := len(ownRepos)
115-
for repo, access := range accessibleReposMap {
116-
apiRepos[i] = repo.APIFormat(access)
117-
i++
116+
results := make([]*api.Repository, len(repos))
117+
for i, repo := range repos {
118+
if err = repo.GetOwner(); err != nil {
119+
ctx.Error(http.StatusInternalServerError, "GetOwner", err)
120+
return
121+
}
122+
accessMode, err := models.AccessLevel(ctx.User, repo)
123+
if err != nil {
124+
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
125+
}
126+
results[i] = repo.APIFormat(accessMode)
118127
}
119-
ctx.JSON(http.StatusOK, &apiRepos)
128+
129+
ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize)
130+
ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10))
131+
ctx.JSON(http.StatusOK, &results)
120132
}
121133

122134
// ListOrgRepos - list the repositories of an organization.

0 commit comments

Comments
 (0)