Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions github/actions_permissions_enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,40 @@ func (s *ActionsService) EditSelfHostedRunnerPermissionsInEnterprise(ctx context

return s.client.Do(ctx, req, nil)
}

// GetPrivateRepoForkPRWorkflowSettingsInEnterprise gets the settings for whether workflows from fork pull requests can run on private repositories in an enterprise.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-private-repo-fork-pr-workflow-settings-for-an-enterprise
//
//meta:operation GET /enterprises/{enterprise}/actions/permissions/fork-pr-workflows-private-repos
func (s *ActionsService) GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string) (*WorkflowsPermissions, *Response, error) {
u := fmt.Sprintf("enterprises/%v/actions/permissions/fork-pr-workflows-private-repos", enterprise)

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

permissions := new(WorkflowsPermissions)
resp, err := s.client.Do(ctx, req, permissions)
if err != nil {
return nil, resp, err
}

return permissions, resp, nil
}

// EditPrivateRepoForkPRWorkflowSettingsInEnterprise sets the settings for whether workflows from fork pull requests can run on private repositories in an enterprise.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-private-repo-fork-pr-workflow-settings-for-an-enterprise
//
//meta:operation PUT /enterprises/{enterprise}/actions/permissions/fork-pr-workflows-private-repos
func (s *ActionsService) EditPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string, permissions *WorkflowsPermissionsOpt) (*Response, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s *ActionsService) EditPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string, permissions *WorkflowsPermissionsOpt) (*Response, error) {
func (s *ActionsService) UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(ctx context.Context, enterprise string, permissions *WorkflowsPermissionsOpt) (*Response, error) {

I think update makes more sense than edit both here and in the wider context of the package API.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right, @stevehipwell, thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's OK, I'll rename it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or may I open another pr to do that? For the permissons APIs above, methods have been named GetXXX and EditXXX where both GET and PUT methods are used for the same path. I think we better rename it all at once to keep consistency.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I renamed the methods implemented in this pr for now.

u := fmt.Sprintf("enterprises/%v/actions/permissions/fork-pr-workflows-private-repos", enterprise)
req, err := s.client.NewRequest("PUT", u, permissions)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
81 changes: 81 additions & 0 deletions github/actions_permissions_enterprise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,84 @@ func TestActionsService_EditSelfHostedRunnerPermissionsInEnterprise(t *testing.T
return client.Actions.EditSelfHostedRunnerPermissionsInEnterprise(ctx, "e", *input)
})
}

func TestActionsService_GetPrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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}`)
})

ctx := context.Background()
permissions, _, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e")
if err != nil {
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise returned error: %v", err)
}
want := &WorkflowsPermissions{
RunWorkflowsFromForkPullRequests: Ptr(true),
SendWriteTokensToWorkflows: Ptr(false),
SendSecretsAndVariables: Ptr(true),
RequireApprovalForForkPRWorkflows: Ptr(false),
}
if !cmp.Equal(permissions, want) {
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise returned %+v, want %+v", permissions, want)
}

const methodName = "GetPrivateRepoForkPRWorkflowSettingsInEnterprise"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActionsService_EditPrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestActionsService_EditPrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {
func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(t *testing.T) {

t.Parallel()
client, mux, _ := setup(t)

input := &WorkflowsPermissionsOpt{
RunWorkflowsFromForkPullRequests: true,
SendWriteTokensToWorkflows: Ptr(false),
SendSecretsAndVariables: Ptr(true),
}

mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
v := new(WorkflowsPermissionsOpt)
assertNilError(t, json.NewDecoder(r.Body).Decode(v))

testMethod(t, r, "PUT")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
resp, err := client.Actions.EditPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e", input)
if err != nil {
t.Errorf("Actions.EditPrivateRepoForkPRWorkflowSettingsInEnterprise returned error: %v", err)
}

if resp.StatusCode != http.StatusNoContent {
t.Errorf("Actions.EditPrivateRepoForkPRWorkflowSettingsInEnterprise = %d, want %d", resp.StatusCode, http.StatusNoContent)
}

const methodName = "EditPrivateRepoForkPRWorkflowSettingsInEnterprise"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.EditPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.EditPrivateRepoForkPRWorkflowSettingsInEnterprise(ctx, "e", input)
})
}
37 changes: 37 additions & 0 deletions github/actions_permissions_orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,40 @@ func (s *ActionsService) RemoveRepositorySelfHostedRunnersAllowedInOrganization(

return resp, nil
}

// GetPrivateRepoForkPRWorkflowSettingsInOrganization gets the settings for whether workflows from fork pull requests can run on private repositories in an organization.
//
// GitHub API docs: https://docs.github.com/rest/actions/permissions#get-private-repo-fork-pr-workflow-settings-for-an-organization
//
//meta:operation GET /orgs/{org}/actions/permissions/fork-pr-workflows-private-repos
func (s *ActionsService) GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx context.Context, org string) (*WorkflowsPermissions, *Response, error) {
u := fmt.Sprintf("orgs/%v/actions/permissions/fork-pr-workflows-private-repos", org)

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

permissions := new(WorkflowsPermissions)
resp, err := s.client.Do(ctx, req, permissions)
if err != nil {
return nil, resp, err
}

return permissions, resp, nil
}

// EditPrivateRepoForkPRWorkflowSettingsInOrganization sets the settings for whether workflows from fork pull requests can run on private repositories in an organization.
//
// GitHub API docs: https://docs.github.com/rest/actions/permissions#set-private-repo-fork-pr-workflow-settings-for-an-organization
//
//meta:operation PUT /orgs/{org}/actions/permissions/fork-pr-workflows-private-repos
func (s *ActionsService) EditPrivateRepoForkPRWorkflowSettingsInOrganization(ctx context.Context, org string, permissions *WorkflowsPermissionsOpt) (*Response, error) {
u := fmt.Sprintf("orgs/%v/actions/permissions/fork-pr-workflows-private-repos", org)
req, err := s.client.NewRequest("PUT", u, permissions)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
81 changes: 81 additions & 0 deletions github/actions_permissions_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,84 @@ func TestActionsService_RemoveRepositorySelfHostedRunnersAllowedInOrganization(t
return client.Actions.RemoveRepositorySelfHostedRunnersAllowedInOrganization(ctx, "o", 123)
})
}

func TestActionsService_GetPrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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}`)
})

