Skip to content

Commit 108f958

Browse files
Allow an empty array of repo ids as a request parameter (#3155)
Fixes: #3106.
1 parent d067824 commit 108f958

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

github/apps.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ type InstallationTokenOptions struct {
5656
Permissions *InstallationPermissions `json:"permissions,omitempty"`
5757
}
5858

59+
type InstallationTokenListRepoOptions struct {
60+
// The IDs of the repositories that the installation token can access.
61+
// Providing repository IDs restricts the access of an installation token to specific repositories.
62+
RepositoryIDs []int64 `json:"repository_ids"`
63+
64+
// The names of the repositories that the installation token can access.
65+
// Providing repository names restricts the access of an installation token to specific repositories.
66+
Repositories []string `json:"repositories,omitempty"`
67+
68+
// The permissions granted to the access token.
69+
// The permissions object includes the permission names and their access type.
70+
Permissions *InstallationPermissions `json:"permissions,omitempty"`
71+
}
72+
5973
// InstallationPermissions lists the repository and organization permissions for an installation.
6074
//
6175
// Permission names taken from:
@@ -344,6 +358,30 @@ func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opt
344358
return t, resp, nil
345359
}
346360

361+
// CreateInstallationTokenListRepos creates a new installation token with a list of all repositories in an installation which is not possible with CreateInstallationToken.
362+
//
363+
// It differs from CreateInstallationToken by taking InstallationTokenListRepoOptions as a parameter which does not omit RepositoryIDs if that field is nil or an empty array.
364+
//
365+
// GitHub API docs: https://docs.github.com/rest/apps/apps#create-an-installation-access-token-for-an-app
366+
//
367+
//meta:operation POST /app/installations/{installation_id}/access_tokens
368+
func (s *AppsService) CreateInstallationTokenListRepos(ctx context.Context, id int64, opts *InstallationTokenListRepoOptions) (*InstallationToken, *Response, error) {
369+
u := fmt.Sprintf("app/installations/%v/access_tokens", id)
370+
371+
req, err := s.client.NewRequest("POST", u, opts)
372+
if err != nil {
373+
return nil, nil, err
374+
}
375+
376+
t := new(InstallationToken)
377+
resp, err := s.client.Do(ctx, req, t)
378+
if err != nil {
379+
return nil, resp, err
380+
}
381+
382+
return t, resp, nil
383+
}
384+
347385
// CreateAttachment creates a new attachment on user comment containing a url.
348386
//
349387
// GitHub API docs: https://docs.github.com/enterprise-server@3.3/rest/reference/apps#create-a-content-attachment

github/apps_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,77 @@ func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) {
469469
}
470470
}
471471

472+
func TestAppsService_CreateInstallationTokenListReposWithOptions(t *testing.T) {
473+
client, mux, _, teardown := setup()
474+
defer teardown()
475+
476+
installationTokenListRepoOptions := &InstallationTokenListRepoOptions{
477+
Repositories: []string{"foo"},
478+
Permissions: &InstallationPermissions{
479+
Contents: String("write"),
480+
Issues: String("read"),
481+
},
482+
}
483+
484+
mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
485+
v := new(InstallationTokenListRepoOptions)
486+
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
487+
488+
if !cmp.Equal(v, installationTokenListRepoOptions) {
489+
t.Errorf("request sent %+v, want %+v", v, installationTokenListRepoOptions)
490+
}
491+
492+
testMethod(t, r, "POST")
493+
fmt.Fprint(w, `{"token":"t"}`)
494+
})
495+
496+
ctx := context.Background()
497+
token, _, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, installationTokenListRepoOptions)
498+
if err != nil {
499+
t.Errorf("Apps.CreateInstallationTokenListRepos returned error: %v", err)
500+
}
501+
502+
want := &InstallationToken{Token: String("t")}
503+
if !cmp.Equal(token, want) {
504+
t.Errorf("Apps.CreateInstallationTokenListRepos returned %+v, want %+v", token, want)
505+
}
506+
}
507+
508+
func TestAppsService_CreateInstallationTokenListReposWithNoOptions(t *testing.T) {
509+
client, mux, _, teardown := setup()
510+
defer teardown()
511+
512+
mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
513+
testMethod(t, r, "POST")
514+
fmt.Fprint(w, `{"token":"t"}`)
515+
})
516+
517+
ctx := context.Background()
518+
token, _, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, nil)
519+
if err != nil {
520+
t.Errorf("Apps.CreateInstallationTokenListRepos returned error: %v", err)
521+
}
522+
523+
want := &InstallationToken{Token: String("t")}
524+
if !cmp.Equal(token, want) {
525+
t.Errorf("Apps.CreateInstallationTokenListRepos returned %+v, want %+v", token, want)
526+
}
527+
528+
const methodName = "CreateInstallationTokenListRepos"
529+
testBadOptions(t, methodName, func() (err error) {
530+
_, _, err = client.Apps.CreateInstallationTokenListRepos(ctx, -1, nil)
531+
return err
532+
})
533+
534+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
535+
got, resp, err := client.Apps.CreateInstallationTokenListRepos(ctx, 1, nil)
536+
if got != nil {
537+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
538+
}
539+
return resp, err
540+
})
541+
}
542+
472543
func TestAppsService_CreateAttachement(t *testing.T) {
473544
client, mux, _, teardown := setup()
474545
defer teardown()

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)