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

Added git tags support. #51

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions github/git_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"fmt"
)

// GitObject represents a git object.
type GitObject struct {
Type string `json:"type,omitempty"`
SHA string `json:"sha,omitempty"`
URL string `json:"url,omitempty"`
}

// Tag represents a tag object.
type Tag struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Tag will also need a type field, which is used just for the create call.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err, no. Actually it's worse than that. On input, you pass an "object" field which is a string, but on output it's an object. So I guess that means we have to have a separate TagRequest (or similarly named) type /facepalm

Tag string `json:"tag,omitempty"`
SHA string `json:"sha,omitempty"`
URL string `json:"url,omitempty"`
Message string `json:"message,omitempty"`
Tagger *CommitAuthor `json:"tagger,omitempty"`
Object *GitObject `json:"object,omitempty"`
}

type TagRequest struct {
Tag string
Message string
Object string
Type string
Tagger *CommitAuthor
}

// GetTag fetchs a tag from a repo given a SHA.
//
// GitHub API docs: http://developer.github.com/v3/git/tags/#get-a-tag
func (s *GitService) GetTag(owner string, repo string, sha string) (*Tag, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/tags/%v", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

tag := new(Tag)
resp, err := s.client.Do(req, tag)
return tag, resp, err
}

// CreateTag creates a tag object.
//
// GitHub API docs: http://developer.github.com/v3/git/tags/#create-a-tag-object
func (s *GitService) CreateTag(owner string, repo string, tagRequest *TagRequest) (*Tag, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/tags", owner, repo)
req, err := s.client.NewRequest("POST", u, tagRequest)
if err != nil {
return nil, nil, err
}

t := new(Tag)
resp, err := s.client.Do(req, t)
return t, resp, err
}
65 changes: 65 additions & 0 deletions github/git_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)

func TestGitService_GetTag(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/repos/o/r/git/tags/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")

fmt.Fprint(w, `{"tag": "t"}`)
})

tag, _, err := client.Git.GetTag("o", "r", "s")

if err != nil {
t.Errorf("Git.GetTag returned error: %v", err)
}

want := &Tag{Tag: "t"}
if !reflect.DeepEqual(tag, want) {
t.Errorf("Git.GetTag returned %+v, want %+v", tag, want)
}
}

func TestGitService_CreateTag(t *testing.T) {
setup()
defer teardown()

input := &TagRequest{Tag: "t", Object: "s"}

mux.HandleFunc("/repos/o/r/git/tags", func(w http.ResponseWriter, r *http.Request) {
v := new(TagRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

fmt.Fprint(w, `{"tag": "t"}`)
})

tag, _, err := client.Git.CreateTag("o", "r", input)
if err != nil {
t.Errorf("Git.CreateTag returned error: %v", err)
}

want := &Tag{Tag: "t"}
if !reflect.DeepEqual(tag, want) {
t.Errorf("Git.GetTag returned %+v, want %+v", tag, want)
}
}