ctx := context.Background()
permissions, _, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o")
if err != nil {
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization returned error: %v", err)
}
want := &WorkflowsPermissions{
RunWorkflowsFromForkPullRequests: Ptr(true),
SendWriteTokensToWorkflows: Ptr(false),
SendSecretsAndVariables: Ptr(true),
RequireApprovalForForkPRWorkflows: Ptr(false),
}
if !cmp.Equal(permissions, want) {
t.Errorf("Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization returned %+v, want %+v", permissions, want)
}

const methodName = "GetPrivateRepoForkPRWorkflowSettingsInOrganization"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.GetPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActionsService_EditPrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestActionsService_EditPrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {
func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I fix it. Thank you!

t.Parallel()
client, mux, _ := setup(t)

input := &WorkflowsPermissionsOpt{
RunWorkflowsFromForkPullRequests: true,
SendWriteTokensToWorkflows: Ptr(false),
SendSecretsAndVariables: Ptr(true),
}

mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
v := new(WorkflowsPermissionsOpt)
assertNilError(t, json.NewDecoder(r.Body).Decode(v))

testMethod(t, r, "PUT")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
resp, err := client.Actions.EditPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o", input)
if err != nil {
t.Errorf("Actions.EditPrivateRepoForkPRWorkflowSettingsInOrganization returned error: %v", err)
}

if resp.StatusCode != http.StatusNoContent {
t.Errorf("Actions.EditPrivateRepoForkPRWorkflowSettingsInOrganization = %d, want %d", resp.StatusCode, http.StatusNoContent)
}

const methodName = "EditPrivateRepoForkPRWorkflowSettingsInOrganization"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.EditPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.EditPrivateRepoForkPRWorkflowSettingsInOrganization(ctx, "o", input)
})
}
20 changes: 20 additions & 0 deletions github/actions_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ type CreateWorkflowDispatchEventRequest struct {
Inputs map[string]any `json:"inputs,omitempty"`
}

// WorkflowsPermissions represents the permissions for workflows in a repository.
type WorkflowsPermissions struct {
RunWorkflowsFromForkPullRequests *bool `json:"run_workflows_from_fork_pull_requests,omitempty"`
SendWriteTokensToWorkflows *bool `json:"send_write_tokens_to_workflows,omitempty"`
SendSecretsAndVariables *bool `json:"send_secrets_and_variables,omitempty"`
RequireApprovalForForkPRWorkflows *bool `json:"require_approval_for_fork_pr_workflows,omitempty"`
}

func (w WorkflowsPermissions) String() string {
return Stringify(w)
}

// WorkflowsPermissionsOpt specifies options for editing workflows permissions in a repository.
type WorkflowsPermissionsOpt struct {
RunWorkflowsFromForkPullRequests bool `json:"run_workflows_from_fork_pull_requests"`
SendWriteTokensToWorkflows *bool `json:"send_write_tokens_to_workflows,omitempty"`
SendSecretsAndVariables *bool `json:"send_secrets_and_variables,omitempty"`
RequireApprovalForForkPRWorkflows *bool `json:"require_approval_for_fork_pr_workflows,omitempty"`
}

// ListWorkflows lists all workflows in a repository.
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows#list-repository-workflows
Expand Down
56 changes: 56 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading