From 93d3f730320429a6e1ecf2ac1778db832930948a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Peignier?= Date: Tue, 27 Sep 2016 20:47:51 -0700 Subject: [PATCH] Add support for integrations service Closes #436. Change-Id: I02f7503a362ffc5ec09e24d0e3da0f8adfcd4b0c --- github/event_types.go | 8 ----- github/github.go | 5 +++ github/integration.go | 38 ++++++++++++++++++++ github/integration_installation.go | 46 +++++++++++++++++++++++++ github/integration_installation_test.go | 39 +++++++++++++++++++++ github/integration_test.go | 39 +++++++++++++++++++++ 6 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 github/integration.go create mode 100644 github/integration_installation.go create mode 100644 github/integration_installation_test.go create mode 100644 github/integration_test.go diff --git a/github/event_types.go b/github/event_types.go index da18f71..97472ce 100644 --- a/github/event_types.go +++ b/github/event_types.go @@ -172,14 +172,6 @@ type IntegrationInstallationRepositoriesEvent struct { Sender *User `json:"sender,omitempty"` } -// 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"` -} - // IssueCommentEvent is triggered when an issue comment is created on an issue // or pull request. // The Webhook event name is "issue_comment". diff --git a/github/github.go b/github/github.go index ba005de..465574b 100644 --- a/github/github.go +++ b/github/github.go @@ -90,6 +90,9 @@ const ( // https://developer.github.com/changes/2016-09-14-projects-api/ mediaTypeProjectsPreview = "application/vnd.github.inertia-preview+json" + + // https://developer.github.com/changes/2016-09-14-Integrations-Early-Access/ + mediaTypeIntegrationPreview = "application/vnd.github.machine-man-preview+json" ) // A Client manages communication with the GitHub API. @@ -120,6 +123,7 @@ type Client struct { Gists *GistsService Git *GitService Gitignores *GitignoresService + Integrations *IntegrationsService Issues *IssuesService Organizations *OrganizationsService PullRequests *PullRequestsService @@ -190,6 +194,7 @@ func NewClient(httpClient *http.Client) *Client { c.Gists = (*GistsService)(&c.common) c.Git = (*GitService)(&c.common) c.Gitignores = (*GitignoresService)(&c.common) + c.Integrations = (*IntegrationsService)(&c.common) c.Issues = (*IssuesService)(&c.common) c.Licenses = (*LicensesService)(&c.common) c.Migrations = (*MigrationService)(&c.common) diff --git a/github/integration.go b/github/integration.go new file mode 100644 index 0000000..b8d77ca --- /dev/null +++ b/github/integration.go @@ -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 +} diff --git a/github/integration_installation.go b/github/integration_installation.go new file mode 100644 index 0000000..aa59bfe --- /dev/null +++ b/github/integration_installation.go @@ -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 +} diff --git a/github/integration_installation_test.go b/github/integration_installation_test.go new file mode 100644 index 0000000..4c7be1f --- /dev/null +++ b/github/integration_installation_test.go @@ -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) + } +} diff --git a/github/integration_test.go b/github/integration_test.go new file mode 100644 index 0000000..f091cc6 --- /dev/null +++ b/github/integration_test.go @@ -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) + } +}