aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorCirno the Strongest <1447794+CirnoT@users.noreply.github.com>2020-06-13 13:35:59 +0200
committerGitHub <noreply@github.com>2020-06-13 12:35:59 +0100
commit0159851cc3fa80e4df4908a5e760afa20452f712 (patch)
tree8f920c6cb3744939c5c9b9e633749cc5a54ab0ab /models/repo.go
parent2447ffc74a6e3ab57d98a52de0a7398cadb71e08 (diff)
downloadgitea-0159851cc3fa80e4df4908a5e760afa20452f712.tar.gz
gitea-0159851cc3fa80e4df4908a5e760afa20452f712.zip
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>
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go21
1 files changed, 14 insertions, 7 deletions
diff --git a/models/repo.go b/models/repo.go
index bf7bf018a1..3b874f3359 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -35,6 +35,7 @@ import (
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
+ "xorm.io/builder"
)
var (
@@ -1774,22 +1775,28 @@ func GetRepositoriesMapByIDs(ids []int64) (map[int64]*Repository, error) {
}
// GetUserRepositories returns a list of repositories of given user.
-func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, error) {
+func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC"
}
- sess := x.
- Where("owner_id = ?", opts.Actor.ID).
- OrderBy(opts.OrderBy.String())
+ var cond = builder.NewCond()
+ cond = cond.And(builder.Eq{"owner_id": opts.Actor.ID})
if !opts.Private {
- sess.And("is_private=?", false)
+ cond = cond.And(builder.Eq{"is_private": false})
}
- sess = opts.setSessionPagination(sess)
+ sess := x.NewSession()
+ defer sess.Close()
+
+ count, err := sess.Where(cond).Count(new(Repository))
+ if err != nil {
+ return nil, 0, fmt.Errorf("Count: %v", err)
+ }
+ sess.Where(cond).OrderBy(opts.OrderBy.String())
repos := make([]*Repository, 0, opts.PageSize)
- return repos, opts.setSessionPagination(sess).Find(&repos)
+ return repos, count, opts.setSessionPagination(sess).Find(&repos)
}
// GetUserMirrorRepositories returns a list of mirror repositories of given user.