新增:标签分页以及最后一次commit信息

This commit is contained in:
yystopf 2022-11-28 11:55:46 +08:00
parent 72f4095786
commit 317f9691f6
4 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package convert
import (
"strings"
"code.gitea.io/gitea/models/repo"
gitea_convert "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
)
func ToTag(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (tag *api.Tag, err error) {
tagCommit, err := ToTagCommit(repo, gitRepo, t)
if err != nil {
return &api.Tag{}, err
}
return &api.Tag{
Name: t.Name,
Message: strings.TrimSpace(t.Message),
ID: t.ID.String(),
Commit: tagCommit,
Tagger: gitea_convert.ToCommitUser(t.Tagger),
ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
}, nil
}
func ToTagCommit(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (result *api.TagCommit, err error) {
commit, err := t.Commit(gitRepo)
if err != nil {
return &api.TagCommit{}, nil
}
return &api.TagCommit{
CommitMeta: gitea_convert.ToCommitMeta(repo, t),
Committer: gitea_convert.ToCommitUser(commit.Committer),
Author: gitea_convert.ToCommitUser(commit.Author),
Message: commit.CommitMessage,
}, nil
}

View File

@ -1,5 +1,26 @@
package structs
import (
gitea_api "code.gitea.io/gitea/modules/structs"
)
type BranchNameSet struct {
BranchName []*string `json:"branch_name"`
}
type Tag struct {
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 *gitea_api.CommitUser `json:"tagger"`
}
type TagCommit struct {
*gitea_api.CommitMeta
Committer *gitea_api.CommitUser `json:"committer"`
Author *gitea_api.CommitUser `json:"author"`
Message string `json:"message"`
}

View File

@ -61,6 +61,9 @@ func Routers() *web.Route {
m.Group("/branches", func() {
m.Get("/branches_slice", context.ReferencesGitRepo(), repo.ListBranchesSlice)
}, reqRepoReader(unit_model.TypeCode))
m.Group("/tags", func() {
m.Get("", repo.ListTags)
}, reqRepoReader(unit_model.TypeCode), context.ReferencesGitRepo(true))
m.Group("/wikies", func() {
m.Combo("").Get(repo.ListWikiPages).
Post(bind(hat_api.WikiOption{}), repo.CreateWiki)

View File

@ -1,13 +1,49 @@
package repo
import (
"math"
"net/http"
"strconv"
"strings"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
)
func ListTags(ctx *context.APIContext) {
listOpts := utils.GetListOptions(ctx)
tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTags", err)
return
}
apiTags := make([]*structs.Tag, len(tags))
for i := range tags {
convertTag, err := convert.ToTag(ctx.Repo.Repository, ctx.Repo.GitRepo, tags[i])
if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToTag", err)
return
}
apiTags[i] = convertTag
}
pageCount := int(math.Ceil(float64(total) / float64(listOpts.PageSize)))
ctx.RespHeader().Set("X-Page", strconv.Itoa(listOpts.Page))
ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOpts.PageSize))
ctx.RespHeader().Set("X-Total", strconv.Itoa(total))
ctx.RespHeader().Set("X-PageCount", strconv.Itoa(pageCount))
ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOpts.Page < pageCount))
ctx.SetLinkHeader(total, listOpts.PageSize)
ctx.SetTotalCountHeader(int64(total))
ctx.JSON(http.StatusOK, &apiTags)
}
func BranchNameSet(ctx *context.APIContext) {
searchName := ctx.Params("name")