Skip to content

Commit

Permalink
use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersBy…
Browse files Browse the repository at this point in the history
…OrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors
  • Loading branch information
hilariocoelho committed Dec 22, 2019
1 parent d0f7898 commit 3d7fd5f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions models/list_options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -30,8 +29,8 @@ func (opts ListOptions) setEnginePagination(e Engine) Engine {
}

func (opts ListOptions) setDefaultValues() {
if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum {
opts.PageSize = setting.UI.ExplorePagingNum
if opts.PageSize <= 0 || opts.PageSize > ItemsPerPage {
opts.PageSize = ItemsPerPage
}
if opts.Page <= 0 {
opts.Page = 1
Expand Down
2 changes: 1 addition & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func GetOrgUsersByOrgID(opts *FindOrgMembersOpts) ([]*OrgUser, error) {
}

func getOrgUsersByOrgID(e Engine, opts *FindOrgMembersOpts) ([]*OrgUser, error) {
ous := make([]*OrgUser, 0, 10)
var ous []*OrgUser

This comment has been minimized.

Copy link
@zeripath

zeripath Dec 22, 2019

Contributor

Sorry, That's not what I meant. You should still preallocate the array - it's a lot more efficient to do so. Just preallocate it to the size of the page.

sess := e.Where("org_id=?", opts.OrgID)
if opts.PublicOnly {
sess.And("is_public = ?", true)
Expand Down
2 changes: 1 addition & 1 deletion models/repo_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func GetRepoWatchersIDs(repoID int64) ([]int64, error) {

// GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(opts ListOptions) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage)
var users []*User
sess := x.Where("watch.repo_id=?", repo.ID).
Join("LEFT", "watch", "`user`.id=`watch`.user_id").
And("`watch`.mode<>?", RepoWatchModeDont)
Expand Down
2 changes: 1 addition & 1 deletion models/repo_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestRepository_GetWatchers(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watchers, err := repo.GetWatchers(ListOptions{Page: 1})
watchers, err := repo.GetWatchers(ListOptions{Page: 1, PageSize: ItemsPerPage})
assert.NoError(t, err)
assert.Len(t, watchers, repo.NumWatches)
for _, watcher := range watchers {
Expand Down
2 changes: 1 addition & 1 deletion models/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func isStaring(e Engine, userID, repoID int64) bool {

// GetStargazers returns the users that starred the repo.
func (repo *Repository) GetStargazers(opts ListOptions) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage)
var users []*User
sess := x.Where("star.repo_id = ?", repo.ID).
Join("LEFT", "star", "`user`.id = star.uid")
if opts.Page > 0 {
Expand Down
8 changes: 4 additions & 4 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func GetAllCommits(ctx *context.APIContext) {
}

pageSize := ctx.QueryInt("limit")
if pageSize <= 0 {
if pageSize <= 0 || pageSize > git.CommitsRangeSize {
pageSize = git.CommitsRangeSize
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func GetAllCommits(ctx *context.APIContext) {
return
}

pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(git.CommitsRangeSize)))
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(pageSize)))

// Query commits
commits, err := baseCommit.CommitsByRange(page, pageSize)
Expand All @@ -190,10 +190,10 @@ func GetAllCommits(ctx *context.APIContext) {
i++
}

ctx.SetLinkHeader(int(commitsCountTotal), git.CommitsRangeSize)
ctx.SetLinkHeader(int(commitsCountTotal), pageSize)

ctx.Header().Set("X-Page", strconv.Itoa(page))
ctx.Header().Set("X-PerPage", strconv.Itoa(git.CommitsRangeSize))
ctx.Header().Set("X-PerPage", strconv.Itoa(pageSize))
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount))
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Search(ctx *context.APIContext) {
Keyword: strings.Trim(ctx.Query("q"), " "),
UID: com.StrTo(ctx.Query("uid")).MustInt64(),
Type: models.UserTypeIndividual,
ListOptions: models.ListOptions{PageSize: ctx.QueryInt("page")},
ListOptions: models.ListOptions{Page: 1, PageSize: ctx.QueryInt("limit")},
}

users, _, err := models.SearchUsers(opts)
Expand Down
7 changes: 7 additions & 0 deletions routers/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func Releases(ctx *context.Context) {
IncludeTags: true,
}

if opts.ListOptions.Page <= 1 {
opts.ListOptions.Page = 1
}
if opts.ListOptions.PageSize <= 0 {
opts.ListOptions.Page = 10
}

releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts)
if err != nil {
ctx.ServerError("GetReleasesByRepoID", err)
Expand Down

0 comments on commit 3d7fd5f

Please sign in to comment.