From 2c8a998409c709558ebac5449aeb96892dbc5a6f Mon Sep 17 00:00:00 2001 From: Matthew Reidy Date: Fri, 26 Apr 2024 09:57:37 -0700 Subject: [PATCH] issue3106 changes --- github/apps.go | 36 +++++++++++++++++ github/apps_test.go | 71 +++++++++++++++++++++++++++++++++ github/github-accessors.go | 8 ++++ github/github-accessors_test.go | 7 ++++ 4 files changed, 122 insertions(+) diff --git a/github/apps.go b/github/apps.go index 2ebfee505c..6a2550f472 100644 --- a/github/apps.go +++ b/github/apps.go @@ -56,6 +56,20 @@ type InstallationTokenOptions struct { Permissions *InstallationPermissions `json:"permissions,omitempty"` } +type InstallationTokenListRepoOptions 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"` + + // The names of the repositories that the installation token can access. + // Providing repository names restricts the access of an installation token to specific repositories. + Repositories []string `json:"repositories,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. // // Permission names taken from: @@ -344,6 +358,28 @@ func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opt return t, resp, nil } +// CreateInstallationTokenListRepos creates a new installation token with an empty repository list as a parameter. +// +// GitHub API docs: https://docs.github.com/rest/apps/apps#create-an-installation-access-token-for-an-app +// +//meta:operation POST /app/installations/{installation_id}/access_tokens +func (s *AppsService) CreateInstallationTokenListRepos(ctx context.Context, id int64, opts *InstallationTokenListRepoOptions) (*InstallationToken, *Response, error) { + u := fmt.Sprintf("app/installations/%v/access_tokens", id) + + req, err := s.client.NewRequest("POST", u, opts) + if err != nil { + return nil, nil, err + } + + t := new(InstallationToken) + resp, err := s.client.Do(ctx, req, t) + if err != nil { + return nil, resp, err + } + + return t, resp, nil +} + // CreateAttachment creates a new attachment on user comment containing a url. // // GitHub API docs: https://docs.github.com/enterprise-server@3.3/rest/reference/apps#create-a-content-attachment diff --git a/github/apps_test.go b/github/apps_test.go index a58326ea5e..ea67034459 100644 --- a/github/apps_test.go +++ b/github/apps_test.go @@ -469,6 +469,77 @@ func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) { } } +func TestAppsService_CreateInstallationTokenListReposWithOptions(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + installationTokenListRepoOptions := &InstallationTokenListRepoOptions{ + Repositories: []string{"foo"}, + Permissions: &InstallationPermissions{ + Contents: String("write"), + Issues: String("read"), + }, + } + + mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { + v := new(InstallationTokenListRepoOptions) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + if !cmp.Equal(v, installationTokenListRepoOptions) { + t.Errorf("request sent %+v, want %+v", v, installationTokenListRepoOptions) + } + + testMethod(t, r, "POST") + fmt.Fprint(w, `{"token":"t"}`) + }) + + ctx := context.Background() + token, _, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, installationTokenListRepoOptions) + if err != nil { + t.Errorf("Apps.CreateInstallationTokenListRepos returned error: %v", err) + } + + want := &InstallationToken{Token: String("t")} + if !cmp.Equal(token, want) { + t.Errorf("Apps.CreateInstallationTokenListRepos returned %+v, want %+v", token, want) + } +} + +func TestAppsService_CreateInstallationTokenListReposWithNoOptions(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + fmt.Fprint(w, `{"token":"t"}`) + }) + + ctx := context.Background() + token, _, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, nil) + if err != nil { + t.Errorf("Apps.CreateInstallationTokenListRepos returned error: %v", err) + } + + want := &InstallationToken{Token: String("t")} + if !cmp.Equal(token, want) { + t.Errorf("Apps.CreateInstallationTokenListRepos returned %+v, want %+v", token, want) + } + + const methodName = "CreateInstallationTokenListRepos" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Apps.CreateInstallationTokenListRepos(ctx, -1, nil) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, nil) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + func TestAppsService_CreateAttachement(t *testing.T) { client, mux, _, teardown := setup() defer teardown() diff --git a/github/github-accessors.go b/github/github-accessors.go index 13bf7d6668..390ae086d3 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -9062,6 +9062,14 @@ func (i *InstallationToken) GetToken() string { return *i.Token } +// GetPermissions returns the Permissions field. +func (i *InstallationTokenListRepoOptions) GetPermissions() *InstallationPermissions { + if i == nil { + return nil + } + return i.Permissions +} + // GetPermissions returns the Permissions field. func (i *InstallationTokenOptions) GetPermissions() *InstallationPermissions { if i == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 313e92c059..8fafe9a218 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -10608,6 +10608,13 @@ func TestInstallationToken_GetToken(tt *testing.T) { i.GetToken() } +func TestInstallationTokenListRepoOptions_GetPermissions(tt *testing.T) { + i := &InstallationTokenListRepoOptions{} + i.GetPermissions() + i = nil + i.GetPermissions() +} + func TestInstallationTokenOptions_GetPermissions(tt *testing.T) { i := &InstallationTokenOptions{} i.GetPermissions()