From 8f139c86abd7d419a647d3927dc642a14aed8a6a Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Thu, 17 Apr 2014 22:54:52 -0700 Subject: [PATCH] Add RepositoriesService.GetByID method. Note: GetByID uses the undocumented GitHub API endpoint /repositories/:id. Include license preview Accept header. Helps #329. --- github/repos.go | 23 +++++++++++++++++++++++ github/repos_test.go | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/github/repos.go b/github/repos.go index e039aca..4909b16 100644 --- a/github/repos.go +++ b/github/repos.go @@ -277,6 +277,29 @@ func (s *RepositoriesService) Get(owner, repo string) (*Repository, *Response, e return repository, resp, err } +// GetByID fetches a repository. +// +// Note: GetByID uses the undocumented GitHub API endpoint /repositories/:id. +func (s *RepositoriesService) GetByID(id int) (*Repository, *Response, error) { + u := fmt.Sprintf("repositories/%d", id) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when the license support fully launches + // https://developer.github.com/v3/licenses/#get-a-repositorys-license + req.Header.Set("Accept", mediaTypeLicensesPreview) + + repository := new(Repository) + resp, err := s.client.Do(req, repository) + if err != nil { + return nil, resp, err + } + + return repository, resp, err +} + // Edit updates a repository. // // GitHub API docs: http://developer.github.com/v3/repos/#edit diff --git a/github/repos_test.go b/github/repos_test.go index e875c0e..c864aff 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -222,6 +222,27 @@ func TestRepositoriesService_Get(t *testing.T) { } } +func TestRepositoriesService_GetByID(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repositories/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeLicensesPreview) + fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`) + }) + + repo, _, err := client.Repositories.GetByID(1) + if err != nil { + t.Errorf("Repositories.GetByID returned error: %v", err) + } + + want := &Repository{ID: Int(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}} + if !reflect.DeepEqual(repo, want) { + t.Errorf("Repositories.GetByID returned %+v, want %+v", repo, want) + } +} + func TestRepositoriesService_Edit(t *testing.T) { setup() defer teardown()