From 8cc1b15245f06145c267f59146f4cb74c6330a1b Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Wed, 10 Jun 2020 11:17:25 +0200 Subject: [PATCH 1/4] Rework api/user/repos for pagination --- models/repo.go | 9 ++++-- models/user.go | 2 +- routers/api/v1/user/repo.go | 63 ++++++++++++++++++++++++------------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/models/repo.go b/models/repo.go index bf7bf018a196c..b5064df7b83b9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1774,7 +1774,7 @@ 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" } @@ -1786,10 +1786,13 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, error) { sess.And("is_private=?", false) } - sess = opts.setSessionPagination(sess) + count, err := sess.Count(new(Repository)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } 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. diff --git a/models/user.go b/models/user.go index 0ecb1b9a48e9a..8056e30cea374 100644 --- a/models/user.go +++ b/models/user.go @@ -646,7 +646,7 @@ func (u *User) GetOrganizationCount() (int64, error) { // GetRepositories returns repositories that user owns, including private repositories. func (u *User) GetRepositories(listOpts ListOptions) (err error) { - u.Repos, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts}) + u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts}) return err } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 887d606998cf7..b689121012f53 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -5,6 +5,7 @@ package user import ( + "fmt" "net/http" "code.gitea.io/gitea/models" @@ -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", fmt.Sprintf("%d", count)) ctx.JSON(http.StatusOK, &apiRepos) } @@ -92,31 +98,46 @@ 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.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: err.Error(), + }) return } - apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap)) - for i := range ownRepos { - apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner) - } - i := len(ownRepos) - for repo, access := range accessibleReposMap { - apiRepos[i] = repo.APIFormat(access) - i++ + results := make([]*api.Repository, len(repos)) + for i, repo := range repos { + if err = repo.GetOwner(); err != nil { + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: err.Error(), + }) + return + } + accessMode, err := models.AccessLevel(ctx.User, repo) + if err != nil { + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: err.Error(), + }) + } + results[i] = repo.APIFormat(accessMode) } - ctx.JSON(http.StatusOK, &apiRepos) + + ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize) + ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count)) + ctx.JSON(http.StatusOK, &results) } // ListOrgRepos - list the repositories of an organization. From 92b47eb0ba6677dfaf6d1adcf6acffd599b3166e Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Wed, 10 Jun 2020 11:21:44 +0200 Subject: [PATCH 2/4] but use old errors please --- routers/api/v1/user/repo.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index b689121012f53..2d9becaae5b13 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -109,28 +109,19 @@ func ListMyRepos(ctx *context.APIContext) { var err error repos, count, err := models.SearchRepository(opts) if err != nil { - ctx.JSON(http.StatusInternalServerError, api.SearchError{ - OK: false, - Error: err.Error(), - }) + ctx.Error(http.StatusInternalServerError, "SearchRepository", err) return } results := make([]*api.Repository, len(repos)) for i, repo := range repos { if err = repo.GetOwner(); err != nil { - ctx.JSON(http.StatusInternalServerError, api.SearchError{ - OK: false, - Error: err.Error(), - }) + ctx.Error(http.StatusInternalServerError, "GetOwner", err) return } accessMode, err := models.AccessLevel(ctx.User, repo) if err != nil { - ctx.JSON(http.StatusInternalServerError, api.SearchError{ - OK: false, - Error: err.Error(), - }) + ctx.Error(http.StatusInternalServerError, "AccessLevel", err) } results[i] = repo.APIFormat(accessMode) } From e94a519901f53e3becfdc6b83a64c0611c483872 Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Wed, 10 Jun 2020 13:33:13 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Lauris BH --- routers/api/v1/user/repo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 2d9becaae5b13..93d1377ec9a31 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -41,7 +41,7 @@ func listUserRepos(ctx *context.APIContext, u *models.User, private bool) { } ctx.SetLinkHeader(int(count), opts.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count)) + ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10)) ctx.JSON(http.StatusOK, &apiRepos) } @@ -127,7 +127,7 @@ func ListMyRepos(ctx *context.APIContext) { } ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize) - ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count)) + ctx.Header().Set("X-Total-Count", strconv.FormatInt(count, 10)) ctx.JSON(http.StatusOK, &results) } From d0795e50a1c81c0810282d541e1242ccfcbda307 Mon Sep 17 00:00:00 2001 From: CirnoT <1447794+CirnoT@users.noreply.github.com> Date: Wed, 10 Jun 2020 14:22:27 +0200 Subject: [PATCH 4/4] fix GetUserRepositories cond --- models/repo.go | 14 +++++++++----- routers/api/v1/user/repo.go | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/models/repo.go b/models/repo.go index b5064df7b83b9..3b874f3359af6 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 ( @@ -1779,18 +1780,21 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error) 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}) } - count, err := sess.Count(new(Repository)) + 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, count, opts.setSessionPagination(sess).Find(&repos) } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 93d1377ec9a31..519a999a98d7f 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -5,8 +5,8 @@ package user import ( - "fmt" "net/http" + "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context"