Skip to content

Commit 01bbf5e

Browse files
lunnyzeripath
authored andcommitted
Add API to list tags (#5850)
* Add API to list tags * update dependency gitea sdk vendor * fix swagger generation * fix swagger * add tests * update code.gitea.io/git vendor
1 parent 2d213b6 commit 01bbf5e

File tree

9 files changed

+216
-14
lines changed

9 files changed

+216
-14
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integrations/api_repo_tags_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"path"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/setting"
14+
api "code.gitea.io/sdk/gitea"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestAPIReposGetTags(t *testing.T) {
20+
prepareTestEnv(t)
21+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
22+
// Login as User2.
23+
session := loginUser(t, user.Name)
24+
token := getTokenForLoggedInUser(t, session)
25+
26+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/tags?token="+token, user.Name)
27+
resp := session.MakeRequest(t, req, http.StatusOK)
28+
29+
var tags []*api.Tag
30+
DecodeJSON(t, resp, &tags)
31+
32+
assert.EqualValues(t, 1, len(tags))
33+
assert.Equal(t, "v1.1", tags[0].Name)
34+
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", tags[0].Commit.SHA)
35+
assert.Equal(t, path.Join(setting.AppSubURL, "/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d"), tags[0].Commit.URL)
36+
assert.Equal(t, path.Join(setting.AppSubURL, "/user2/repo1/archive/v1.1.zip"), tags[0].ZipballURL)
37+
assert.Equal(t, path.Join(setting.AppSubURL, "/user2/repo1/archive/v1.1.tar.gz"), tags[0].TarballURL)
38+
}

models/repo_tag.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"code.gitea.io/git"
9+
)
10+
11+
// GetTagsByPath returns repo tags by it's path
12+
func GetTagsByPath(path string) ([]*git.Tag, error) {
13+
gitRepo, err := git.OpenRepository(path)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
return gitRepo.GetTagInfos()
19+
}
20+
21+
// GetTags return repo's tags
22+
func (repo *Repository) GetTags() ([]*git.Tag, error) {
23+
return GetTagsByPath(repo.RepoPath())
24+
}

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ func RegisterRoutes(m *macaron.Macaron) {
513513
m.Get("", repo.ListBranches)
514514
m.Get("/*", context.RepoRefByType(context.RepoRefBranch), repo.GetBranch)
515515
}, reqRepoReader(models.UnitTypeCode))
516+
m.Group("/tags", func() {
517+
m.Get("", repo.ListTags)
518+
}, reqRepoReader(models.UnitTypeCode))
516519
m.Group("/keys", func() {
517520
m.Combo("").Get(repo.ListDeployKeys).
518521
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)

routers/api/v1/convert/convert.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ func ToBranch(repo *models.Repository, b *models.Branch, c *git.Commit) *api.Bra
3434
}
3535
}
3636

37+
// ToTag convert a tag to an api.Tag
38+
func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
39+
return &api.Tag{
40+
Name: t.Name,
41+
Commit: struct {
42+
SHA string `json:"sha"`
43+
URL string `json:"url"`
44+
}{
45+
SHA: t.ID.String(),
46+
URL: util.URLJoin(repo.Link(), "commit", t.ID.String()),
47+
},
48+
ZipballURL: util.URLJoin(repo.Link(), "archive", t.Name+".zip"),
49+
TarballURL: util.URLJoin(repo.Link(), "archive", t.Name+".tar.gz"),
50+
}
51+
}
52+
3753
// ToCommit convert a commit to api.PayloadCommit
3854
func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
3955
authorUsername := ""

