From 71fcef4a0e22913331ee808fa6b4a4a8e88dba44 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 3 Jul 2013 16:20:10 -0700 Subject: [PATCH] misc cleanup of trees API - complete the rename from TreesService to GitService. For example, renaming Get to GetTree, and Create to CreateTree. - rename GitTree to TreeEntry. I really have no idea why GitHub chose to use the field name "tree" for this, since these aren't necessarily trees in the git sense. Looking at it more closely, these are really just the entries within a tree, hence the rename. - simplify several tests and shuffle some things for consistency with the rest of the library --- github/git.go | 65 ++++++++++++++-------------- github/git_test.go | 103 ++++++++++++++++----------------------------- 2 files changed, 71 insertions(+), 97 deletions(-) diff --git a/github/git.go b/github/git.go index d358f27..830444b 100644 --- a/github/git.go +++ b/github/git.go @@ -1,14 +1,13 @@ -// Copyright 2013 Google. All rights reserved. +// 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 or at -// https://developers.google.com/open-source/licenses/bsd +// license that can be found in the LICENSE file. package github import "fmt" -// GitService handles communication with the tree related +// GitService handles communication with the git data related // methods of the GitHub API. // // GitHub API docs: http://developer.github.com/v3/git/ @@ -16,13 +15,16 @@ type GitService struct { client *Client } +// Tree represents a GitHub tree. type Tree struct { - SHA string `json:"sha,omitempty"` - Trees []GitTree `json:"tree,omitempty"` + SHA string `json:"sha,omitempty"` + Entries []TreeEntry `json:"tree,omitempty"` } -// Tree represents a Git tree. -type GitTree struct { +// 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"` @@ -30,48 +32,49 @@ type GitTree struct { Size int `json:"size,omitempty"` } +// createTree represents the body of a CreateTree request. type createTree struct { - baseTree string `json:base_tree` - trees []GitTree `json:tree` + BaseTree string `json:base_tree` + Entries []TreeEntry `json:tree` } -// Get the Tree object for a given sha hash from a users repository. +// GetTree fetches the Tree object for a given sha hash from a users repository. // // GitHub API docs: http://developer.github.com/v3/git/trees/#get-a-tree -func (s *GitService) Get(user string, repo string, sha string, recursive bool) (*Tree, error) { - url_ := fmt.Sprintf("repos/%v/%v/git/trees/%v", user, repo, sha) - +func (s *GitService) GetTree(user string, repo string, sha string, recursive bool) (*Tree, error) { + u := fmt.Sprintf("repos/%v/%v/git/trees/%v", user, repo, sha) if recursive { - url_ += "?recursive=1" + u += "?recursive=1" } - req, err := s.client.NewRequest("GET", url_, nil) + req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, err } - var response Tree - _, err = s.client.Do(req, &response) - return &response, err + t := new(Tree) + _, err = s.client.Do(req, t) + return t, err } -// The tree creation API will take nested entries as well. -// 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. +// 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) Create(owner string, repo string, sha string, baseTreeSha string, trees []GitTree) (*Tree, error) { - url_ := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha) +func (s *GitService) CreateTree(owner string, repo string, sha string, baseTree string, entries []TreeEntry) (*Tree, error) { + u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha) - req, err := s.client.NewRequest("POST", url_, createTree{ - baseTree: baseTreeSha, - trees: trees, - }) + body := &createTree{ + BaseTree: baseTree, + Entries: entries, + } + req, err := s.client.NewRequest("POST", u, body) if err != nil { return nil, err } - r := new(Tree) - _, err = s.client.Do(req, r) - return r, err + t := new(Tree) + _, err = s.client.Do(req, t) + return t, err } diff --git a/github/git_test.go b/github/git_test.go index 4935856..c813f19 100644 --- a/github/git_test.go +++ b/github/git_test.go @@ -1,8 +1,7 @@ -// Copyright 2013 Google. All rights reserved. +// 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 or at -// https://developers.google.com/open-source/licenses/bsd +// license that can be found in the LICENSE file. package github @@ -14,95 +13,67 @@ import ( "testing" ) -func TestGitService_Get_authenticatedUser(t *testing.T) { +func TestGitService_GetTree(t *testing.T) { setup() defer teardown() - url_ := fmt.Sprintf("/repos/%v/%v/git/trees/%v", "user", "repo", "coffebabecoffebabecoffebabe") - - mux.HandleFunc(url_, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/repos/u/r/git/trees/s", func(w http.ResponseWriter, r *http.Request) { if m := "GET"; m != r.Method { t.Errorf("Request method = %v, want %v", r.Method, m) } fmt.Fprint(w, `{ - "sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312", - "url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312", - "tree": [ - { - "path": "file.rb", - "mode": "100644", - "type": "blob", - "size": 30, - "sha": "44b4fc6d56897b048c772eb4087f854f46256132", - "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132" - }, - { - "path": "subdir", - "mode": "040000", - "type": "tree", - "sha": "f484d249c660418515fb01c2b9662073663c242e", - "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e" - } - ] + "sha": "s", + "tree": [ { "type": "blob" } ] }`) }) - trees, err := client.Trees.Get("user", "repo", "coffebabecoffebabecoffebabe", true) + tree, err := client.Git.GetTree("u", "r", "s", true) if err != nil { - t.Errorf("Trees.List returned error: %v", err) + t.Errorf("Git.GetTree returned error: %v", err) } want := Tree{ - SHA: `9fb037999f264ba9a7fc6274d15fa3ae2ab98312`, - Trees: []GitTree{ - GitTree{ - Path: "file.rb", - Mode: "100644", + SHA: "s", + Entries: []TreeEntry{ + TreeEntry{ Type: "blob", - Size: 30, - SHA: "44b4fc6d56897b048c772eb4087f854f46256132", - }, - GitTree{ - Path: "subdir", - Mode: "040000", - Type: "tree", - SHA: "f484d249c660418515fb01c2b9662073663c242e", }, }, } - if !reflect.DeepEqual(*trees, want) { - t.Errorf("Tree.List returned %+v, want %+v", *trees, want) + if !reflect.DeepEqual(*tree, want) { + t.Errorf("Tree.Get returned %+v, want %+v", *tree, want) } } -func TestGitService_Create_authenticatedUser(t *testing.T) { +func TestGitService_CreateTree(t *testing.T) { setup() defer teardown() - url_ := fmt.Sprintf("/repos/%v/%v/git/trees/%v", "user", "repo", "coffebabecoffebabecoffebabe") - - treesToCreate := - []GitTree{ - GitTree{ - Path: "file.rb", - Mode: "100644", - Type: "blob", - SHA: "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b", - }, - } - - mux.HandleFunc(url_, func(w http.ResponseWriter, r *http.Request) { - v := new(struct { - BaseTree string `json:base_tree` - Tree []GitTree `json:tree` - }) + input := []TreeEntry{ + TreeEntry{ + Path: "file.rb", + Mode: "100644", + Type: "blob", + SHA: "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b", + }, + } + mux.HandleFunc("/repos/u/r/git/trees/s", func(w http.ResponseWriter, r *http.Request) { + v := new(createTree) json.NewDecoder(r.Body).Decode(v) if m := "POST"; m != r.Method { t.Errorf("Request method = %v, want %v", r.Method, m) } + want := &createTree{ + BaseTree: "b", + Entries: input, + } + if !reflect.DeepEqual(v, want) { + t.Errorf("Git.CreateTree request body: %+v, want %+v", v, want) + } + fmt.Fprint(w, `{ "sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7", "tree": [ @@ -117,15 +88,15 @@ func TestGitService_Create_authenticatedUser(t *testing.T) { }`) }) - tree, err := client.Trees.Create("user", "repo", "coffebabecoffebabecoffebabe", "basebasebase", treesToCreate) + tree, err := client.Git.CreateTree("u", "r", "s", "b", input) if err != nil { - t.Errorf("Trees.Create returned error: %v", err) + t.Errorf("Git.CreateTree returned error: %v", err) } want := Tree{ "cd8274d15fa3ae2ab983129fb037999f264ba9a7", - []GitTree{ - GitTree{ + []TreeEntry{ + TreeEntry{ Path: "file.rb", Mode: "100644", Type: "blob", @@ -136,6 +107,6 @@ func TestGitService_Create_authenticatedUser(t *testing.T) { } if !reflect.DeepEqual(*tree, want) { - t.Errorf("Tree.Create returned %+v, want %+v", *tree, want) + t.Errorf("Git.CreateTree returned %+v, want %+v", *tree, want) } }