Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update apps.go #1238

Merged
merged 18 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions github/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,21 @@ type App struct {

// InstallationToken represents an installation token.
type InstallationToken struct {
Token *string `json:"token,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Token *string `json:"token,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Permissions *InstallationPermissions `json:"permissions,omitempty"`
Repositories []*Repository `json:"repositories,omitempty"`
}

// InstallationTokenOptions allow restricting a token's access to specific repositories.
type InstallationTokenOptions struct {
// The IDs of the repositories that the installation token can access.
// Providing repository IDs restricts the access of an installation token to specific repositories.
RepositoryIDs []int64 `json:"repository_ids,omitempty"`

// The permissions granted to the access token.
// The permissions object includes the permission names and their access type.
Permissions *InstallationPermissions `json:"permissions,omitempty"`
}

// InstallationPermissions lists the repository and organization permissions for an installation.
Expand Down Expand Up @@ -194,10 +207,10 @@ func (s *AppsService) ListUserInstallations(ctx context.Context, opt *ListOption
// CreateInstallationToken creates a new installation token.
//
// GitHub API docs: https://developer.github.com/v3/apps/#create-a-new-installation-token
func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64) (*InstallationToken, *Response, error) {
func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opt *InstallationTokenOptions) (*InstallationToken, *Response, error) {
u := fmt.Sprintf("app/installations/%v/access_tokens", id)

req, err := s.client.NewRequest("POST", u, nil)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
Expand Down
82 changes: 81 additions & 1 deletion github/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
package github

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -214,7 +218,54 @@ func TestAppsService_CreateInstallationToken(t *testing.T) {
fmt.Fprint(w, `{"token":"t"}`)
})

token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1)
token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1, nil)
if err != nil {
t.Errorf("Apps.CreateInstallationToken returned error: %v", err)
}

want := &InstallationToken{Token: String("t")}
if !reflect.DeepEqual(token, want) {
t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want)
}
}

func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

installationTokenOptions := &InstallationTokenOptions{
RepositoryIDs: []int64{1234},
Permissions: &InstallationPermissions{
Contents: String("write"),
Issues: String("read"),
},
}

// Convert InstallationTokenOptions into an io.ReadCloser object for comparison.
wantBody, err := GetReadCloser(installationTokenOptions)
if err != nil {
t.Errorf("GetReadCloser returned error: %v", err)
}

mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
// Read request body contents.
var gotBodyBytes []byte
gotBodyBytes, err = ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("ReadAll returned error: %v", err)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(gotBodyBytes))

if !reflect.DeepEqual(r.Body, wantBody) {
t.Errorf("request sent %+v, want %+v", r.Body, wantBody)
}

testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"token":"t"}`)
})

token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1, installationTokenOptions)
if err != nil {
t.Errorf("Apps.CreateInstallationToken returned error: %v", err)
}
Expand Down Expand Up @@ -247,6 +298,7 @@ func TestAppsService_CreateAttachement(t *testing.T) {
t.Errorf("CreateAttachment = %+v, want %+v", got, want)
}
}

func TestAppsService_FindOrganizationInstallation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down Expand Up @@ -330,3 +382,31 @@ func TestAppsService_FindUserInstallation(t *testing.T) {
t.Errorf("Apps.FindUserInstallation returned %+v, want %+v", installation, want)
}
}

// GetReadWriter converts a body interface into an io.ReadWriter object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I much prefer these two new helper functions be used for testing only... thank you!

In fact, since they are rather general purpose, I would be fine to move them to github_test.go if you want, or we could just leave them here for now and move them in a later PR if they become more generally used.

We'll see what @gauntface thinks about them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome! Will leave them here for now if that is alright.

func GetReadWriter(body interface{}) (io.ReadWriter, error) {
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
enc := json.NewEncoder(buf)
err := enc.Encode(body)
if err != nil {
return nil, err
}
}
return buf, nil
}

// GetReadCloser converts a body interface into an io.ReadCloser object.
func GetReadCloser(body interface{}) (io.ReadCloser, error) {
buf, err := GetReadWriter(body)
if err != nil {
return nil, err
}

all, err := ioutil.ReadAll(buf)
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewBuffer(all)), nil
}
16 changes: 16 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.