From d60d9cf9857d1e7a7915b62851b254a5304b6cdc Mon Sep 17 00:00:00 2001 From: Devops <943927833@qq.com> Date: Tue, 14 Dec 2021 20:41:12 +0800 Subject: api: support listing repository tags (#6656) Co-authored-by: zhouzhibo Co-authored-by: Joe Chen --- internal/route/api/v1/api.go | 1 + internal/route/api/v1/convert/convert.go | 12 ++++++++++++ internal/route/api/v1/repo/tag.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 internal/route/api/v1/repo/tag.go (limited to 'internal/route') diff --git a/internal/route/api/v1/api.go b/internal/route/api/v1/api.go index a662f55f..62c933ad 100644 --- a/internal/route/api/v1/api.go +++ b/internal/route/api/v1/api.go @@ -280,6 +280,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/:sha", repo.GetRepoGitTree) }) m.Get("/forks", repo.ListForks) + m.Get("/tags", repo.ListTags) m.Group("/branches", func() { m.Get("", repo.ListBranches) m.Get("/*", repo.GetBranch) diff --git a/internal/route/api/v1/convert/convert.go b/internal/route/api/v1/convert/convert.go index a374efc1..bf5aabbd 100644 --- a/internal/route/api/v1/convert/convert.go +++ b/internal/route/api/v1/convert/convert.go @@ -30,6 +30,18 @@ func ToBranch(b *db.Branch, c *git.Commit) *api.Branch { } } +type Tag struct { + Name string `json:"name"` + Commit *api.PayloadCommit `json:"commit"` +} + +func ToTag(b *db.Tag, c *git.Commit) *Tag { + return &Tag{ + Name: b.Name, + Commit: ToCommit(c), + } +} + func ToCommit(c *git.Commit) *api.PayloadCommit { authorUsername := "" author, err := db.GetUserByEmail(c.Author.Email) diff --git a/internal/route/api/v1/repo/tag.go b/internal/route/api/v1/repo/tag.go new file mode 100644 index 00000000..73b7e064 --- /dev/null +++ b/internal/route/api/v1/repo/tag.go @@ -0,0 +1,30 @@ +// Copyright 2021 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "gogs.io/gogs/internal/context" + "gogs.io/gogs/internal/route/api/v1/convert" +) + +func ListTags(c *context.APIContext) { + tags, err := c.Repo.Repository.GetTags() + if err != nil { + c.Error(err, "get tags") + return + } + + apiTags := make([]*convert.Tag, len(tags)) + for i := range tags { + commit, err := tags[i].GetCommit() + if err != nil { + c.Error(err, "get commit") + return + } + apiTags[i] = convert.ToTag(tags[i], commit) + } + + c.JSONSuccess(&apiTags) +} -- cgit v1.2.3