From 0e92a57a35b2a6eebf19ad8ec1d69e699b824cf2 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 30 May 2013 11:13:26 -0700 Subject: [PATCH] add functions for listing and creating repo forks --- github/repos.go | 59 +++++++++++++++++++++++++++++++++++ github/repos_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/github/repos.go b/github/repos.go index ca17f5f..f41d5d6 100644 --- a/github/repos.go +++ b/github/repos.go @@ -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 +} diff --git a/github/repos_test.go b/github/repos_test.go index 29aeec2..958ec99 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -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) + } +}