Skip to content

Commit 3646f33

Browse files
authored
feat: Add private repo workflows permission API support (#3679)
1 parent f68f136 commit 3646f33

10 files changed

+521
-0
lines changed

github/actions_permissions_enterprise.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,40 @@ func (s *ActionsService) EditSelfHostedRunnerPermissionsInEnterprise(ctx context
338338

339339
return s.client.Do(ctx, req, nil)
340340
}
341+
342+
// GetPrivateRepoForkPRWorkflowSettingsInEnterprise gets the settings for whether workflows from fork pull requests can run on private repositories in an enterprise.
343+
//
344+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-private-repo-fork-pr-workflow-settings-for-an-enterprise
345+
//
346+
//meta:operation GET /enterprises/{enterprise}/actions/permissions/fork-pr-workflows-private-repos
347+
func (s *ActionsService) GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string) (*WorkflowsPermissions, *Response, error) {
348+
u := fmt.Sprintf("enterprises/%v/actions/permissions/fork-pr-workflows-private-repos", enterprise)
349+
350+
req, err := s.client.NewRequest("GET", u, nil)
351+
if err != nil {
352+
return nil, nil, err
353+
}
354+
355+
permissions := new(WorkflowsPermissions)
356+
resp, err := s.client.Do(ctx, req, permissions)
357+
if err != nil {
358+
return nil, resp, err
359+
}
360+
361+
return permissions, resp, nil
362+
}
363+
364+
// UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise sets the settings for whether workflows from fork pull requests can run on private repositories in an enterprise.
365+
//
366+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-private-repo-fork-pr-workflow-settings-for-an-enterprise
367+
//
368+
//meta:operation PUT /enterprises/{enterprise}/actions/permissions/fork-pr-workflows-private-repos
369+
func (s *ActionsService) UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string, permissions *WorkflowsPermissionsOpt) (*Response, error) {
370+
u := fmt.Sprintf("enterprises/%v/actions/permissions/fork-pr-workflows-private-repos", enterprise)
371+
req, err := s.client.NewRequest("PUT", u, permissions)
372+
if err != nil {
373+
return nil, err
374+
}
375+
376+
return s.client.Do(ctx, req, nil)
377+
}

github/actions_permissions_enterprise_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,84 @@ func TestActionsService_EditSelfHostedRunnerPermissionsInEnterprise(t *testing.T
523523
return client.Actions.EditSelfHostedRunnerPermissionsInEnterprise(ctx, "e", *input)
524524
})
525525
}
526+
527+
func TestActionsService_GetPrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {
528+
t.Parallel()
529+
client, mux, _ := setup(t)
530+
531+
mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
532+
testMethod(t, r, "GET")
533+
fmt.Fprint(w, `{"run_workflows_from_fork_pull_requests": true, "send_write_tokens_to_workflows": false, "send_secrets_and_variables": true, "require_approval_for_fork_pr_workflows": false}`)
534+
})
535+
536+
ctx := context.Background()
537+
permissions, _, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e")
538+
if err != nil {
539+
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise returned error: %v", err)
540+
}
541+
want := &WorkflowsPermissions{
542+
RunWorkflowsFromForkPullRequests: Ptr(true),
543+
SendWriteTokensToWorkflows: Ptr(false),
544+
SendSecretsAndVariables: Ptr(true),
545+
RequireApprovalForForkPRWorkflows: Ptr(false),
546+
}
547+
if !cmp.Equal(permissions, want) {
548+
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise returned %+v, want %+v", permissions, want)
549+
}
550+
551+
const methodName = "GetPrivateRepoForkPRWorkflowSettingsInEnterprise"
552+
testBadOptions(t, methodName, func() (err error) {
553+
_, _, err = client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "\n")
554+
return err
555+
})
556+
557+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
558+
got, resp, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e")
559+
if got != nil {
560+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
561+
}
562+
return resp, err
563+
})
564+
}
565+
566+
func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {
567+
t.Parallel()
568+
client, mux, _ := setup(t)
569+
570+
input := &WorkflowsPermissionsOpt{
571+
RunWorkflowsFromForkPullRequests: true,
572+
SendWriteTokensToWorkflows: Ptr(false),
573+
SendSecretsAndVariables: Ptr(true),
574+
}
575+
576+
mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
577+
v := new(WorkflowsPermissionsOpt)
578+
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
579+
580+
testMethod(t, r, "PUT")
581+
if !cmp.Equal(v, input) {
582+
t.Errorf("Request body = %+v, want %+v", v, input)
583+
}
584+
w.WriteHeader(http.StatusNoContent)
585+
})
586+
587+
ctx := context.Background()
588+
resp, err := client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e", input)
589+
if err != nil {
590+
t.Errorf("Actions.UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise returned error: %v", err)
591+
}
592+
593+
if resp.StatusCode != http.StatusNoContent {
594+
t.Errorf("Actions.UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise = %d, want %d", resp.StatusCode, http.StatusNoContent)
595+
}
596+
597+
const methodName = "UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise"
598+
testBadOptions(t, methodName, func() (err error) {
599+
_, err = client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "\n", input)
600+
return err
601+
})
602+
603+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
604+
return client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e", input)
605+
})
606+
}