routers/api/v1/repo/tag.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
"code.gitea.io/gitea/modules/context"
9+
"code.gitea.io/gitea/routers/api/v1/convert"
10+
11+
api "code.gitea.io/sdk/gitea"
12+
)
13+
14+
// ListTags list all the tags of a repository
15+
func ListTags(ctx *context.APIContext) {
16+
// swagger:operation GET /repos/{owner}/{repo}/tags repository repoListTags
17+
// ---
18+
// summary: List a repository's tags
19+
// produces:
20+
// - application/json
21+
// parameters:
22+
// - name: owner
23+
// in: path
24+
// description: owner of the repo
25+
// type: string
26+
// required: true
27+
// - name: repo
28+
// in: path
29+
// description: name of the repo
30+
// type: string
31+
// required: true
32+
// responses:
33+
// "200":
34+
// "$ref": "#/responses/TagList"
35+
tags, err := ctx.Repo.Repository.GetTags()
36+
if err != nil {
37+
ctx.Error(500, "GetTags", err)
38+
return
39+
}
40+
41+
apiTags := make([]*api.Tag, len(tags))
42+
for i := range tags {
43+
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
44+
}
45+
46+
ctx.JSON(200, &apiTags)
47+
}

routers/api/v1/swagger/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ type swaggerResponseBranchList struct {
3636
Body []api.Branch `json:"body"`
3737
}
3838

39+
// TagList
40+
// swagger:response TagList
41+
type swaggerReponseTagList struct {
42+
// in:body
43+
Body []api.Tag `json:"body"`
44+
}
45+
3946
// Reference
4047
// swagger:response Reference
4148
type swaggerResponseReference struct {

templates/swagger/v1_json.tmpl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,6 +4681,39 @@
46814681
}
46824682
}
46834683
},
4684+
"/repos/{owner}/{repo}/tags": {
4685+
"get": {
4686+
"produces": [
4687+
"application/json"
4688+
],
4689+
"tags": [
4690+
"repository"
4691+
],
4692+
"summary": "List a repository's tags",
4693+
"operationId": "repoListTags",
4694+
"parameters": [
4695+
{
4696+
"type": "string",
4697+
"description": "owner of the repo",
4698+
"name": "owner",
4699+
"in": "path",
4700+
"required": true
4701+
},
4702+
{
4703+
"type": "string",
4704+
"description": "name of the repo",
4705+
"name": "repo",
4706+
"in": "path",
4707+
"required": true
4708+
}
4709+
],
4710+
"responses": {
4711+
"200": {
4712+
"$ref": "#/responses/TagList"
4713+
}
4714+
}
4715+
}
4716+
},
46844717
"/repos/{owner}/{repo}/times": {
46854718
"get": {
46864719
"produces": [
@@ -8419,6 +8452,39 @@
84198452
"type": "string",
84208453
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
84218454
},
8455+
"Tag": {
8456+
"description": "Tag represents a repository tag",
8457+
"type": "object",
8458+
"properties": {
8459+
"commit": {
8460+
"type": "object",
8461+
"properties": {
8462+
"sha": {
8463+
"type": "string",
8464+
"x-go-name": "SHA"
8465+
},
8466+
"url": {
8467+
"type": "string",
8468+
"x-go-name": "URL"
8469+
}
8470+
},
8471+
"x-go-name": "Commit"
8472+
},
8473+
"name": {
8474+
"type": "string",
8475+
"x-go-name": "Name"
8476+
},
8477+
"tarball_url": {
8478+
"type": "string",
8479+
"x-go-name": "TarballURL"
8480+
},
8481+
"zipball_url": {
8482+
"type": "string",
8483+
"x-go-name": "ZipballURL"
8484+
}
8485+
},
8486+
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
8487+
},
84228488
"Team": {
84238489
"description": "Team represents a team in an organization",
84248490
"type": "object",
@@ -8898,6 +8964,15 @@
88988964
}
88998965
}
89008966
},
8967+
"TagList": {
8968+
"description": "TagList",
8969+
"schema": {
8970+
"type": "array",
8971+
"items": {
8972+
"$ref": "#/definitions/Tag"
8973+
}
8974+
}
8975+
},
89018976
"Team": {
89028977
"description": "Team",
89038978
"schema": {

vendor/code.gitea.io/git/repo_tag.go

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)