From d3d9655fad081711f66546dbd439814a8ef1d5c7 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 29 May 2013 13:39:34 -0700 Subject: [PATCH] add ListAll and Create methods for repositories --- github/github_test.go | 4 +- github/repos.go | 52 +++++++++++++++++++++++++ github/repos_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 2 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 4b967bc..d47d053 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -231,7 +231,7 @@ func TestParseBooleanResponse_true(t *testing.T) { } func TestParseBooleanResponse_false(t *testing.T) { - v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusNotFound} } + v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusNotFound}} result, err := parseBoolResponse(v) if err != nil { @@ -244,7 +244,7 @@ func TestParseBooleanResponse_false(t *testing.T) { } func TestParseBooleanResponse_error(t *testing.T) { - v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusBadRequest} } + v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusBadRequest}} result, err := parseBoolResponse(v) if err == nil { diff --git a/github/repos.go b/github/repos.go index 72548e0..ca17f5f 100644 --- a/github/repos.go +++ b/github/repos.go @@ -116,6 +116,58 @@ func (s *RepositoriesService) ListByOrg(org string, opt *RepositoryListByOrgOpti return *repos, err } +// RepositoryListAllOptions specifies the optional parameters to the +// RepositoriesService.ListAll method. +type RepositoryListAllOptions struct { + // ID of the last repository seen + Since int +} + +// ListAll lists all GitHub repositories in the order that they were created. +// +// GitHub API docs: http://developer.github.com/v3/repos/#list-all-repositories +func (s *RepositoriesService) ListAll(opt *RepositoryListAllOptions) ([]Repository, error) { + url_ := "repositories" + if opt != nil { + params := url.Values{ + "since": []string{strconv.Itoa(opt.Since)}, + } + url_ += "?" + params.Encode() + } + + req, err := s.client.NewRequest("GET", url_, nil) + if err != nil { + return nil, err + } + + repos := new([]Repository) + _, err = s.client.Do(req, repos) + return *repos, err +} + +// Create a new repository. If an organization is specified, the new +// repository will be created under that org. If the empty string is +// specified, it will be created for the authenticated user. +// +// GitHub API docs: http://developer.github.com/v3/repos/#create +func (s *RepositoriesService) Create(org string, repo *Repository) (*Repository, error) { + var url_ string + if org != "" { + url_ = fmt.Sprintf("orgs/%v/repos", org) + } else { + url_ = "user/repos" + } + + req, err := s.client.NewRequest("POST", url_, repo) + if err != nil { + return nil, err + } + + r := new(Repository) + _, err = s.client.Do(req, r) + return r, err +} + // Get fetches a repository. // // GitHub API docs: http://developer.github.com/v3/repos/#get diff --git a/github/repos_test.go b/github/repos_test.go index d7fdfea..3195d45 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -7,6 +7,7 @@ package github import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -125,6 +126,96 @@ func TestRepositoriesService_ListByOrg_invalidOrg(t *testing.T) { } } +func TestRepositoriesService_ListAll(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repositories", func(w http.ResponseWriter, r *http.Request) { + var v string + if m := "GET"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + v = r.FormValue("since") + if v != "1" { + t.Errorf("Request since parameter = %v, want %v", v, 1) + } + fmt.Fprint(w, `[{"id":1}]`) + }) + + opt := &RepositoryListAllOptions{1} + repos, err := client.Repositories.ListAll(opt) + if err != nil { + t.Errorf("Repositories.ListAll returned error: %v", err) + } + + want := []Repository{Repository{ID: 1}} + if !reflect.DeepEqual(repos, want) { + t.Errorf("Repositories.ListAll returned %+v, want %+v", repos, want) + } +} + +func TestRepositoriesService_Create_user(t *testing.T) { + setup() + defer teardown() + + input := &Repository{Name: "n"} + + mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) { + v := new(Repository) + json.NewDecoder(r.Body).Decode(v) + + if m := "POST"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + if !reflect.DeepEqual(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `{"id":1}`) + }) + + repo, err := client.Repositories.Create("", input) + if err != nil { + t.Errorf("Repositories.Create returned error: %v", err) + } + + want := &Repository{ID: 1} + if !reflect.DeepEqual(repo, want) { + t.Errorf("Repositories.Create returned %+v, want %+v", repo, want) + } +} + +func TestRepositoriesService_Create_org(t *testing.T) { + setup() + defer teardown() + + input := &Repository{Name: "n"} + + mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { + v := new(Repository) + json.NewDecoder(r.Body).Decode(v) + + if m := "POST"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + if !reflect.DeepEqual(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `{"id":1}`) + }) + + repo, err := client.Repositories.Create("o", input) + if err != nil { + t.Errorf("Repositories.Create returned error: %v", err) + } + + want := &Repository{ID: 1} + if !reflect.DeepEqual(repo, want) { + t.Errorf("Repositories.Create returned %+v, want %+v", repo, want) + } +} + func TestRepositoriesService_Get(t *testing.T) { setup() defer teardown()