Skip to content

Commit

Permalink
added gitea endpoint to get and list tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 28, 2020
1 parent a014bb1 commit 427b8a8
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
46 changes: 44 additions & 2 deletions scm/driver/gitea/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Com
}

func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
name = scm.TrimRef(name)
path := fmt.Sprintf("api/v1/repos/%s/git/refs/tags/%s", repo, url.PathEscape(name))
out := []*tag{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
if err != nil {
return nil, res, err
}
for _, tag := range convertTagList(out) {
if tag.Name == name {
return tag, res, nil
}
}
return nil, res, scm.ErrNotFound
}

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

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

func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
Expand Down Expand Up @@ -94,6 +109,17 @@ type (
Email string `json:"email"`
Username string `json:"username"`
}

// gitea tag object
tag struct {
Ref string `json:"ref"`
URL string `json:"url"`
Object struct {
Type string `json:"type"`
Sha string `json:"sha"`
URL string `json:"url"`
} `json:"object"`
}
)

//
Expand Down Expand Up @@ -150,3 +176,19 @@ func convertUserSignature(src user) scm.Signature {
Avatar: src.Avatar,
}
}

func convertTagList(src []*tag) []*scm.Reference {
var dst []*scm.Reference
for _, v := range src {
dst = append(dst, convertTag(v))
}
return dst
}

func convertTag(src *tag) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(src.Ref),
Path: src.Ref,
Sha: src.Object.Sha,
}
}
48 changes: 42 additions & 6 deletions scm/driver/gitea/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,53 @@ func TestGitListBranches(t *testing.T) {
//

func TestGitFindTag(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0").
Reply(200).
Type("application/json").
File("testdata/tag.json")

client, _ := New("https://try.gitea.io")
_, _, err := client.Git.FindTag(context.Background(), "go-gitea/gitea", "v1.0.0")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
got, _, err := client.Git.FindTag(context.Background(), "go-gitea/gitea", "v1.0.0")
if err != nil {
t.Error(err)
return
}

want := &scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/tag.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

func TestGitListTags(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags").
Reply(200).
Type("application/json").
File("testdata/tags.json")

client, _ := New("https://try.gitea.io")
_, _, err := client.Git.ListTags(context.Background(), "go-gitea/gitea", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
got, _, err := client.Git.ListTags(context.Background(), "go-gitea/gitea", scm.ListOptions{})
if err != nil {
t.Error(err)
return
}

want := []*scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/tags.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
11 changes: 11 additions & 0 deletions scm/driver/gitea/testdata/tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"ref": "refs/tags/v1.0.0",
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0",
"object": {
"type": "commit",
"sha": "4b736a01b6291e21c663ae9aab494850e7a50723",
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/commits/4b736a01b6291e21c663ae9aab494850e7a50723"
}
}
]
5 changes: 5 additions & 0 deletions scm/driver/gitea/testdata/tag.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "v1.0.0",
"Path": "refs/tags/v1.0.0",
"Sha": "4b736a01b6291e21c663ae9aab494850e7a50723"
}
11 changes: 11 additions & 0 deletions scm/driver/gitea/testdata/tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"ref": "refs/tags/v1.0.0",
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0",
"object": {
"type": "commit",
"sha": "4b736a01b6291e21c663ae9aab494850e7a50723",
"url": "https://try.gitea.io/api/v1/repos/go-gitea/gitea/git/commits/4b736a01b6291e21c663ae9aab494850e7a50723"
}
}
]
5 changes: 5 additions & 0 deletions scm/driver/gitea/testdata/tags.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{
"Name": "v1.0.0",
"Path": "refs/tags/v1.0.0",
"Sha": "4b736a01b6291e21c663ae9aab494850e7a50723"
}]

0 comments on commit 427b8a8

Please sign in to comment.