diff --git a/github/git_commits.go b/github/git_commits.go index c96fc99..43a0a44 100644 --- a/github/git_commits.go +++ b/github/git_commits.go @@ -57,6 +57,15 @@ func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit, 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. // // 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 func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, *Response, error) { 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 { return nil, nil, err } diff --git a/github/git_commits_test.go b/github/git_commits_test.go index 12a8e22..538f523 100644 --- a/github/git_commits_test.go +++ b/github/git_commits_test.go @@ -42,15 +42,25 @@ func TestGitService_CreateCommit(t *testing.T) { setup() 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) { - v := new(Commit) + v := new(createCommit) json.NewDecoder(r.Body).Decode(v) 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"}`) })