Browse Source

add functions for listing and creating repo forks

Will Norris 13 years ago
parent
commit
0e92a57a35
2 changed files with 133 additions and 0 deletions
  1. +59
    -0
      github/repos.go
  2. +74
    -0
      github/repos_test.go

+ 59
- 0
github/repos.go View File

@ -181,3 +181,62 @@ func (s *RepositoriesService) Get(owner, repo string) (*Repository, error) {
_, err = s.client.Do(req, repository)
return repository, err
}
// RepositoryListForksOptions specifies the optional parameters to the
// RepositoriesService.ListForks method.
type RepositoryListForksOptions struct {
// How to sort the forks list. Possible values are: newest, oldest,
// watchers. Default is "newest".
Sort string
}
// ListForks lists the forks of the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/forks/#list-forks
func (s *RepositoriesService) ListForks(owner, repo string, opt *RepositoryListForksOptions) ([]Repository, error) {
url_ := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
if opt != nil {
params := url.Values{
"sort": []string{opt.Sort},
}
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
}
// RepositoryCreateForkOptions specifies the optional parameters to the
// RepositoriesService.CreateFork method.
type RepositoryCreateForkOptions struct {
// The organization to fork the repository into.
Organization string
}
// CreateFork creates a fork of the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/forks/#list-forks
func (s *RepositoriesService) CreateFork(owner, repo string, opt *RepositoryCreateForkOptions) (*Repository, error) {
url_ := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
if opt != nil {
params := url.Values{
"organization": []string{opt.Organization},
}
url_ += "?" + params.Encode()
}
req, err := s.client.NewRequest("POST", url_, nil)
if err != nil {
return nil, err
}
fork := new(Repository)
_, err = s.client.Do(req, fork)
return fork, err
}

+ 74
- 0
github/repos_test.go View File

@ -257,3 +257,77 @@ func TestRepositoriesService_Get_invalidOwner(t *testing.T) {
t.Errorf("Expected URL parse error, got %+v", err)
}
}
func TestRepositoriesService_ListForks(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/forks", 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)
}
if v = r.FormValue("sort"); v != "newest" {
t.Errorf("Request type parameter = %v, want %v", v, "newest")
}
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &RepositoryListForksOptions{Sort: "newest"}
repos, err := client.Repositories.ListForks("o", "r", opt)
if err != nil {
t.Errorf("Repositories.ListForks returned error: %v", err)
}
want := []Repository{Repository{ID: 1}, Repository{ID: 2}}
if !reflect.DeepEqual(repos, want) {
t.Errorf("Repositories.ListForks returned %+v, want %+v", repos, want)
}
}
func TestRepositoriesService_ListForks_invalidOwner(t *testing.T) {
_, err := client.Repositories.ListForks("%", "r", nil)
if err == nil {
t.Errorf("Expected error to be returned")
}
if err, ok := err.(*url.Error); !ok {
t.Errorf("Expected URL parse error, got %+v", err)
}
}
func TestRepositoriesService_CreateFork(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) {
var v string
if m := "POST"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
if v = r.FormValue("organization"); v != "o" {
t.Errorf("Request type parameter = %v, want %v", v, "o")
}
fmt.Fprint(w, `{"id":1}`)
})
opt := &RepositoryCreateForkOptions{Organization: "o"}
repo, err := client.Repositories.CreateFork("o", "r", opt)
if err != nil {
t.Errorf("Repositories.CreateFork returned error: %v", err)
}
want := &Repository{ID: 1}
if !reflect.DeepEqual(repo, want) {
t.Errorf("Repositories.CreateFork returned %+v, want %+v", repo, want)
}
}
func TestRepositoriesService_CreateFork_invalidOwner(t *testing.T) {
_, err := client.Repositories.CreateFork("%", "r", nil)
if err == nil {
t.Errorf("Expected error to be returned")
}
if err, ok := err.(*url.Error); !ok {
t.Errorf("Expected URL parse error, got %+v", err)
}
}

Loading…
Cancel
Save