Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination on releases page #2035

Merged
merged 10 commits into from
Jun 28, 2017
14 changes: 14 additions & 0 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/sdk/gitea"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
)

Expand Down Expand Up @@ -244,6 +245,19 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
return rels, err
}

// GetReleaseCountByRepoID returns the count of releases of repository
func GetReleaseCountByRepoID(repoID int64, includeDrafts bool) (int64, error) {
var cond = builder.NewCond()
cond = cond.And(builder.Eq{"repo_id": repoID})

if includeDrafts {
return x.Where(cond).Count(&Release{})
}

cond = cond.And(builder.Eq{"is_draft": false})
return x.Where(cond).Count(&Release{})
}

// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
err = x.
Expand Down
10 changes: 8 additions & 2 deletions routers/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func Releases(ctx *context.Context) {

releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit)
Copy link
Member

@ethantkoenig ethantkoenig Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make sure that if we're not the owner, then this doesn't return drafts. I know that we check in the for loop below, but right now it's possible that we end up displaying fewer than limit releases on the first page, even if there are more than limit displayable releases in total.

EDIT: Oh, looks like @lafriks already pointed this out 😂

if err != nil {
ctx.Handle(500, "GetReleasesByRepoIDAndNames", err)
ctx.Handle(500, "GetReleasesByRepoID", err)
return
}

count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, ctx.Repo.IsOwner())
if err != nil {
ctx.Handle(500, "GetReleaseCountByRepoID", err)
return
}

Expand Down Expand Up @@ -110,7 +116,7 @@ func Releases(ctx *context.Context) {
releasesToDisplay = append(releasesToDisplay, r)
}

pager := paginater.New(len(releasesToDisplay), limit, page, 5)
pager := paginater.New(int(count), limit, page, 5)
ctx.Data["Page"] = pager
ctx.Data["Releases"] = releasesToDisplay
ctx.HTML(200, tplReleases)
Expand Down