add: tags list page info

This commit is contained in:
yystopf 2021-09-30 16:09:11 +08:00
parent d78a760be7
commit 15882844c6
4 changed files with 70 additions and 0 deletions
modules/git
routers/api/v1
repo
swagger
templates/swagger

View File

@ -186,6 +186,16 @@ func (repo *Repository) GetTag(name string) (*Tag, error) {
return tag, nil
}
func (repo *Repository) GetTagCount() (int64, error) {
stdout, err := NewCommand("tag").RunInDir(repo.Path)
if err != nil {
return 0, err
}
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
return int64(len(tagNames)), nil
}
// GetTagInfos returns all tag infos of the repository.
func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, error) {
// TODO this a slow implementation, makes one git command per tag

View File

@ -5,7 +5,10 @@
package repo
import (
"fmt"
"math"
"net/http"
"strconv"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
@ -56,6 +59,22 @@ func ListTags(ctx *context.APIContext) {
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
}
tagsCountTotal, err := ctx.Repo.GitRepo.GetTagCount()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTagCount", err)
return
}
pageCount := int(math.Ceil(float64(tagsCountTotal) / float64(listOpts.PageSize)))
ctx.Header().Set("X-Page", strconv.Itoa(listOpts.Page))
ctx.Header().Set("X-PerPage", strconv.Itoa(listOpts.PageSize))
ctx.Header().Set("X-Total", strconv.FormatInt(tagsCountTotal, 10))
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOpts.Page < pageCount))
ctx.SetLinkHeader(int(tagsCountTotal), listOpts.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", tagsCountTotal))
ctx.JSON(http.StatusOK, &apiTags)
}

View File

@ -53,6 +53,21 @@ type swaggerResponseBranchProtectionList struct {
// TagList
// swagger:response TagList
type swaggerResponseTagList struct {
// The current page
Page int `json:"X-Page"`
// Commits per page
PerPage int `json:"X-PerPage"`
// Total commit count
Total int `json:"X-Total"`
// Total number of pages
PageCount int `json:"X-PageCount"`
// True if there is another page
HasMore bool `json:"X-HasMore"`
// in:body
Body []api.Tag `json:"body"`
}

View File

@ -16379,6 +16379,32 @@
"items": {
"$ref": "#/definitions/Tag"
}
},
"headers": {
"X-HasMore": {
"type": "boolean",
"description": "True if there is another page"
},
"X-Page": {
"type": "integer",
"format": "int64",
"description": "The current page"
},
"X-PageCount": {
"type": "integer",
"format": "int64",
"description": "Total number of pages"
},
"X-PerPage": {
"type": "integer",
"format": "int64",
"description": "Commits per page"
},
"X-Total": {
"type": "integer",
"format": "int64",
"description": "Total commit count"
}
}
},
"Team": {