Browse Source

add ListAll and Create methods for repositories

Will Norris 13 years ago
parent
commit
d3d9655fad
3 changed files with 145 additions and 2 deletions
  1. +2
    -2
      github/github_test.go
  2. +52
    -0
      github/repos.go
  3. +91
    -0
      github/repos_test.go

+ 2
- 2
github/github_test.go View File

@ -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 {


+ 52
- 0
github/repos.go View File

@ -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


+ 91
- 0
github/repos_test.go View File

@ -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()


Loading…
Cancel
Save