Skip to content

Commit 4427a93

Browse files
6543guillep2k
andauthored
[API] enable paggination for ListRepoTags (#10454)
* enable paggination for repoTags * precalculate first, cut slice second * Apply suggestions from code review Co-Authored-By: guillep2k <[email protected]> Co-authored-by: guillep2k <[email protected]>
1 parent b098cc2 commit 4427a93

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

modules/git/repo_tag.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,26 @@ func (repo *Repository) GetTag(name string) (*Tag, error) {
187187
}
188188

189189
// GetTagInfos returns all tag infos of the repository.
190-
func (repo *Repository) GetTagInfos() ([]*Tag, error) {
190+
func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, error) {
191191
// TODO this a slow implementation, makes one git command per tag
192192
stdout, err := NewCommand("tag").RunInDir(repo.Path)
193193
if err != nil {
194194
return nil, err
195195
}
196196

197197
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
198+
199+
if page != 0 {
200+
skip := (page - 1) * pageSize
201+
if skip >= len(tagNames) {
202+
return nil, nil
203+
}
204+
if (len(tagNames) - skip) < pageSize {
205+
pageSize = len(tagNames) - skip
206+
}
207+
tagNames = tagNames[skip : skip+pageSize]
208+
}
209+
198210
var tags = make([]*Tag, 0, len(tagNames))
199211
for _, tagName := range tagNames {
200212
tagName = strings.TrimSpace(tagName)

modules/git/repo_tag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestRepository_GetTags(t *testing.T) {
1818
assert.NoError(t, err)
1919
defer bareRepo1.Close()
2020

21-
tags, err := bareRepo1.GetTagInfos()
21+
tags, err := bareRepo1.GetTagInfos(0, 0)
2222
assert.NoError(t, err)
2323
assert.Len(t, tags, 1)
2424
assert.EqualValues(t, "test", tags[0].Name)

routers/api/v1/repo/tag.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/modules/context"
1111
"code.gitea.io/gitea/modules/convert"
1212
api "code.gitea.io/gitea/modules/structs"
13+
"code.gitea.io/gitea/routers/api/v1/utils"
1314
)
1415

1516
// ListTags list all the tags of a repository
@@ -30,11 +31,21 @@ func ListTags(ctx *context.APIContext) {
3031
// description: name of the repo
3132
// type: string
3233
// required: true
34+
// - name: page
35+
// in: query
36+
// description: page number of results to return (1-based)
37+
// type: integer
38+
// - name: limit
39+
// in: query
40+
// description: page size of results, default maximum page size is 50
41+
// type: integer
3342
// responses:
3443
// "200":
3544
// "$ref": "#/responses/TagList"
3645

37-
tags, err := ctx.Repo.GitRepo.GetTagInfos()
46+
listOpts := utils.GetListOptions(ctx)
47+
48+
tags, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
3849
if err != nil {
3950
ctx.Error(http.StatusInternalServerError, "GetTags", err)
4051
return

templates/swagger/v1_json.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7251,6 +7251,18 @@
72517251
"name": "repo",
72527252
"in": "path",
72537253
"required": true
7254+
},
7255+
{
7256+
"type": "integer",
7257+
"description": "page number of results to return (1-based)",
7258+
"name": "page",
7259+
"in": "query"
7260+
},
7261+
{
7262+
"type": "integer",
7263+
"description": "page size of results, default maximum page size is 50",
7264+
"name": "limit",
7265+
"in": "query"
72547266
}
72557267
],
72567268
"responses": {

0 commit comments

Comments
 (0)