Merge pull request '[API]Fix tags' (#11) from wonderful/gitea-1156:develop into develop

This commit is contained in:
yystopf 2021-11-18 16:23:53 +08:00
commit 99c180cc3f
5 changed files with 91 additions and 8 deletions

View File

@ -168,12 +168,27 @@ func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
Name: t.Name,
Message: strings.TrimSpace(t.Message),
ID: t.ID.String(),
Commit: ToCommitMeta(repo, t),
Commit: ToTagCommit(repo, t),
Tagger: ToCommitUser(t.Tagger),
ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
}
}
func ToTagCommit(repo *models.Repository, t *git.Tag) *api.TagCommit {
commit, err := t.Commit()
if err != nil {
log.Error("Commit", err)
return &api.TagCommit{}
}
return &api.TagCommit{
CommitMeta: ToCommitMeta(repo, t),
Commiter: ToCommitUser(commit.Committer),
Author: ToCommitUser(commit.Author),
Message: commit.CommitMessage,
}
}
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
verif := models.ParseCommitWithSignature(c)

View File

@ -20,6 +20,16 @@ func IsTagExist(repoPath, name string) bool {
return IsReferenceExist(repoPath, TagPrefix+name)
}
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
}
// CreateTag create one tag in the repository
func (repo *Repository) CreateTag(name, revision string) error {
_, err := NewCommand("tag", "--", name, revision).RunInDir(repo.Path)

View File

@ -6,12 +6,21 @@ package structs
// Tag represents a repository tag
type Tag struct {
Name string `json:"name"`
Message string `json:"message"`
ID string `json:"id"`
Commit *CommitMeta `json:"commit"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Name string `json:"name"`
Message string `json:"message"`
ID string `json:"id"`
Commit *TagCommit `json:"commit"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Tagger *CommitUser `json:"tagger"`
}
type TagCommit struct {
*CommitMeta
Commiter *CommitUser `json:"commiter"`
Author *CommitUser `json:"author"`
Message string `json:"message"`
}
// AnnotatedTag represents an annotated tag

View File

@ -7,7 +7,9 @@ package repo
import (
"errors"
"fmt"
"math"
"net/http"
"strconv"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
@ -61,6 +63,21 @@ 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

@ -16962,7 +16962,7 @@
"type": "object",
"properties": {
"commit": {
"$ref": "#/definitions/CommitMeta"
"$ref": "#/definitions/TagCommit"
},
"id": {
"type": "string",
@ -16976,6 +16976,9 @@
"type": "string",
"x-go-name": "Name"
},
"tagger": {
"$ref": "#/definitions/CommitUser"
},
"tarball_url": {
"type": "string",
"x-go-name": "TarballURL"
@ -16987,6 +16990,35 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"TagCommit": {
"type": "object",
"properties": {
"author": {
"$ref": "#/definitions/CommitUser"
},
"commiter": {
"$ref": "#/definitions/CommitUser"
},
"created": {
"type": "string",
"format": "date-time",
"x-go-name": "Created"
},
"message": {
"type": "string",
"x-go-name": "Message"
},
"sha": {
"type": "string",
"x-go-name": "SHA"
},
"url": {
"type": "string",
"x-go-name": "URL"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Team": {
"description": "Team represents a team in an organization",
"type": "object",