Skip to content

Commit 7c1a5f3

Browse files
author
Erik Nobel
authored
Add new method: ListEnabledReposInOrg for Actions/Runner (google#1653)
1 parent b02f47a commit 7c1a5f3

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

github/actions_runners.go

+30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ type RunnerApplicationDownload struct {
1818
Filename *string `json:"filename,omitempty"`
1919
}
2020

21+
// ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled.
22+
type ActionsEnabledOnOrgRepos struct {
23+
TotalCount int `json:"total_count"`
24+
Repositories []*Repository `json:"repositories"`
25+
}
26+
2127
// ListRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run.
2228
//
2329
// GitHub API docs: https://docs.github.com/en/rest/reference/actions/#list-runner-applications-for-a-repository
@@ -232,6 +238,30 @@ func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner stri
232238
return runners, resp, nil
233239
}
234240

241+
// ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization.
242+
//
243+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#list-selected-repositories-enabled-for-github-actions-in-an-organization
244+
func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) {
245+
u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner)
246+
u, err := addOptions(u, opts)
247+
if err != nil {
248+
return nil, nil, err
249+
}
250+
251+
req, err := s.client.NewRequest("GET", u, nil)
252+
if err != nil {
253+
return nil, nil, err
254+
}
255+
256+
repos := &ActionsEnabledOnOrgRepos{}
257+
resp, err := s.client.Do(ctx, req, repos)
258+
if err != nil {
259+
return nil, resp, err
260+
}
261+
262+
return repos, resp, nil
263+
}
264+
235265
// GetOrganizationRunner gets a specific self-hosted runner for an organization using its runner ID.
236266
//
237267
// GitHub API docs: https://docs.github.com/en/rest/reference/actions/#get-a-self-hosted-runner-for-an-organization

github/actions_runners_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,56 @@ func TestActionsService_ListOrganizationRunners(t *testing.T) {
225225
}
226226
}
227227

228+
func TestActionsService_ListEnabledReposInOrg(t *testing.T) {
229+
client, mux, _, teardown := setup()
230+
defer teardown()
231+
232+
mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) {
233+
testMethod(t, r, "GET")
234+
testFormValues(t, r, values{
235+
"page": "1",
236+
})
237+
fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":2}, {"id": 3}]}`)
238+
})
239+
240+
ctx := context.Background()
241+
opt := &ListOptions{
242+
Page: 1,
243+
}
244+
got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
245+
if err != nil {
246+
t.Errorf("Actions.ListEnabledReposInOrg returned error: %v", err)
247+
}
248+
249+
want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{
250+
{ID: Int64(2)},
251+
{ID: Int64(3)},
252+
}}
253+
if !reflect.DeepEqual(got, want) {
254+
t.Errorf("Actions.ListEnabledReposInOrg returned %+v, want %+v", got, want)
255+
}
256+
257+
// Test addOptions failure
258+
_, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt)
259+
if err == nil {
260+
t.Error("bad options ListEnabledReposInOrg err = nil, want error")
261+
}
262+
263+
// Test s.client.Do failure
264+
client.BaseURL.Path = "/api-v3/"
265+
client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute)
266+
got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
267+
if got != nil {
268+
t.Errorf("rate.Reset.Time > now ListEnabledReposInOrg = %#v, want nil", got)
269+
}
270+
if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
271+
t.Errorf("rate.Reset.Time > now ListEnabledReposInOrg resp = %#v, want StatusCode=%v", resp.Response, want)
272+
}
273+
if err == nil {
274+
t.Error("rate.Reset.Time > now ListEnabledReposInOrg err = nil, want error")
275+
}
276+
}
277+
228278
func TestActionsService_GetOrganizationRunner(t *testing.T) {
229279
client, mux, _, teardown := setup()
230280
defer teardown()

0 commit comments

Comments
 (0)