From 1e532bd48bdfc586a40f09fe487fa36761efaeea Mon Sep 17 00:00:00 2001 From: Billy Lynch Date: Thu, 1 Aug 2013 11:19:58 -0400 Subject: [PATCH] Restructured Git Data Service files. --- github/git.go | 122 ---------------------- github/git_commits.go | 63 +++++++++++ github/git_commits_test.go | 62 +++++++++++ github/git_trees.go | 74 +++++++++++++ github/{git_test.go => git_trees_test.go} | 48 --------- 5 files changed, 199 insertions(+), 170 deletions(-) create mode 100644 github/git_commits.go create mode 100644 github/git_commits_test.go create mode 100644 github/git_trees.go rename github/{git_test.go => git_trees_test.go} (64%) diff --git a/github/git.go b/github/git.go index c075a05..a80e55b 100644 --- a/github/git.go +++ b/github/git.go @@ -5,12 +5,6 @@ package github -import ( - "time" - - "fmt" -) - // GitService handles communication with the git data related // methods of the GitHub API. // @@ -18,119 +12,3 @@ import ( type GitService struct { client *Client } - -// Commit represents a GitHub commit. -type Commit struct { - SHA string `json:"sha,omitempty"` - Author *CommitAuthor `json:"author,omitempty"` - Committer *CommitAuthor `json:"committer,omitempty"` - Message string `json:"message,omitempty"` - Tree *Tree `json:"tree,omitempty"` - Parents []Commit `json:"parents,omitempty"` -} - -// CommitAuthor represents the author or committer of a commit. The commit -// author may not correspond to a GitHub User. -type CommitAuthor struct { - Date *time.Time `json:"date,omitempty"` - Name string `json:"name,omitempty"` - Email string `json:"email,omitempty"` -} - -// Tree represents a GitHub tree. -type Tree struct { - SHA string `json:"sha,omitempty"` - Entries []TreeEntry `json:"tree,omitempty"` -} - -// TreeEntry represents the contents of a tree structure. TreeEntry can -// represent either a blob, a commit (in the case of a submodule), or another -// tree. -type TreeEntry struct { - SHA string `json:"sha,omitempty"` - Path string `json:"path,omitempty"` - Mode string `json:"mode,omitempty"` - Type string `json:"type,omitempty"` - Size int `json:"size,omitempty"` -} - -// GetCommit fetchs the Commit object for a given SHA. -// -// GitHub API docs: http://developer.github.com/v3/git/commits/#get-a-commit -func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit, error) { - u := fmt.Sprintf("repos/%v/%v/git/commits/%v", owner, repo, sha) - req, err := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, err - } - - c := new(Commit) - _, err = s.client.Do(req, c) - return c, err -} - -// CreateCommit creates a new commit in a repository. -// -// The commit.Committer is optional and will be filled with the commit.Author -// data if omitted. If the commit.Author is omitted, it will be filled in with -// the authenticated user’s information and the current date. -// -// GitHub API docs: http://developer.github.com/v3/git/commits/#create-a-commit -func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, error) { - u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo) - req, err := s.client.NewRequest("POST", u, commit) - if err != nil { - return nil, err - } - - c := new(Commit) - _, err = s.client.Do(req, c) - return c, err -} - -// GetTree fetches the Tree object for a given sha hash from a repository. -// -// GitHub API docs: http://developer.github.com/v3/git/trees/#get-a-tree -func (s *GitService) GetTree(owner string, repo string, sha string, recursive bool) (*Tree, error) { - u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha) - if recursive { - u += "?recursive=1" - } - - req, err := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, err - } - - t := new(Tree) - _, err = s.client.Do(req, t) - return t, err -} - -// createTree represents the body of a CreateTree request. -type createTree struct { - BaseTree string `json:"base_tree"` - Entries []TreeEntry `json:"tree"` -} - -// CreateTree creates a new tree in a repository. If both a tree and a nested -// path modifying that tree are specified, it will overwrite the contents of -// that tree with the new path contents and write a new tree out. -// -// GitHub API docs: http://developer.github.com/v3/git/trees/#create-a-tree -func (s *GitService) CreateTree(owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, error) { - u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo) - - body := &createTree{ - BaseTree: baseTree, - Entries: entries, - } - req, err := s.client.NewRequest("POST", u, body) - if err != nil { - return nil, err - } - - t := new(Tree) - _, err = s.client.Do(req, t) - return t, err -} diff --git a/github/git_commits.go b/github/git_commits.go new file mode 100644 index 0000000..1d26c6e --- /dev/null +++ b/github/git_commits.go @@ -0,0 +1,63 @@ +// 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 ( + "time" + "fmt" +) + +// Commit represents a GitHub commit. +type Commit struct { + SHA string `json:"sha,omitempty"` + Author *CommitAuthor `json:"author,omitempty"` + Committer *CommitAuthor `json:"committer,omitempty"` + Message string `json:"message,omitempty"` + Tree *Tree `json:"tree,omitempty"` + Parents []Commit `json:"parents,omitempty"` +} + +// CommitAuthor represents the author or committer of a commit. The commit +// author may not correspond to a GitHub User. +type CommitAuthor struct { + Date *time.Time `json:"date,omitempty"` + Name string `json:"name,omitempty"` + Email string `json:"email,omitempty"` +} + +// GetCommit fetchs the Commit object for a given SHA. +// +// GitHub API docs: http://developer.github.com/v3/git/commits/#get-a-commit +func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit, error) { + u := fmt.Sprintf("repos/%v/%v/git/commits/%v", owner, repo, sha) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + + c := new(Commit) + _, err = s.client.Do(req, c) + return c, err +} + +// CreateCommit creates a new commit in a repository. +// +// The commit.Committer is optional and will be filled with the commit.Author +// data if omitted. If the commit.Author is omitted, it will be filled in with +// the authenticated user’s information and the current date. +// +// GitHub API docs: http://developer.github.com/v3/git/commits/#create-a-commit +func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, error) { + u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo) + req, err := s.client.NewRequest("POST", u, commit) + if err != nil { + return nil, err + } + + c := new(Commit) + _, err = s.client.Do(req, c) + return c, err +} diff --git a/github/git_commits_test.go b/github/git_commits_test.go new file mode 100644 index 0000000..ad1062b --- /dev/null +++ b/github/git_commits_test.go @@ -0,0 +1,62 @@ +// 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_GetCommit(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/git/commits/s", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"sha":"s","message":"m","author":{"name":"n"}}`) + }) + + commit, err := client.Git.GetCommit("o", "r", "s") + if err != nil { + t.Errorf("Git.GetCommit returned error: %v", err) + } + + want := &Commit{SHA: "s", Message: "m", Author: &CommitAuthor{Name: "n"}} + if !reflect.DeepEqual(commit, want) { + t.Errorf("Git.GetCommit returned %+v, want %+v", commit, want) + } +} + +func TestGitService_CreateCommit(t *testing.T) { + setup() + defer teardown() + + input := &Commit{Message: "m", Tree: &Tree{SHA: "t"}} + + mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { + v := new(Commit) + 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, `{"sha":"s"}`) + }) + + commit, err := client.Git.CreateCommit("o", "r", input) + if err != nil { + t.Errorf("Git.CreateCommit returned error: %v", err) + } + + want := &Commit{SHA: "s"} + if !reflect.DeepEqual(commit, want) { + t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want) + } +} diff --git a/github/git_trees.go b/github/git_trees.go new file mode 100644 index 0000000..729a011 --- /dev/null +++ b/github/git_trees.go @@ -0,0 +1,74 @@ +// 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" +) + +// Tree represents a GitHub tree. +type Tree struct { + SHA string `json:"sha,omitempty"` + Entries []TreeEntry `json:"tree,omitempty"` +} + +// TreeEntry represents the contents of a tree structure. TreeEntry can +// represent either a blob, a commit (in the case of a submodule), or another +// tree. +type TreeEntry struct { + SHA string `json:"sha,omitempty"` + Path string `json:"path,omitempty"` + Mode string `json:"mode,omitempty"` + Type string `json:"type,omitempty"` + Size int `json:"size,omitempty"` +} + +// GetTree fetches the Tree object for a given sha hash from a repository. +// +// GitHub API docs: http://developer.github.com/v3/git/trees/#get-a-tree +func (s *GitService) GetTree(owner string, repo string, sha string, recursive bool) (*Tree, error) { + u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha) + if recursive { + u += "?recursive=1" + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + + t := new(Tree) + _, err = s.client.Do(req, t) + return t, err +} + +// createTree represents the body of a CreateTree request. +type createTree struct { + BaseTree string `json:"base_tree"` + Entries []TreeEntry `json:"tree"` +} + +// CreateTree creates a new tree in a repository. If both a tree and a nested +// path modifying that tree are specified, it will overwrite the contents of +// that tree with the new path contents and write a new tree out. +// +// GitHub API docs: http://developer.github.com/v3/git/trees/#create-a-tree +func (s *GitService) CreateTree(owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, error) { + u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo) + + body := &createTree{ + BaseTree: baseTree, + Entries: entries, + } + req, err := s.client.NewRequest("POST", u, body) + if err != nil { + return nil, err + } + + t := new(Tree) + _, err = s.client.Do(req, t) + return t, err +} diff --git a/github/git_test.go b/github/git_trees_test.go similarity index 64% rename from github/git_test.go rename to github/git_trees_test.go index ccd814f..167e4a6 100644 --- a/github/git_test.go +++ b/github/git_trees_test.go @@ -13,54 +13,6 @@ import ( "testing" ) -func TestGitService_GetCommit(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/repos/o/r/git/commits/s", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"sha":"s","message":"m","author":{"name":"n"}}`) - }) - - commit, err := client.Git.GetCommit("o", "r", "s") - if err != nil { - t.Errorf("Git.GetCommit returned error: %v", err) - } - - want := &Commit{SHA: "s", Message: "m", Author: &CommitAuthor{Name: "n"}} - if !reflect.DeepEqual(commit, want) { - t.Errorf("Git.GetCommit returned %+v, want %+v", commit, want) - } -} - -func TestGitService_CreateCommit(t *testing.T) { - setup() - defer teardown() - - input := &Commit{Message: "m", Tree: &Tree{SHA: "t"}} - - mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { - v := new(Commit) - 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, `{"sha":"s"}`) - }) - - commit, err := client.Git.CreateCommit("o", "r", input) - if err != nil { - t.Errorf("Git.CreateCommit returned error: %v", err) - } - - want := &Commit{SHA: "s"} - if !reflect.DeepEqual(commit, want) { - t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want) - } -} - func TestGitService_GetTree(t *testing.T) { setup() defer teardown()