github/actions_permissions_orgs.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,40 @@ func (s *ActionsService) RemoveRepositorySelfHostedRunnersAllowedInOrganization(
446446

447447
return resp, nil
448448
}
449+
450+
// GetPrivateRepoForkPRWorkflowSettingsInOrganization gets the settings for whether workflows from fork pull requests can run on private repositories in an organization.
451+
//
452+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#get-private-repo-fork-pr-workflow-settings-for-an-organization
453+
//
454+
//meta:operation GET /orgs/{org}/actions/permissions/fork-pr-workflows-private-repos
455+
func (s *ActionsService) GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx context.Context, org string) (*WorkflowsPermissions, *Response, error) {
456+
u := fmt.Sprintf("orgs/%v/actions/permissions/fork-pr-workflows-private-repos", org)
457+
458+
req, err := s.client.NewRequest("GET", u, nil)
459+
if err != nil {
460+
return nil, nil, err
461+
}
462+
463+
permissions := new(WorkflowsPermissions)
464+
resp, err := s.client.Do(ctx, req, permissions)
465+
if err != nil {
466+
return nil, resp, err
467+
}
468+
469+
return permissions, resp, nil
470+
}
471+
472+
// UpdatePrivateRepoForkPRWorkflowSettingsInOrganization sets the settings for whether workflows from fork pull requests can run on private repositories in an organization.
473+
//
474+
// GitHub API docs: https://docs.github.com/rest/actions/permissions#set-private-repo-fork-pr-workflow-settings-for-an-organization
475+
//
476+
//meta:operation PUT /orgs/{org}/actions/permissions/fork-pr-workflows-private-repos
477+
func (s *ActionsService) UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(ctx context.Context, org string, permissions *WorkflowsPermissionsOpt) (*Response, error) {
478+
u := fmt.Sprintf("orgs/%v/actions/permissions/fork-pr-workflows-private-repos", org)
479+
req, err := s.client.NewRequest("PUT", u, permissions)
480+
if err != nil {
481+
return nil, err
482+
}
483+
484+
return s.client.Do(ctx, req, nil)
485+
}

