Browse Source

Fix CreateCommit

The input and response types for commit messages differ in the API.
While commit.Tree and commit.Parents are objects with fields when
returned by the GitHub API, the attributes are plain strings when
creating new commits.

See: https://developer.github.com/v3/git/commits/#create-a-commit
Tobias Schmidt 12 years ago
committed by Will Norris
parent
commit
5be211b578
2 changed files with 41 additions and 5 deletions
  1. +27
    -1
      github/git_commits.go
  2. +14
    -4
      github/git_commits_test.go

+ 27
- 1
github/git_commits.go View File

@ -57,6 +57,15 @@ func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit,
return c, resp, err return c, resp, err
} }
// createCommit represents the body of a CreateCommit request.
type createCommit struct {
Author *CommitAuthor `json:"author,omitempty"`
Committer *CommitAuthor `json:"committer,omitempty"`
Message *string `json:"message,omitempty"`
Tree *string `json:"tree,omitempty"`
Parents []string `json:"parents,omitempty"`
}
// CreateCommit creates a new commit in a repository. // CreateCommit creates a new commit in a repository.
// //
// The commit.Committer is optional and will be filled with the commit.Author // The commit.Committer is optional and will be filled with the commit.Author
@ -66,7 +75,24 @@ func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit,
// GitHub API docs: http://developer.github.com/v3/git/commits/#create-a-commit // GitHub API docs: http://developer.github.com/v3/git/commits/#create-a-commit
func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, *Response, error) { func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo) u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo)
req, err := s.client.NewRequest("POST", u, commit)
body := &createCommit{}
if commit != nil {
parents := make([]string, len(commit.Parents))
for i, parent := range commit.Parents {
parents[i] = *parent.SHA
}
body = &createCommit{
Author: commit.Author,
Committer: commit.Committer,
Message: commit.Message,
Tree: commit.Tree.SHA,
Parents: parents,
}
}
req, err := s.client.NewRequest("POST", u, body)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }


+ 14
- 4
github/git_commits_test.go View File

@ -42,15 +42,25 @@ func TestGitService_CreateCommit(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
input := &Commit{Message: String("m"), Tree: &Tree{SHA: String("t")}}
input := &Commit{
Message: String("m"),
Tree: &Tree{SHA: String("t")},
Parents: []Commit{{SHA: String("p")}},
}
mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
v := new(Commit)
v := new(createCommit)
json.NewDecoder(r.Body).Decode(v) json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "POST") testMethod(t, r, "POST")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
want := &createCommit{
Message: input.Message,
Tree: String("t"),
Parents: []string{"p"},
}
if !reflect.DeepEqual(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
} }
fmt.Fprint(w, `{"sha":"s"}`) fmt.Fprint(w, `{"sha":"s"}`)
}) })


Loading…
Cancel
Save