From 81fd27092a73ae36269b7ecad4fe32f7971c3722 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 10 Apr 2014 17:05:19 -0700 Subject: [PATCH] add remaining repository methods - Repositories.Delete - Repositories.ListTeams - Repositories.ListTags - Repositories.GetBranch --- github/repos.go | 90 +++++++++++++++++++++++++++++++++++++++++--- github/repos_test.go | 84 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 6 deletions(-) diff --git a/github/repos.go b/github/repos.go index 775cbc5..9c9fbf1 100644 --- a/github/repos.go +++ b/github/repos.go @@ -230,6 +230,19 @@ func (s *RepositoriesService) Edit(owner, repo string, repository *Repository) ( return r, resp, err } +// Delete a repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#delete-a-repository +func (s *RepositoriesService) Delete(owner, repo string) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v", owner, repo) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + // Contributor represents a repository contributor type Contributor struct { Login *string `json:"login,omitempty"` @@ -263,7 +276,7 @@ type ListContributorsOptions struct { // // GitHub API docs: http://developer.github.com/v3/repos/#list-contributors func (s *RepositoriesService) ListContributors(owner string, repository string, opt *ListContributorsOptions) ([]Contributor, *Response, error) { - u := fmt.Sprintf("/repos/%v/%v/contributors", owner, repository) + u := fmt.Sprintf("repos/%v/%v/contributors", owner, repository) u, err := addOptions(u, opt) if err != nil { @@ -294,8 +307,8 @@ func (s *RepositoriesService) ListContributors(owner string, repository string, // } // // GitHub API Docs: http://developer.github.com/v3/repos/#list-languages -func (s *RepositoriesService) ListLanguages(owner string, repository string) (map[string]int, *Response, error) { - u := fmt.Sprintf("/repos/%v/%v/languages", owner, repository) +func (s *RepositoriesService) ListLanguages(owner string, repo string) (map[string]int, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/languages", owner, repo) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err @@ -310,17 +323,63 @@ func (s *RepositoriesService) ListLanguages(owner string, repository string) (ma return languages, resp, err } +// ListTeams lists the teams for the specified repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#list-teams +func (s *RepositoriesService) ListTeams(owner string, repo string) ([]Team, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/teams", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + teams := new([]Team) + resp, err := s.client.Do(req, teams) + if err != nil { + return nil, resp, err + } + + return *teams, resp, err +} + +// RepositoryTag represents a repository tag. +type RepositoryTag struct { + Name *string `json:"name,omitempty"` + Commit *Commit `json:"commit,omitempty"` + ZipballURL *string `json:"zipball_url,omitempty"` + TarballURL *string `json:"tarball_url,omitempty"` +} + +// ListTags lists tags for the specified repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#list-tags +func (s *RepositoriesService) ListTags(owner string, repo string) ([]RepositoryTag, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/tags", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + tags := new([]RepositoryTag) + resp, err := s.client.Do(req, tags) + if err != nil { + return nil, resp, err + } + + return *tags, resp, err +} + // Branch represents a repository branch type Branch struct { Name *string `json:"name,omitempty"` - Commit *Commit + Commit *Commit `json:"commit,omitempty"` } // ListBranches lists branches for the specified repository. // // GitHub API docs: http://developer.github.com/v3/repos/#list-branches -func (s *RepositoriesService) ListBranches(owner string, repository string) ([]Branch, *Response, error) { - u := fmt.Sprintf("/repos/%v/%v/branches", owner, repository) +func (s *RepositoriesService) ListBranches(owner string, repo string) ([]Branch, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/branches", owner, repo) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err @@ -334,3 +393,22 @@ func (s *RepositoriesService) ListBranches(owner string, repository string) ([]B return *branches, resp, err } + +// GetBranch gets the specified branch for a repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#get-branch +func (s *RepositoriesService) GetBranch(owner, repo, branch string) (*Branch, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + b := new(Branch) + resp, err := s.client.Do(req, b) + if err != nil { + return nil, resp, err + } + + return b, resp, err +} diff --git a/github/repos_test.go b/github/repos_test.go index 76503a5..02b8351 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -234,6 +234,20 @@ func TestRepositoriesService_Edit(t *testing.T) { } } +func TestRepositoriesService_Delete(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Repositories.Delete("o", "r") + if err != nil { + t.Errorf("Repositories.Delete returned error: %v", err) + } +} + func TestRepositoriesService_Get_invalidOwner(t *testing.T) { _, _, err := client.Repositories.Get("%", "r") testURLParseError(t, err) @@ -286,6 +300,56 @@ func TestRepositoriesService_ListLanguages(t *testing.T) { } } +func TestRepositoriesService_ListTeams(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/teams", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"id":1}]`) + }) + + teams, _, err := client.Repositories.ListTeams("o", "r") + if err != nil { + t.Errorf("Repositories.ListTeams returned error: %v", err) + } + + want := []Team{{ID: Int(1)}} + if !reflect.DeepEqual(teams, want) { + t.Errorf("Repositories.ListTeams returned %+v, want %+v", teams, want) + } +} + +func TestRepositoriesService_ListTags(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/tags", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"name":"n", "commit" : {"sha" : "s", "url" : "u"}, "zipball_url": "z", "tarball_url": "t"}]`) + }) + + tags, _, err := client.Repositories.ListTags("o", "r") + if err != nil { + t.Errorf("Repositories.ListTags returned error: %v", err) + } + + want := []RepositoryTag{ + { + Name: String("n"), + Commit: &Commit{ + SHA: String("s"), + URL: String("u"), + }, + ZipballURL: String("z"), + TarballURL: String("t"), + }, + } + if !reflect.DeepEqual(tags, want) { + t.Errorf("Repositories.ListTags returned %+v, want %+v", tags, want) + } +} + func TestRepositoriesService_ListBranches(t *testing.T) { setup() defer teardown() @@ -306,6 +370,26 @@ func TestRepositoriesService_ListBranches(t *testing.T) { } } +func TestRepositoriesService_GetBranch(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s"}}`) + }) + + branch, _, err := client.Repositories.GetBranch("o", "r", "b") + if err != nil { + t.Errorf("Repositories.GetBranch returned error: %v", err) + } + + want := &Branch{Name: String("n"), Commit: &Commit{SHA: String("s")}} + if !reflect.DeepEqual(branch, want) { + t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want) + } +} + func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) { _, _, err := client.Repositories.ListLanguages("%", "%") testURLParseError(t, err)