Skip to content

Commit 3f90164

Browse files
iszlalunny
authored andcommitted
Pagination on releases page (#2035)
* Added count to GetReleasesByRepoID so pagination will work * Separated it out to a new function, can then also leave the API part unaffected * Remove extra whitespace added in untouched function * Added comment and corrected name in error handler * Account for if the user is owner or not in the count * Also check if repo is draft * revert back to the correct count in the ReleasesToDisplay loop * Fixed lint error regarding else with return statement * Use Cond struct instead of string, corrected name in error handler * Removed unused return variable names
1 parent 1f4d84b commit 3f90164

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

models/release.go

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/process"
1515
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/sdk/gitea"
17+
"github.com/go-xorm/builder"
1718
"github.com/go-xorm/xorm"
1819
)
1920

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

248+
// GetReleaseCountByRepoID returns the count of releases of repository
249+
func GetReleaseCountByRepoID(repoID int64, includeDrafts bool) (int64, error) {
250+
var cond = builder.NewCond()
251+
cond = cond.And(builder.Eq{"repo_id": repoID})
252+
253+
if includeDrafts {
254+
return x.Where(cond).Count(&Release{})
255+
}
256+
257+
cond = cond.And(builder.Eq{"is_draft": false})
258+
return x.Where(cond).Count(&Release{})
259+
}
260+
247261
// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
248262
func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
249263
err = x.

routers/repo/release.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ func Releases(ctx *context.Context) {
6767

6868
releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit)
6969
if err != nil {
70-
ctx.Handle(500, "GetReleasesByRepoIDAndNames", err)
70+
ctx.Handle(500, "GetReleasesByRepoID", err)
71+
return
72+
}
73+
74+
count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, ctx.Repo.IsOwner())
75+
if err != nil {
76+
ctx.Handle(500, "GetReleaseCountByRepoID", err)
7177
return
7278
}
7379

@@ -110,7 +116,7 @@ func Releases(ctx *context.Context) {
110116
releasesToDisplay = append(releasesToDisplay, r)
111117
}
112118

113-
pager := paginater.New(len(releasesToDisplay), limit, page, 5)
119+
pager := paginater.New(int(count), limit, page, 5)
114120
ctx.Data["Page"] = pager
115121
ctx.Data["Releases"] = releasesToDisplay
116122
ctx.HTML(200, tplReleases)

0 commit comments

Comments
 (0)