Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented FindTag for github driver. #79

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion scm/driver/github/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ 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
path := fmt.Sprintf("repos/%s/git/ref/tags/%s", repo, name)
out := new(ref)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRef(out), res, err
}

func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
Expand Down Expand Up @@ -102,6 +105,14 @@ type commit struct {
Files []*file `json:"files"`
}

type ref struct {
Ref string `json:"ref"`
Object struct {
Type string `json:"type"`
Sha string `json:"sha"`
} `json:"object"`
}

type compare struct {
Files []*file `json:"files"`
}
Expand Down Expand Up @@ -152,6 +163,14 @@ func convertBranch(from *branch) *scm.Reference {
}
}

func convertRef(from *ref) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(from.Ref),
Path: from.Ref,
Sha: from.Object.Sha,
}
}

func convertTagList(from []*branch) []*scm.Reference {
to := []*scm.Reference{}
for _, v := range from {
Expand Down
30 changes: 26 additions & 4 deletions scm/driver/github/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,33 @@ func TestGitFindBranch(t *testing.T) {
}

func TestGitFindTag(t *testing.T) {
git := new(gitService)
_, _, err := git.FindTag(context.Background(), "octocat/hello-world", "v1.0")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
defer gock.Off()

gock.New("https://api.github.com").
Get("/repos/octocat/hello-world/git/ref/tags/v0.1").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/tag.json")

client := NewDefault()
got, res, err := client.Git.FindTag(context.Background(), "octocat/hello-world", "v0.1")
if err != nil {
t.Error(err)
return
}

want := new(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)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestGitListCommits(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions scm/driver/github/testdata/tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ref": "refs/tags/v0.1",
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo3ZmQxYTYwYjAxZjkxYjMxNGY1OTk1NWE0ZTRkNGU4MGQ4ZWRmMTFk",
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/tags/v0.1",
"object": {
"sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"type": "commit",
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"
}
}
5 changes: 5 additions & 0 deletions scm/driver/github/testdata/tag.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "v0.1",
"Path": "refs/tags/v0.1",
"Sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"
}