github/actions_permissions_orgs_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,84 @@ func TestActionsService_RemoveRepositorySelfHostedRunnersAllowedInOrganization(t
693693
return client.Actions.RemoveRepositorySelfHostedRunnersAllowedInOrganization(ctx, "o", 123)
694694
})
695695
}
696+
697+
func TestActionsService_GetPrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {
698+
t.Parallel()
699+
client, mux, _ := setup(t)
700+
701+
mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
702+
testMethod(t, r, "GET")
703+
fmt.Fprint(w, `{"run_workflows_from_fork_pull_requests": true, "send_write_tokens_to_workflows": false, "send_secrets_and_variables": true, "require_approval_for_fork_pr_workflows": false}`)
704+
})
705+
706+
ctx := context.Background()
707+
permissions, _, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o")
708+
if err != nil {
709+
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization returned error: %v", err)
710+
}
711+
want := &WorkflowsPermissions{
712+
RunWorkflowsFromForkPullRequests: Ptr(true),
713+
SendWriteTokensToWorkflows: Ptr(false),
714+
SendSecretsAndVariables: Ptr(true),
715+
RequireApprovalForForkPRWorkflows: Ptr(false),
716+
}
717+
if !cmp.Equal(permissions, want) {
718+
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization returned %+v, want %+v", permissions, want)
719+
}
720+
721+
const methodName = "GetPrivateRepoForkPRWorkflowSettingsInOrganization"
722+
testBadOptions(t, methodName, func() (err error) {
723+
_, _, err = client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "\n")
724+
return err
725+
})
726+
727+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
728+
got, resp, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o")
729+
if got != nil {
730+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
731+
}
732+
return resp, err
733+
})
734+
}
735+
736+
func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {
737+
t.Parallel()
738+
client, mux, _ := setup(t)
739+
740+
input := &WorkflowsPermissionsOpt{
741+
RunWorkflowsFromForkPullRequests: true,
742+
SendWriteTokensToWorkflows: Ptr(false),
743+
SendSecretsAndVariables: Ptr(true),
744+
}
745+
746+
mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
747+
v := new(WorkflowsPermissionsOpt)
748+
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
749+
750+
testMethod(t, r, "PUT")
751+
if !cmp.Equal(v, input) {
752+
t.Errorf("Request body = %+v, want %+v", v, input)
753+
}
754+
w.WriteHeader(http.StatusNoContent)
755+
})
756+
757+
ctx := context.Background()
758+
resp, err := client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o", input)
759+
if err != nil {
760+
t.Errorf("Actions.UpdatePrivateRepoForkPRWorkflowSettingsInOrganization returned error: %v", err)
761+
}
762+
763+
if resp.StatusCode != http.StatusNoContent {
764+
t.Errorf("Actions.UpdatePrivateRepoForkPRWorkflowSettingsInOrganization = %d, want %d", resp.StatusCode, http.StatusNoContent)
765+
}
766+
767+
const methodName = "UpdatePrivateRepoForkPRWorkflowSettingsInOrganization"
768+
testBadOptions(t, methodName, func() (err error) {
769+
_, err = client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "\n", input)
770+
return err
771+
})
772+
773+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
774+
return client.Actions.UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o", input)
775+
})
776+
}

github/actions_workflows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ type CreateWorkflowDispatchEventRequest struct {
5656
Inputs map[string]any `json:"inputs,omitempty"`
5757
}
5858

59+
// WorkflowsPermissions represents the permissions for workflows in a repository.
60+
type WorkflowsPermissions struct {
61+
RunWorkflowsFromForkPullRequests *bool `json:"run_workflows_from_fork_pull_requests,omitempty"`
62+
SendWriteTokensToWorkflows *bool `json:"send_write_tokens_to_workflows,omitempty"`
63+
SendSecretsAndVariables *bool `json:"send_secrets_and_variables,omitempty"`
64+
RequireApprovalForForkPRWorkflows *bool `json:"require_approval_for_fork_pr_workflows,omitempty"`
65+
}
66+
67+
func (w WorkflowsPermissions) String() string {
68+
return Stringify(w)
69+
}
70+
71+
// WorkflowsPermissionsOpt specifies options for editing workflows permissions in a repository.
72+
type WorkflowsPermissionsOpt struct {
73+
RunWorkflowsFromForkPullRequests bool `json:"run_workflows_from_fork_pull_requests"`
74+
SendWriteTokensToWorkflows *bool `json:"send_write_tokens_to_workflows,omitempty"`
75+
SendSecretsAndVariables *bool `json:"send_secrets_and_variables,omitempty"`
76+
RequireApprovalForForkPRWorkflows *bool `json:"require_approval_for_fork_pr_workflows,omitempty"`
77+
}
78+
5979
// ListWorkflows lists all workflows in a repository.
6080
//
6181
// GitHub API docs: https://docs.github.com/rest/actions/workflows#list-repository-workflows

github/github-accessors.go

Lines changed: 56 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)