Closes #436. Change-Id: I02f7503a362ffc5ec09e24d0e3da0f8adfcd4b0c
| @ -0,0 +1,38 @@ | |||||
| // Copyright 2016 The go-github AUTHORS. All rights reserved. | |||||
| // | |||||
| // Use of this source code is governed by a BSD-style | |||||
| // license that can be found in the LICENSE file. | |||||
| package github | |||||
| // IntegrationsService provides access to the installation related functions | |||||
| // in the GitHub API. | |||||
| // | |||||
| // GitHub API docs: https://developer.github.com/v3/integrations/ | |||||
| type IntegrationsService service | |||||
| // ListInstallations lists the installations that the current integration has. | |||||
| // | |||||
| // GitHub API docs: https://developer.github.com/v3/integrations/#find-installations | |||||
| func (s *IntegrationsService) ListInstallations(opt *ListOptions) ([]*Installation, *Response, error) { | |||||
| u, err := addOptions("integration/installations", opt) | |||||
| if err != nil { | |||||
| return nil, nil, err | |||||
| } | |||||
| req, err := s.client.NewRequest("GET", u, nil) | |||||
| if err != nil { | |||||
| return nil, nil, err | |||||
| } | |||||
| // TODO: remove custom Accept header when this API fully launches. | |||||
| req.Header.Set("Accept", mediaTypeIntegrationPreview) | |||||
| i := new([]*Installation) | |||||
| resp, err := s.client.Do(req, &i) | |||||
| if err != nil { | |||||
| return nil, resp, err | |||||
| } | |||||
| return *i, resp, err | |||||
| } | |||||
| @ -0,0 +1,46 @@ | |||||
| // Copyright 2016 The go-github AUTHORS. All rights reserved. | |||||
| // | |||||
| // Use of this source code is governed by a BSD-style | |||||
| // license that can be found in the LICENSE file. | |||||
| package github | |||||
| // Installation represents a GitHub integration installation. | |||||
| type Installation struct { | |||||
| ID *int `json:"id,omitempty"` | |||||
| Account *User `json:"account,omitempty"` | |||||
| AccessTokensURL *string `json:"access_tokens_url,omitempty"` | |||||
| RepositoriesURL *string `json:"repositories_url,omitempty"` | |||||
| } | |||||
| func (i Installation) String() string { | |||||
| return Stringify(i) | |||||
| } | |||||
| // ListRepos lists the repositories that the current installation has access to. | |||||
| // | |||||
| // GitHub API docs: https://developer.github.com/v3/integrations/installations/#list-repositories | |||||
| func (s *IntegrationsService) ListRepos(opt *ListOptions) ([]*Repository, *Response, error) { | |||||
| u, err := addOptions("installation/repositories", opt) | |||||
| if err != nil { | |||||
| return nil, nil, err | |||||
| } | |||||
| req, err := s.client.NewRequest("GET", u, nil) | |||||
| if err != nil { | |||||
| return nil, nil, err | |||||
| } | |||||
| // TODO: remove custom Accept header when this API fully launches. | |||||
| req.Header.Set("Accept", mediaTypeIntegrationPreview) | |||||
| var r struct { | |||||
| Repositories []*Repository `json:"repositories"` | |||||
| } | |||||
| resp, err := s.client.Do(req, &r) | |||||
| if err != nil { | |||||
| return nil, resp, err | |||||
| } | |||||
| return r.Repositories, resp, err | |||||
| } | |||||
| @ -0,0 +1,39 @@ | |||||
| // Copyright 2016 The go-github AUTHORS. All rights reserved. | |||||
| // | |||||
| // Use of this source code is governed by a BSD-style | |||||
| // license that can be found in the LICENSE file. | |||||
| package github | |||||
| import ( | |||||
| "fmt" | |||||
| "net/http" | |||||
| "reflect" | |||||
| "testing" | |||||
| ) | |||||
| func TestIntegrationService_ListRepos(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/installation/repositories", func(w http.ResponseWriter, r *http.Request) { | |||||
| testMethod(t, r, "GET") | |||||
| testHeader(t, r, "Accept", mediaTypeIntegrationPreview) | |||||
| testFormValues(t, r, values{ | |||||
| "page": "1", | |||||
| "per_page": "2", | |||||
| }) | |||||
| fmt.Fprint(w, `{"repositories": [{"id":1}]}`) | |||||
| }) | |||||
| opt := &ListOptions{Page: 1, PerPage: 2} | |||||
| repositories, _, err := client.Integrations.ListRepos(opt) | |||||
| if err != nil { | |||||
| t.Errorf("Integration.ListRepos returned error: %v", err) | |||||
| } | |||||
| want := []*Repository{{ID: Int(1)}} | |||||
| if !reflect.DeepEqual(repositories, want) { | |||||
| t.Errorf("Integration.ListRepos returned %+v, want %+v", repositories, want) | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,39 @@ | |||||
| // Copyright 2016 The go-github AUTHORS. All rights reserved. | |||||
| // | |||||
| // Use of this source code is governed by a BSD-style | |||||
| // license that can be found in the LICENSE file. | |||||
| package github | |||||
| import ( | |||||
| "fmt" | |||||
| "net/http" | |||||
| "reflect" | |||||
| "testing" | |||||
| ) | |||||
| func TestIntegrationService_ListInstallations(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/integration/installations", func(w http.ResponseWriter, r *http.Request) { | |||||
| testMethod(t, r, "GET") | |||||
| testHeader(t, r, "Accept", mediaTypeIntegrationPreview) | |||||
| testFormValues(t, r, values{ | |||||
| "page": "1", | |||||
| "per_page": "2", | |||||
| }) | |||||
| fmt.Fprint(w, `[{"id":1}]`) | |||||
| }) | |||||
| opt := &ListOptions{Page: 1, PerPage: 2} | |||||
| installations, _, err := client.Integrations.ListInstallations(opt) | |||||
| if err != nil { | |||||
| t.Errorf("Integration.ListInstallations returned error: %v", err) | |||||
| } | |||||
| want := []*Installation{{ID: Int(1)}} | |||||
| if !reflect.DeepEqual(installations, want) { | |||||
| t.Errorf("Integration.ListInstallations returned %+v, want %+v", installations, want) | |||||
| } | |||||
| } | |||||