-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Add GitHub Enterprise App installation repository management APIs #3831
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
Open
nithish-95
wants to merge
5
commits into
google:master
Choose a base branch
from
nithish-95:issues-3829-p2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c29684f
feat: add enterprise app installation repository endpoints;
nithish-95 d06edea
fix: resolve linter issues from script/lint.sh
nithish-95 3740182
fix: address code review feadback
nithish-95 da913d0
feat: add `AccessibleRepository` struct and update `ListRepositoriesF…
nithish-95 1a69bbf
fix: update ToggleInstallationRepositories to return Installation ins…
nithish-95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright 2025 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 ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // EnterpriseInstallationRepositoriesOptions specifies the parameters for | ||
| // EnterpriseService.AddRepositoriesToInstallation and | ||
| // EnterpriseService.RemoveRepositoriesFromInstallation. | ||
| type EnterpriseInstallationRepositoriesOptions struct { | ||
| SelectedRepositoryIDs []int64 `json:"selected_repository_ids"` | ||
| } | ||
|
|
||
| // EnterpriseInstallationRepositoriesToggleOptions specifies the parameters for | ||
| // EnterpriseService.ToggleInstallationRepositories. | ||
| type EnterpriseInstallationRepositoriesToggleOptions struct { | ||
| RepositorySelection *string `json:"repository_selection,omitempty"` // Can be "all" or "selected" | ||
| SelectedRepositoryIDs []int64 `json:"selected_repository_ids,omitempty"` | ||
| } | ||
|
|
||
| // ListRepositoriesForOrgInstallation lists the repositories that an enterprise app installation | ||
| // has access to on an organization. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/organization-installations#get-the-repositories-accessible-to-a-given-github-app-installation | ||
| // | ||
| //meta:operation GET /enterprises/{enterprise}/apps/organizations/{org}/installations/{installation_id}/repositories | ||
| func (s *EnterpriseService) ListRepositoriesForOrgInstallation(ctx context.Context, enterprise, org string, installationID int64, opts *ListOptions) ([]*AccessibleRepository, *Response, error) { | ||
| u := fmt.Sprintf("enterprises/%v/apps/organizations/%v/installations/%v/repositories", enterprise, org, installationID) | ||
| u, err := addOptions(u, opts) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| req, err := s.client.NewRequest("GET", u, nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var r []*AccessibleRepository | ||
| resp, err := s.client.Do(ctx, req, &r) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return r, resp, nil | ||
| } | ||
|
|
||
| // ToggleInstallationRepositories changes a GitHub App installation's repository access | ||
| // between all repositories and a selected set. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/organization-installations#toggle-installation-repository-access-between-selected-and-all-repositories | ||
| // | ||
| //meta:operation PATCH /enterprises/{enterprise}/apps/organizations/{org}/installations/{installation_id}/repositories | ||
| func (s *EnterpriseService) ToggleInstallationRepositories(ctx context.Context, enterprise, org string, installationID int64, opts EnterpriseInstallationRepositoriesToggleOptions) (*Installation, *Response, error) { | ||
| u := fmt.Sprintf("enterprises/%v/apps/organizations/%v/installations/%v/repositories", enterprise, org, installationID) | ||
| req, err := s.client.NewRequest("PATCH", u, opts) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var r *Installation | ||
| resp, err := s.client.Do(ctx, req, &r) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return r, resp, nil | ||
| } | ||
|
|
||
| // AddRepositoriesToInstallation grants repository access for a GitHub App installation. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/organization-installations#grant-repository-access-to-an-organization-installation | ||
| // | ||
| //meta:operation PATCH /enterprises/{enterprise}/apps/organizations/{org}/installations/{installation_id}/repositories/add | ||
| func (s *EnterpriseService) AddRepositoriesToInstallation(ctx context.Context, enterprise, org string, installationID int64, opts EnterpriseInstallationRepositoriesOptions) (*ListRepositories, *Response, error) { | ||
| u := fmt.Sprintf("enterprises/%v/apps/organizations/%v/installations/%v/repositories/add", enterprise, org, installationID) | ||
| req, err := s.client.NewRequest("PATCH", u, opts) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var r *ListRepositories | ||
| resp, err := s.client.Do(ctx, req, &r) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return r, resp, nil | ||
| } | ||
|
|
||
| // RemoveRepositoriesFromInstallation revokes repository access from a GitHub App installation. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/organization-installations#remove-repository-access-from-an-organization-installation | ||
| // | ||
| //meta:operation PATCH /enterprises/{enterprise}/apps/organizations/{org}/installations/{installation_id}/repositories/remove | ||
| func (s *EnterpriseService) RemoveRepositoriesFromInstallation(ctx context.Context, enterprise, org string, installationID int64, opts EnterpriseInstallationRepositoriesOptions) (*ListRepositories, *Response, error) { | ||
nithish-95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| u := fmt.Sprintf("enterprises/%v/apps/organizations/%v/installations/%v/repositories/remove", enterprise, org, installationID) | ||
| req, err := s.client.NewRequest("PATCH", u, opts) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var r *ListRepositories | ||
| resp, err := s.client.Do(ctx, req, &r) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return r, resp, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright 2025 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" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestEnterpriseService_ListRepositoriesForOrgInstallation(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "GET") | ||
| testFormValues(t, r, values{"page": "1"}) | ||
| fmt.Fprint(w, `[{"id":1}]`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| repos, _, err := client.Enterprise.ListRepositoriesForOrgInstallation(ctx, "e", "o", 1, &ListOptions{Page: 1}) | ||
| if err != nil { | ||
| t.Errorf("Enterprise.ListRepositoriesForOrgInstallation returned error: %v", err) | ||
| } | ||
|
|
||
| want := []*AccessibleRepository{{ID: 1}} | ||
| if diff := cmp.Diff(repos, want); diff != "" { | ||
| t.Errorf("Enterprise.ListRepositoriesForOrgInstallation returned diff (-want +got):\n%v", diff) | ||
| } | ||
|
|
||
| const methodName = "ListRepositoriesForOrgInstallation" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Enterprise.ListRepositoriesForOrgInstallation(ctx, "\n", "\n", -1, &ListOptions{}) | ||
| return err | ||
| }) | ||
|
|
||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| _, resp, err := client.Enterprise.ListRepositoriesForOrgInstallation(ctx, "e", "o", 1, &ListOptions{}) | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestEnterpriseService_ToggleInstallationRepositories(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| input := EnterpriseInstallationRepositoriesToggleOptions{ | ||
| RepositorySelection: String("selected"), | ||
| SelectedRepositoryIDs: []int64{1, 2}, | ||
| } | ||
|
|
||
| mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "PATCH") | ||
| testBody(t, r, `{"repository_selection":"selected","selected_repository_ids":[1,2]}`+"\n") | ||
| fmt.Fprint(w, `{"id":1, "repository_selection":"selected"}`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| inst, _, err := client.Enterprise.ToggleInstallationRepositories(ctx, "e", "o", 1, input) | ||
| if err != nil { | ||
| t.Errorf("Enterprise.ToggleInstallationRepositories returned error: %v", err) | ||
| } | ||
|
|
||
| want := &Installation{ID: Ptr(int64(1)), RepositorySelection: Ptr("selected")} | ||
| if diff := cmp.Diff(inst, want); diff != "" { | ||
| t.Errorf("Enterprise.ToggleInstallationRepositories returned diff (-want +got):\n%v", diff) | ||
| } | ||
|
|
||
| const methodName = "ToggleInstallationRepositories" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Enterprise.ToggleInstallationRepositories(ctx, "\n", "\n", -1, input) | ||
| return err | ||
| }) | ||
|
|
||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| _, resp, err := client.Enterprise.ToggleInstallationRepositories(ctx, "e", "o", 1, input) | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestEnterpriseService_AddRepositoriesToInstallation(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| input := EnterpriseInstallationRepositoriesOptions{SelectedRepositoryIDs: []int64{1, 2}} | ||
|
|
||
| mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/add", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "PATCH") | ||
| testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") | ||
| fmt.Fprint(w, `{"total_count":2, "repositories":[{"id":1},{"id":2}]}`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| repos, _, err := client.Enterprise.AddRepositoriesToInstallation(ctx, "e", "o", 1, input) | ||
| if err != nil { | ||
| t.Errorf("Enterprise.AddRepositoriesToInstallation returned error: %v", err) | ||
| } | ||
|
|
||
| want := &ListRepositories{TotalCount: Ptr(2), Repositories: []*Repository{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}} | ||
| if diff := cmp.Diff(repos, want); diff != "" { | ||
| t.Errorf("Enterprise.AddRepositoriesToInstallation returned diff (-want +got):\n%v", diff) | ||
| } | ||
|
|
||
| const methodName = "AddRepositoriesToInstallation" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Enterprise.AddRepositoriesToInstallation(ctx, "\n", "\n", -1, input) | ||
| return err | ||
| }) | ||
|
|
||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| _, resp, err := client.Enterprise.AddRepositoriesToInstallation(ctx, "e", "o", 1, input) | ||
| return resp, err | ||
| }) | ||
| } | ||
|
|
||
| func TestEnterpriseService_RemoveRepositoriesFromInstallation(t *testing.T) { | ||
| t.Parallel() | ||
| client, mux, _ := setup(t) | ||
|
|
||
| input := EnterpriseInstallationRepositoriesOptions{SelectedRepositoryIDs: []int64{1, 2}} | ||
|
|
||
| mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/remove", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "PATCH") | ||
| testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") | ||
| fmt.Fprint(w, `{"total_count":2, "repositories":[{"id":1},{"id":2}]}`) | ||
| }) | ||
|
|
||
| ctx := t.Context() | ||
| repos, _, err := client.Enterprise.RemoveRepositoriesFromInstallation(ctx, "e", "o", 1, input) | ||
| if err != nil { | ||
| t.Errorf("Enterprise.RemoveRepositoriesFromInstallation returned error: %v", err) | ||
| } | ||
|
|
||
| want := &ListRepositories{TotalCount: Ptr(2), Repositories: []*Repository{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}} | ||
| if diff := cmp.Diff(repos, want); diff != "" { | ||
| t.Errorf("Enterprise.RemoveRepositoriesFromInstallation returned diff (-want +got):\n%v", diff) | ||
| } | ||
|
|
||
| const methodName = "RemoveRepositoriesFromInstallation" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Enterprise.RemoveRepositoriesFromInstallation(ctx, "\n", "\n", -1, input) | ||
| return err | ||
| }) | ||
|
|
||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| _, resp, err := client.Enterprise.RemoveRepositoriesFromInstallation(ctx, "e", "o", 1, input) | ||
| return resp, err | ||
| }) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.