Skip to content

Commit 54d257b

Browse files
authored
feat: Add self-hosted runner permission API support (#3675)
1 parent b0d4e1e commit 54d257b

7 files changed

+558
-0
lines changed

github/actions_permissions_enterprise.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ type DefaultWorkflowPermissionEnterprise struct {
3737
CanApprovePullRequestReviews *bool `json:"can_approve_pull_request_reviews,omitempty"`
3838
}
3939

40+
// SelfHostRunnerPermissionsEnterprise represents the settings for whether organizations in the enterprise are allowed to manage self-hosted runners at the repository level.
41+
type SelfHostRunnerPermissionsEnterprise struct {
42+
DisableSelfHostedRunnersForAllOrgs *bool `json:"disable_self_hosted_runners_for_all_orgs,omitempty"`
43+
}
44+
45+
func (a SelfHostRunnerPermissionsEnterprise) String() string {
46+
return Stringify(a)
47+
}
48+
4049
// GetActionsPermissionsInEnterprise gets the GitHub Actions permissions policy for an enterprise.
4150
//
4251
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-github-actions-permissions-for-an-enterprise
@@ -293,3 +302,39 @@ func (s *ActionsService) EditArtifactAndLogRetentionPeriodInEnterprise(ctx conte
293302

294303
return s.client.Do(ctx, req, nil)
295304
}
305+
306+
// GetSelfHostedRunnerPermissionsInEnterprise gets the self-hosted runner permissions for an enterprise.
307+
//
308+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-self-hosted-runners-permissions-for-an-enterprise
309+
//
310+
//meta:operation GET /enterprises/{enterprise}/actions/permissions/self-hosted-runners
311+
func (s *ActionsService) GetSelfHostedRunnerPermissionsInEnterprise(ctx context.Context, enterprise string) (*SelfHostRunnerPermissionsEnterprise, *Response, error) {
312+
u := fmt.Sprintf("enterprises/%v/actions/permissions/self-hosted-runners", enterprise)
313+
req, err := s.client.NewRequest("GET", u, nil)
314+
if err != nil {
315+
return nil, nil, err
316+
}
317+
318+
permissions := new(SelfHostRunnerPermissionsEnterprise)
319+
resp, err := s.client.Do(ctx, req, permissions)
320+
if err != nil {
321+
return nil, resp, err
322+
}
323+
324+
return permissions, resp, nil
325+
}
326+
327+
// EditSelfHostedRunnerPermissionsInEnterprise sets the self-hosted runner permissions for an enterprise.
328+
//
329+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-self-hosted-runners-permissions-for-an-enterprise
330+
//
331+
//meta:operation PUT /enterprises/{enterprise}/actions/permissions/self-hosted-runners
332+
func (s *ActionsService) EditSelfHostedRunnerPermissionsInEnterprise(ctx context.Context, enterprise string, permissions SelfHostRunnerPermissionsEnterprise) (*Response, error) {
333+
u := fmt.Sprintf("enterprises/%v/actions/permissions/self-hosted-runners", enterprise)
334+
req, err := s.client.NewRequest("PUT", u, permissions)
335+
if err != nil {
336+
return nil, err
337+
}
338+
339+
return s.client.Do(ctx, req, nil)
340+
}

github/actions_permissions_enterprise_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,75 @@ func TestActionsService_EditArtifactAndLogRetentionPeriodInEnterprise(t *testing
451451
return client.Actions.EditArtifactAndLogRetentionPeriodInEnterprise(ctx, "e", *input)
452452
})
453453
}
454+
455+
func TestActionsService_GetSelfHostedRunnerPermissionsInEnterprise(t *testing.T) {
456+
t.Parallel()
457+
client, mux, _ := setup(t)
458+
459+
mux.HandleFunc("/enterprises/e/actions/permissions/self-hosted-runners", func(w http.ResponseWriter, r *http.Request) {
460+
testMethod(t, r, "GET")
461+
fmt.Fprint(w, `{"disable_self_hosted_runners_for_all_orgs": true}`)
462+
})
463+
464+
ctx := context.Background()
465+
permissions, _, err := client.Actions.GetSelfHostedRunnerPermissionsInEnterprise(ctx, "e")
466+
if err != nil {
467+
t.Errorf("Actions.GetSelfHostedRunnerPermissionsInEnterprise returned error: %v", err)
468+
}
469+
want := &SelfHostRunnerPermissionsEnterprise{DisableSelfHostedRunnersForAllOrgs: Ptr(true)}
470+
if !cmp.Equal(permissions, want) {
471+
t.Errorf("Actions.GetSelfHostedRunnerPermissionsInEnterprise returned %+v, want %+v", permissions, want)
472+
}
473+
474+
const methodName = "GetSelfHostedRunnerPermissionsInEnterprise"
475+
testBadOptions(t, methodName, func() (err error) {
476+
_, _, err = client.Actions.GetSelfHostedRunnerPermissionsInEnterprise(ctx, "\n")
477+
return err
478+
})
479+
480+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
481+
got, resp, err := client.Actions.GetSelfHostedRunnerPermissionsInEnterprise(ctx, "e")
482+
if got != nil {
483+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
484+
}
485+
return resp, err
486+
})
487+
}
488+
489+
func TestActionsService_EditSelfHostedRunnerPermissionsInEnterprise(t *testing.T) {
490+
t.Parallel()
491+
client, mux, _ := setup(t)
492+
493+
input := &SelfHostRunnerPermissionsEnterprise{DisableSelfHostedRunnersForAllOrgs: Ptr(false)}
494+
495+
mux.HandleFunc("/enterprises/e/actions/permissions/self-hosted-runners", func(w http.ResponseWriter, r *http.Request) {
496+
v := new(SelfHostRunnerPermissionsEnterprise)
497+
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
498+
499+
testMethod(t, r, "PUT")
500+
if !cmp.Equal(v, input) {
501+
t.Errorf("Request body = %+v, want %+v", v, input)
502+
}
503+
w.WriteHeader(http.StatusNoContent)
504+
})
505+
506+
ctx := context.Background()
507+
resp, err := client.Actions.EditSelfHostedRunnerPermissionsInEnterprise(ctx, "e", *input)
508+
if err != nil {
509+
t.Errorf("Actions.EditSelfHostedRunnerPermissionsInEnterprise returned error: %v", err)
510+
}
511+
512+
if resp.StatusCode != http.StatusNoContent {
513+
t.Errorf("Actions.EditSelfHostedRunnerPermissionsInEnterprise = %d, want %d", resp.StatusCode, http.StatusNoContent)
514+
}
515+
516+
const methodName = "EditSelfHostedRunnerPermissionsInEnterprise"
517+
testBadOptions(t, methodName, func() (err error) {
518+
_, err = client.Actions.EditSelfHostedRunnerPermissionsInEnterprise(ctx, "\n", *input)
519+
return err
520+
})
521+
522+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
523+
return client.Actions.EditSelfHostedRunnerPermissionsInEnterprise(ctx, "e", *input)
524+
})
525+
}

