Skip to content

Commit 427b8a8

Browse files
committed
added gitea endpoint to get and list tags
1 parent a014bb1 commit 427b8a8

File tree

6 files changed

+118
-8
lines changed

6 files changed

+118
-8
lines changed

scm/driver/gitea/git.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Com
3232
}
3333

3434
func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
35-
return nil, nil, scm.ErrNotSupported
35+
name = scm.TrimRef(name)
36+
path := fmt.Sprintf("api/v1/repos/%s/git/refs/tags/%s", repo, url.PathEscape(name))
37+
out := []*tag{}
38+
res, err := s.client.do(ctx, "GET", path, nil, &out)
39+
if err != nil {
40+
return nil, res, err
41+
}
42+
for _, tag := range convertTagList(out) {
43+
if tag.Name == name {
44+
return tag, res, nil
45+
}
46+
}
47+
return nil, res, scm.ErrNotFound
3648
}
3749

3850
func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
@@ -47,7 +59,10 @@ func (s *gitService) ListCommits(ctx context.Context, repo string, _ scm.CommitL
4759
}
4860

4961
func (s *gitService) ListTags(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
50-
return nil, nil, scm.ErrNotSupported
62+
path := fmt.Sprintf("api/v1/repos/%s/git/refs/tags", repo)
63+
out := []*tag{}
64+
res, err := s.client.do(ctx, "GET", path, nil, &out)
65+
return convertTagList(out), res, err
5166
}
5267

5368
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
@@ -94,6 +109,17 @@ type (
94109
Email string `json:"email"`
95110
Username string `json:"username"`
96111
}
112+
113+
// gitea tag object
114+
tag struct {
115+
Ref string `json:"ref"`
116+
URL string `json:"url"`
117+
Object struct {
118+
Type string `json:"type"`
119+
Sha string `json:"sha"`
120+
URL string `json:"url"`
121+
} `json:"object"`
122+
}
97123
)
98124

99125
//
@@ -150,3 +176,19 @@ func convertUserSignature(src user) scm.Signature {
150176
Avatar: src.Avatar,
151177
}
152178
}
179+
180+
func convertTagList(src []*tag) []*scm.Reference {
181+
var dst []*scm.Reference
182+
for _, v := range src {
183+
dst = append(dst, convertTag(v))
184+
}
185+
return dst
186+
}
187+
188+
func convertTag(src *tag) *scm.Reference {
189+
return &scm.Reference{
190+
Name: scm.TrimRef(src.Ref),
191+
Path: src.Ref,
192+
Sha: src.Object.Sha,
193+
}
194+
}

scm/driver/gitea/git_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,53 @@ func TestGitListBranches(t *testing.T) {
135135
//
136136

137137
func TestGitFindTag(t *testing.T) {
138+
defer gock.Off()
139+
140+
gock.New("https://try.gitea.io").
141+
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0").
142+
Reply(200).
143+
Type("application/json").
144+
File("testdata/tag.json")
145+
138146
client, _ := New("https://try.gitea.io")
139-
_, _, err := client.Git.FindTag(context.Background(), "go-gitea/gitea", "v1.0.0")
140-
if err != scm.ErrNotSupported {
141-
t.Errorf("Expect Not Supported error")
147+
got, _, err := client.Git.FindTag(context.Background(), "go-gitea/gitea", "v1.0.0")
148+
if err != nil {
149+
t.Error(err)
150+
return
151+
}
152+
153+
want := &scm.Reference{}
154+
raw, _ := ioutil.ReadFile("testdata/tag.json.golden")
155+
json.Unmarshal(raw, &want)
156+
157+
if diff := cmp.Diff(got, want); diff != "" {
158+
t.Errorf("Unexpected Results")
159+
t.Log(diff)
142160
}
143161
}
144162

145163
func TestGitListTags(t *testing.T) {
164+
defer gock.Off()
165+
166+
gock.New("https://try.gitea.io").
167+
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags").
168+
Reply(200).
169+
Type("application/json").
170+
File("testdata/tags.json")
171+
146172
client, _ := New("https://try.gitea.io")
147-
_, _, err := client.Git.ListTags(context.Background(), "go-gitea/gitea", scm.ListOptions{})
148-
if err != scm.ErrNotSupported {
149-
t.Errorf("Expect Not Supported error")
173+
got, _, err := client.Git.ListTags(context.Background(), "go-gitea/gitea", scm.ListOptions{})
174+
if err != nil {
175+
t.Error(err)
176+
return
177+
}
178+
179+
want := []*scm.Reference{}
180+
raw, _ := ioutil.ReadFile("testdata/tags.json.golden")
181+
json.Unmarshal(raw, &want)
182+
183+
if diff := cmp.Diff(got, want); diff != "" {
184+
t.Errorf("Unexpected Results")
185+
t.Log(diff)
150186
}
151187
}

scm/driver/gitea/testdata/tag.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"ref": "refs/tags/v1.0.0",
4+
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0",
5+
"object": {
6+
"type": "commit",
7+
"sha": "4b736a01b6291e21c663ae9aab494850e7a50723",
8+
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/commits/4b736a01b6291e21c663ae9aab494850e7a50723"
9+
}
10+
}
11+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Name": "v1.0.0",
3+
"Path": "refs/tags/v1.0.0",
4+
"Sha": "4b736a01b6291e21c663ae9aab494850e7a50723"
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"ref": "refs/tags/v1.0.0",
4+
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0",
5+
"object": {
6+
"type": "commit",
7+
"sha": "4b736a01b6291e21c663ae9aab494850e7a50723",
8+
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/commits/4b736a01b6291e21c663ae9aab494850e7a50723"
9+
}
10+
}
11+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[{
2+
"Name": "v1.0.0",
3+
"Path": "refs/tags/v1.0.0",
4+
"Sha": "4b736a01b6291e21c663ae9aab494850e7a50723"
5+
}]

0 commit comments

Comments
 (0)