Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.5k
Trees API#16
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
Uh oh!
There was an error while loading. Please reload this page.
Trees API #16
Changes from all commits
7b2c58b4d6b0ac012aa3ed1aa232808ecb0b8d911c3dce7fdcf0db42a1b823aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2013 Google. 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 | ||
| package github | ||
| import "fmt" | ||
| // TreesService handles communication with the tree related | ||
| // methods of the GitHub API. | ||
| // | ||
| // GitHub API docs: http://developer.github.com/v3/trees/ | ||
| type TreesService struct { | ||
| client *Client | ||
| } | ||
| type Tree struct { | ||
| SHA string `json:"sha,omitempty"` | ||
| Trees []GitTree `json:"tree,omitempty"` | ||
| } | ||
| // Tree represents a Git tree. | ||
| type GitTree 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"` | ||
| } | ||
| type createTree struct { | ||
| baseTree string `json:base_tree` | ||
| trees []GitTree `json:tree` | ||
| } | ||
| // Get 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 *TreesService) Get(user string, repo string, sha string, recursive bool) (*Tree, error) { | ||
| url_ := fmt.Sprintf("repos/%v/%v/git/trees/%v", user, repo, sha) | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is only returning a single tree, it should be a | ||
| if recursive { | ||
| url_ += "?recursive=1" | ||
| } | ||
| req, err := s.client.NewRequest("GET", url_, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var response Tree | ||
| _, err = s.client.Do(req, &response) | ||
| return &response, 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. | ||
| // | ||
| // GitHub API docs: http://developer.github.com/v3/git/trees/#create-a-tree | ||
| func (s *TreesService) 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) | ||
| req, err := s.client.NewRequest("POST", url_, createTree{ | ||
| baseTree: baseTreeSha, | ||
| trees: trees, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| r := new(Tree) | ||
| _, err = s.client.Do(req, r) | ||
| return r, err | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright 2013 Google. 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 | ||
| package github | ||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
| func TestTreesService_Get_authenticatedUser(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) { | ||
| 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" | ||
| } | ||
| ] | ||
| }`) | ||
| }) | ||
| trees, err := client.Trees.Get("user", "repo", "coffebabecoffebabecoffebabe", true) | ||
| if err != nil { | ||
| t.Errorf("Trees.List returned error: %v", err) | ||
| } | ||
| want := Tree{ | ||
| SHA: `9fb037999f264ba9a7fc6274d15fa3ae2ab98312`, | ||
| Trees: []GitTree{ | ||
| GitTree{ | ||
| Path: "file.rb", | ||
| Mode: "100644", | ||
| 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) | ||
| } | ||
| } | ||
| func TestTreesService_Create_authenticatedUser(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` | ||
| }) | ||
| json.NewDecoder(r.Body).Decode(v) | ||
| if m := "POST"; m != r.Method { | ||
| t.Errorf("Request method = %v, want %v", r.Method, m) | ||
| } | ||
| fmt.Fprint(w, `{ | ||
| "sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7", | ||
| "tree": [ | ||
| { | ||
| "path": "file.rb", | ||
| "mode": "100644", | ||
| "type": "blob", | ||
| "size": 132, | ||
| "sha": "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b" | ||
| } | ||
| ] | ||
| }`) | ||
| }) | ||
| tree, err := client.Trees.Create("user", "repo", "coffebabecoffebabecoffebabe", "basebasebase", treesToCreate) | ||
| if err != nil { | ||
| t.Errorf("Trees.Create returned error: %v", err) | ||
| } | ||
| want := Tree{ | ||
| "cd8274d15fa3ae2ab983129fb037999f264ba9a7", | ||
| []GitTree{ | ||
| GitTree{ | ||
| Path: "file.rb", | ||
| Mode: "100644", | ||
| Type: "blob", | ||
| Size: 132, | ||
| SHA: "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b", | ||
| }, | ||
| }, | ||
| } | ||
| if !reflect.DeepEqual(*tree, want) { | ||
| t.Errorf("Tree.Create returned %+v, want %+v", *tree, want) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing comment. The whole multiple Tree types is certainly unfortunate and a little confusing, though I think the naming convention you've got now is good. All the more reason for good comments to explain what's going on :)