github/actions_permissions_orgs.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ type DefaultWorkflowPermissionOrganization struct {
5050
CanApprovePullRequestReviews *bool `json:"can_approve_pull_request_reviews,omitempty"`
5151
}
5252

53+
// SelfHostedRunnersSettingsOrganization represents the self-hosted runners permissions settings for repositories in an organization.
54+
type SelfHostedRunnersSettingsOrganization struct {
55+
EnabledRepositories *string `json:"enabled_repositories,omitempty"`
56+
SelectedRepositoriesURL *string `json:"selected_repositories_url,omitempty"`
57+
}
58+
59+
func (s SelfHostedRunnersSettingsOrganization) String() string {
60+
return Stringify(s)
61+
}
62+
63+
// SelfHostedRunnersSettingsOrganizationOpt specifies the self-hosted runners permissions settings for repositories in an organization.
64+
type SelfHostedRunnersSettingsOrganizationOpt struct {
65+
EnabledRepositories *string `json:"enabled_repositories,omitempty"`
66+
}
67+
5368
// GetActionsPermissions gets the GitHub Actions permissions policy for repositories and allowed actions in an organization.
5469
//
5570
// GitHub API docs: https://docs.github.com/rest/actions/permissions#get-github-actions-permissions-for-an-organization
@@ -306,3 +321,128 @@ func (s *ActionsService) EditArtifactAndLogRetentionPeriodInOrganization(ctx con
306321

307322
return s.client.Do(ctx, req, nil)
308323
}
324+
325+
// GetSelfHostedRunnersSettingsInOrganization gets the self-hosted runners permissions settings for repositories in an organization.
326+
//
327+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#get-self-hosted-runners-settings-for-an-organization
328+
//
329+
//meta:operation GET /orgs/{org}/actions/permissions/self-hosted-runners
330+
func (s *ActionsService) GetSelfHostedRunnersSettingsInOrganization(ctx context.Context, org string) (*SelfHostedRunnersSettingsOrganization, *Response, error) {
331+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners", org)
332+
333+
req, err := s.client.NewRequest("GET", u, nil)
334+
if err != nil {
335+
return nil, nil, err
336+
}
337+
338+
settings := new(SelfHostedRunnersSettingsOrganization)
339+
resp, err := s.client.Do(ctx, req, settings)
340+
if err != nil {
341+
return nil, resp, err
342+
}
343+
344+
return settings, resp, nil
345+
}
346+
347+
// EditSelfHostedRunnersSettingsInOrganization sets the self-hosted runners permissions settings for repositories in an organization.
348+
//
349+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#set-self-hosted-runners-settings-for-an-organization
350+
//
351+
//meta:operation PUT /orgs/{org}/actions/permissions/self-hosted-runners
352+
func (s *ActionsService) EditSelfHostedRunnersSettingsInOrganization(ctx context.Context, org string, opt SelfHostedRunnersSettingsOrganizationOpt) (*Response, error) {
353+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners", org)
354+
355+
req, err := s.client.NewRequest("PUT", u, opt)
356+
if err != nil {
357+
return nil, err
358+
}
359+
360+
return s.client.Do(ctx, req, nil)
361+
}
362+
363+
// SelfHostedRunnersAllowedRepos represents the repositories that are allowed to use self-hosted runners in an organization.
364+
type SelfHostedRunnersAllowedRepos struct {
365+
TotalCount int `json:"total_count"`
366+
Repositories []*Repository `json:"repositories"`
367+
}
368+
369+
// ListRepositoriesSelfHostedRunnersAllowedInOrganization lists the repositories that are allowed to use self-hosted runners in an organization.
370+
//
371+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#list-repositories-allowed-to-use-self-hosted-runners-in-an-organization
372+
//
373+
//meta:operation GET /orgs/{org}/actions/permissions/self-hosted-runners/repositories
374+
func (s *ActionsService) ListRepositoriesSelfHostedRunnersAllowedInOrganization(ctx context.Context, org string, opts *ListOptions) (*SelfHostedRunnersAllowedRepos, *Response, error) {
375+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners/repositories", org)
376+
u, err := addOptions(u, opts)
377+
if err != nil {
378+
return nil, nil, err
379+
}
380+
381+
req, err := s.client.NewRequest("GET", u, nil)
382+
if err != nil {
383+
return nil, nil, err
384+
}
385+
386+
settings := new(SelfHostedRunnersAllowedRepos)
387+
resp, err := s.client.Do(ctx, req, settings)
388+
if err != nil {
389+
return nil, resp, err
390+
}
391+
392+
return settings, resp, nil
393+
}
394+
395+
// SetRepositoriesSelfHostedRunnersAllowedInOrganization allows the list of repositories to use self-hosted runners in an organization.
396+
//
397+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#set-repositories-allowed-to-use-self-hosted-runners-in-an-organization
398+
//
399+
//meta:operation PUT /orgs/{org}/actions/permissions/self-hosted-runners/repositories
400+
func (s *ActionsService) SetRepositoriesSelfHostedRunnersAllowedInOrganization(ctx context.Context, org string, repositoryIDs []int64) (*Response, error) {
401+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners/repositories", org)
402+
403+
req, err := s.client.NewRequest("PUT", u, struct {
404+
IDs []int64 `json:"selected_repository_ids"`
405+
}{IDs: repositoryIDs})
406+
if err != nil {
407+
return nil, err
408+
}
409+
410+
return s.client.Do(ctx, req, nil)
411+
}
412+
413+
// AddRepositorySelfHostedRunnersAllowedInOrganization adds a repository to the list of repositories that are allowed to use self-hosted runners in an organization.
414+
//
415+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#add-a-repository-to-the-list-of-repositories-allowed-to-use-self-hosted-runners-in-an-organization
416+
//
417+
//meta:operation PUT /orgs/{org}/actions/permissions/self-hosted-runners/repositories/{repository_id}
418+
func (s *ActionsService) AddRepositorySelfHostedRunnersAllowedInOrganization(ctx context.Context, org string, repositoryID int64) (*Response, error) {
419+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners/repositories/%v", org, repositoryID)
420+
421+
req, err := s.client.NewRequest("PUT", u, nil)
422+
if err != nil {
423+
return nil, err
424+
}
425+
426+
return s.client.Do(ctx, req, nil)
427+
}
428+
429+
// RemoveRepositorySelfHostedRunnersAllowedInOrganization removes a repository from the list of repositories that are allowed to use self-hosted runners in an organization.
430+
//
431+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#remove-a-repository-from-the-list-of-repositories-allowed-to-use-self-hosted-runners-in-an-organization
432+
//
433+
//meta:operation DELETE /orgs/{org}/actions/permissions/self-hosted-runners/repositories/{repository_id}
434+
func (s *ActionsService) RemoveRepositorySelfHostedRunnersAllowedInOrganization(ctx context.Context, org string, repositoryID int64) (*Response, error) {
435+
u := fmt.Sprintf("orgs/%v/actions/permissions/self-hosted-runners/repositories/%v", org, repositoryID)
436+
437+
req, err := s.client.NewRequest("DELETE", u, nil)
438+
if err != nil {
439+
return nil, err
440+
}
441+
442+
resp, err := s.client.Do(ctx, req, nil)
443+
if err != nil {
444+
return resp, err
445+
}
446+
447+
return resp, nil
448+
}

0 commit comments

Comments
 (0)