From 317f9691f60b4dabe89a183c92119b5ebb32679c Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 28 Nov 2022 11:55:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E4=BB=A5=E5=8F=8A=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E6=AC=A1commit=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/convert/convert.go | 41 +++++++++++++++++++++++++++++++++++++ modules/structs/repo_tag.go | 21 +++++++++++++++++++ routers/hat/hat.go | 3 +++ routers/hat/repo/tag.go | 36 ++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 modules/convert/convert.go diff --git a/modules/convert/convert.go b/modules/convert/convert.go new file mode 100644 index 0000000..029d323 --- /dev/null +++ b/modules/convert/convert.go @@ -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 +} diff --git a/modules/structs/repo_tag.go b/modules/structs/repo_tag.go index 251aa00..64fdff2 100644 --- a/modules/structs/repo_tag.go +++ b/modules/structs/repo_tag.go @@ -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"` +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 161040c..b13e257 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -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) diff --git a/routers/hat/repo/tag.go b/routers/hat/repo/tag.go index e58cc78..01ece2a 100644 --- a/routers/hat/repo/tag.go +++ b/routers/hat/repo/tag.go @@ -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")