diff --git a/github/actions_permissions_enterprise.go b/github/actions_permissions_enterprise.go new file mode 100644 index 00000000000..8311e729438 --- /dev/null +++ b/github/actions_permissions_enterprise.go @@ -0,0 +1,191 @@ +// Copyright 2023 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" +) + +// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. +type ActionsEnabledOnEnterpriseRepos struct { + TotalCount int `json:"total_count"` + Organizations []*Organization `json:"organizations"` +} + +// ActionsPermissionsEnterprise represents a policy for allowed actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions +type ActionsPermissionsEnterprise struct { + EnabledOrganizations *string `json:"enabled_organizations,omitempty"` + AllowedActions *string `json:"allowed_actions,omitempty"` + SelectedActionsURL *string `json:"selected_actions_url,omitempty"` +} + +func (a ActionsPermissionsEnterprise) String() string { + return Stringify(a) +} + +// GetActionsPermissionsInEnterprise gets the GitHub Actions permissions policy for an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-github-actions-permissions-for-an-enterprise +func (s *ActionsService) GetActionsPermissionsInEnterprise(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + permissions := new(ActionsPermissionsEnterprise) + resp, err := s.client.Do(ctx, req, permissions) + if err != nil { + return nil, resp, err + } + + return permissions, resp, nil +} + +// EditActionsPermissionsInEnterprise sets the permissions policy in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-github-actions-permissions-for-an-enterprise +func (s *ActionsService) EditActionsPermissionsInEnterprise(ctx context.Context, enterprise string, actionsPermissionsEnterprise ActionsPermissionsEnterprise) (*ActionsPermissionsEnterprise, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise) + req, err := s.client.NewRequest("PUT", u, actionsPermissionsEnterprise) + if err != nil { + return nil, nil, err + } + + p := new(ActionsPermissionsEnterprise) + resp, err := s.client.Do(ctx, req, p) + if err != nil { + return nil, resp, err + } + + return p, resp, nil +} + +// ListEnabledOrgsInEnterprise lists the selected organizations that are enabled for GitHub Actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#list-selected-organizations-enabled-for-github-actions-in-an-enterprise +func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnEnterpriseRepos, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/organizations", owner) + 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 + } + + orgs := &ActionsEnabledOnEnterpriseRepos{} + resp, err := s.client.Do(ctx, req, orgs) + if err != nil { + return nil, resp, err + } + + return orgs, resp, nil +} + +// SetEnabledOrgsInEnterprise replaces the list of selected organizations that are enabled for GitHub Actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-selected-organizations-enabled-for-github-actions-in-an-enterprise +func (s *ActionsService) SetEnabledOrgsInEnterprise(ctx context.Context, owner string, organizationIDs []int64) (*Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/organizations", owner) + + req, err := s.client.NewRequest("PUT", u, struct { + IDs []int64 `json:"selected_organization_ids"` + }{IDs: organizationIDs}) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// AddEnabledOrgInEnterprise adds an organization to the list of selected organizations that are enabled for GitHub Actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#enable-a-selected-organization-for-github-actions-in-an-enterprise +func (s *ActionsService) AddEnabledOrgInEnterprise(ctx context.Context, owner string, organizationID int64) (*Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/organizations/%v", owner, organizationID) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// RemoveEnabledOrgInEnterprise removes an organization from the list of selected organizations that are enabled for GitHub Actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#disable-a-selected-organization-for-github-actions-in-an-enterprise +func (s *ActionsService) RemoveEnabledOrgInEnterprise(ctx context.Context, owner string, organizationID int64) (*Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/organizations/%v", owner, organizationID) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// GetActionsAllowedInEnterprise gets the actions that are allowed in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#get-allowed-actions-and-reusable-workflows-for-an-enterprise +func (s *ActionsService) GetActionsAllowedInEnterprise(ctx context.Context, enterprise string) (*ActionsAllowed, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/selected-actions", enterprise) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + actionsAllowed := new(ActionsAllowed) + resp, err := s.client.Do(ctx, req, actionsAllowed) + if err != nil { + return nil, resp, err + } + + return actionsAllowed, resp, nil +} + +// EditActionsAllowedInEnterprise sets the actions that are allowed in an enterprise. +// +// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/permissions#set-allowed-actions-and-reusable-workflows-for-an-enterprise +func (s *ActionsService) EditActionsAllowedInEnterprise(ctx context.Context, enterprise string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/permissions/selected-actions", enterprise) + req, err := s.client.NewRequest("PUT", u, actionsAllowed) + if err != nil { + return nil, nil, err + } + + p := new(ActionsAllowed) + resp, err := s.client.Do(ctx, req, p) + if err != nil { + return nil, resp, err + } + + return p, resp, nil +} diff --git a/github/actions_permissions_enterprise_test.go b/github/actions_permissions_enterprise_test.go new file mode 100644 index 00000000000..14e8e3a6b33 --- /dev/null +++ b/github/actions_permissions_enterprise_test.go @@ -0,0 +1,298 @@ +// Copyright 2023 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" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestActionsService_GetActionsPermissionsInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "all"}`) + }) + + ctx := context.Background() + ent, _, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetActionsPermissionsInEnterprise returned error: %v", err) + } + want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("all")} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.GetActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "GetActionsPermissionsInEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetActionsPermissionsInEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_EditActionsPermissionsInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} + + mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { + v := new(ActionsPermissionsEnterprise) + 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) + } + + fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "selected"}`) + }) + + ctx := context.Background() + ent, _, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) + if err != nil { + t.Errorf("Actions.EditActionsPermissionsInEnterprise returned error: %v", err) + } + + want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.EditActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "EditActionsPermissionsInEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.EditActionsPermissionsInEnterprise(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_ListEnabledOrgsInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "page": "1", + }) + fmt.Fprint(w, `{"total_count":2,"organizations":[{"id":2}, {"id":3}]}`) + }) + + ctx := context.Background() + opt := &ListOptions{ + Page: 1, + } + got, _, err := client.Actions.ListEnabledOrgsInEnterprise(ctx, "e", opt) + if err != nil { + t.Errorf("Actions.ListEnabledOrgsInEnterprise returned error: %v", err) + } + + want := &ActionsEnabledOnEnterpriseRepos{TotalCount: int(2), Organizations: []*Organization{ + {ID: Int64(2)}, + {ID: Int64(3)}, + }} + if !cmp.Equal(got, want) { + t.Errorf("Actions.ListEnabledOrgsInEnterprise returned %+v, want %+v", got, want) + } + + const methodName = "ListEnabledOrgsInEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListEnabledOrgsInEnterprise(ctx, "\n", opt) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListEnabledOrgsInEnterprise(ctx, "e", opt) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetEnabledOrgsInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"selected_organization_ids":[123,1234]}`+"\n") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234}) + if err != nil { + t.Errorf("Actions.SetEnabledOrgsInEnterprise returned error: %v", err) + } + + const methodName = "SetEnabledOrgsInEnterprise" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetEnabledOrgsInEnterprise(ctx, "\n", []int64{123, 1234}) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234}) + }) +} + +func TestActionsService_AddEnabledOrgInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions/organizations/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.AddEnabledOrgInEnterprise(ctx, "e", 123) + if err != nil { + t.Errorf("Actions.AddEnabledOrgInEnterprise returned error: %v", err) + } + + const methodName = "AddEnabledOrgInEnterprise" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.AddEnabledOrgInEnterprise(ctx, "\n", 123) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.AddEnabledOrgInEnterprise(ctx, "e", 123) + }) +} + +func TestActionsService_RemoveEnabledOrgInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions/organizations/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.RemoveEnabledOrgInEnterprise(ctx, "e", 123) + if err != nil { + t.Errorf("Actions.RemoveEnabledOrgInEnterprise returned error: %v", err) + } + + const methodName = "RemoveEnabledOrgInEnterprise" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.RemoveEnabledOrgInEnterprise(ctx, "\n", 123) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.RemoveEnabledOrgInEnterprise(ctx, "e", 123) + }) +} + +func TestActionsService_GetActionsAllowedInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) + }) + + ctx := context.Background() + ent, _, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetActionsAllowedInEnterprise returned error: %v", err) + } + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.GetActionsAllowedInEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "GetActionsAllowedInEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetActionsAllowedInEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_EditActionsAllowedInEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + + mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { + v := new(ActionsAllowed) + 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) + } + + fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) + }) + + ctx := context.Background() + ent, _, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) + if err != nil { + t.Errorf("Actions.EditActionsAllowedInEnterprise returned error: %v", err) + } + + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.EditActionsAllowedInEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "EditActionsAllowedInEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.EditActionsAllowedInEnterprise(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/actions_permissions_orgs.go b/github/actions_permissions_orgs.go new file mode 100644 index 00000000000..801f1d97437 --- /dev/null +++ b/github/actions_permissions_orgs.go @@ -0,0 +1,204 @@ +// Copyright 2023 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" +) + +// ActionsPermissions represents a policy for repositories and allowed actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions +type ActionsPermissions struct { + EnabledRepositories *string `json:"enabled_repositories,omitempty"` + AllowedActions *string `json:"allowed_actions,omitempty"` + SelectedActionsURL *string `json:"selected_actions_url,omitempty"` +} + +func (a ActionsPermissions) String() string { + return Stringify(a) +} + +// ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled. +type ActionsEnabledOnOrgRepos struct { + TotalCount int `json:"total_count"` + Repositories []*Repository `json:"repositories"` +} + +// ActionsAllowed represents selected actions that are allowed. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions +type ActionsAllowed struct { + GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"` + VerifiedAllowed *bool `json:"verified_allowed,omitempty"` + PatternsAllowed []string `json:"patterns_allowed,omitempty"` +} + +func (a ActionsAllowed) String() string { + return Stringify(a) +} + +// GetActionsPermissions gets the GitHub Actions permissions policy for repositories and allowed actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-github-actions-permissions-for-an-organization +func (s *ActionsService) GetActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions", org) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + permissions := new(ActionsPermissions) + resp, err := s.client.Do(ctx, req, permissions) + if err != nil { + return nil, resp, err + } + + return permissions, resp, nil +} + +// EditActionsPermissions sets the permissions policy for repositories and allowed actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-github-actions-permissions-for-an-organization +func (s *ActionsService) EditActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions", org) + req, err := s.client.NewRequest("PUT", u, actionsPermissions) + if err != nil { + return nil, nil, err + } + + p := new(ActionsPermissions) + resp, err := s.client.Do(ctx, req, p) + if err != nil { + return nil, resp, err + } + + return p, resp, nil +} + +// ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-repositories-enabled-for-github-actions-in-an-organization +func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) + 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 + } + + repos := &ActionsEnabledOnOrgRepos{} + resp, err := s.client.Do(ctx, req, repos) + if err != nil { + return nil, resp, err + } + + return repos, resp, nil +} + +// SetEnabledReposInOrg replaces the list of selected repositories that are enabled for GitHub Actions in an organization.. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-selected-repositories-enabled-for-github-actions-in-an-organization +func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) + + req, err := s.client.NewRequest("PUT", u, struct { + IDs []int64 `json:"selected_repository_ids"` + }{IDs: repositoryIDs}) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// AddEnabledReposInOrg adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#enable-a-selected-repository-for-github-actions-in-an-organization +func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// RemoveEnabledRepoInOrg removes a single repository from the list of enabled repos for GitHub Actions in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#disable-a-selected-repository-for-github-actions-in-an-organization +func (s *ActionsService) RemoveEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// GetActionsAllowed gets the actions that are allowed in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-allowed-actions-and-reusable-workflows-for-an-organization +func (s *ActionsService) GetActionsAllowed(ctx context.Context, org string) (*ActionsAllowed, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + actionsAllowed := new(ActionsAllowed) + resp, err := s.client.Do(ctx, req, actionsAllowed) + if err != nil { + return nil, resp, err + } + + return actionsAllowed, resp, nil +} + +// EditActionsAllowed sets the actions that are allowed in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-allowed-actions-and-reusable-workflows-for-an-organization +func (s *ActionsService) EditActionsAllowed(ctx context.Context, org string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org) + req, err := s.client.NewRequest("PUT", u, actionsAllowed) + if err != nil { + return nil, nil, err + } + + p := new(ActionsAllowed) + resp, err := s.client.Do(ctx, req, p) + if err != nil { + return nil, resp, err + } + + return p, resp, nil +} diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go new file mode 100644 index 00000000000..36a241f6ea2 --- /dev/null +++ b/github/actions_permissions_orgs_test.go @@ -0,0 +1,336 @@ +// Copyright 2023 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" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestActionsService_GetActionsPermissions(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "all"}`) + }) + + ctx := context.Background() + org, _, err := client.Actions.GetActionsPermissions(ctx, "o") + if err != nil { + t.Errorf("Actions.GetActionsPermissions returned error: %v", err) + } + want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("all")} + if !cmp.Equal(org, want) { + t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", org, want) + } + + const methodName = "GetActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetActionsPermissions(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetActionsPermissions(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_EditActionsPermissions(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")} + + mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { + v := new(ActionsPermissions) + 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) + } + + fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "selected"}`) + }) + + ctx := context.Background() + org, _, err := client.Actions.EditActionsPermissions(ctx, "o", *input) + if err != nil { + t.Errorf("Actions.EditActionsPermissions returned error: %v", err) + } + + want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")} + if !cmp.Equal(org, want) { + t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", org, want) + } + + const methodName = "EditActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.EditActionsPermissions(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.EditActionsPermissions(ctx, "o", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_ListEnabledReposInOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "page": "1", + }) + fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":2}, {"id": 3}]}`) + }) + + ctx := context.Background() + opt := &ListOptions{ + Page: 1, + } + got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) + if err != nil { + t.Errorf("Actions.ListEnabledRepos returned error: %v", err) + } + + want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{ + {ID: Int64(2)}, + {ID: Int64(3)}, + }} + if !cmp.Equal(got, want) { + t.Errorf("Actions.ListEnabledRepos returned %+v, want %+v", got, want) + } + + const methodName = "ListEnabledRepos" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetEnabledReposInOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) + if err != nil { + t.Errorf("Actions.SetEnabledRepos returned error: %v", err) + } + + const methodName = "SetEnabledRepos" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234}) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) + }) +} + +func TestActionsService_AddEnabledReposInOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123) + if err != nil { + t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err) + } + + const methodName = "AddEnabledReposInOrg" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.AddEnabledReposInOrg(ctx, "o", 123) + }) +} + +func TestActionsService_RemoveEnabledReposInOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) + if err != nil { + t.Errorf("Actions.RemoveEnabledReposInOrg returned error: %v", err) + } + + const methodName = "RemoveEnabledReposInOrg" + + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.RemoveEnabledReposInOrg(ctx, "\n", 123) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) + }) +} + +func TestActionsService_GetActionsAllowed(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) + }) + + ctx := context.Background() + org, _, err := client.Actions.GetActionsAllowed(ctx, "o") + if err != nil { + t.Errorf("Actions.GetActionsAllowed returned error: %v", err) + } + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(org, want) { + t.Errorf("Actions.GetActionsAllowed returned %+v, want %+v", org, want) + } + + const methodName = "GetActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetActionsAllowed(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetActionsAllowed(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_EditActionsAllowed(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + + mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { + v := new(ActionsAllowed) + 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) + } + + fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) + }) + + ctx := context.Background() + org, _, err := client.Actions.EditActionsAllowed(ctx, "o", *input) + if err != nil { + t.Errorf("Actions.EditActionsAllowed returned error: %v", err) + } + + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(org, want) { + t.Errorf("Actions.EditActionsAllowed returned %+v, want %+v", org, want) + } + + const methodName = "EditActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.EditActionsAllowed(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.EditActionsAllowed(ctx, "o", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsAllowed_Marshal(t *testing.T) { + testJSONMarshal(t, &ActionsAllowed{}, "{}") + + u := &ActionsAllowed{ + GithubOwnedAllowed: Bool(false), + VerifiedAllowed: Bool(false), + PatternsAllowed: []string{"s"}, + } + + want := `{ + "github_owned_allowed": false, + "verified_allowed": false, + "patterns_allowed": [ + "s" + ] + }` + + testJSONMarshal(t, u, want) +} + +func TestActionsPermissions_Marshal(t *testing.T) { + testJSONMarshal(t, &ActionsPermissions{}, "{}") + + u := &ActionsPermissions{ + EnabledRepositories: String("e"), + AllowedActions: String("a"), + SelectedActionsURL: String("sau"), + } + + want := `{ + "enabled_repositories": "e", + "allowed_actions": "a", + "selected_actions_url": "sau" + }` + + testJSONMarshal(t, u, want) +} diff --git a/github/actions_runners.go b/github/actions_runners.go index 7427451ccfa..d54d6d41ca2 100644 --- a/github/actions_runners.go +++ b/github/actions_runners.go @@ -20,12 +20,6 @@ type RunnerApplicationDownload struct { SHA256Checksum *string `json:"sha256_checksum,omitempty"` } -// ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled. -type ActionsEnabledOnOrgRepos struct { - TotalCount int `json:"total_count"` - Repositories []*Repository `json:"repositories"` -} - // ListRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run. // // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-runner-applications-for-a-repository @@ -295,89 +289,6 @@ func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner stri return runners, resp, nil } -// ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-repositories-enabled-for-github-actions-in-an-organization -func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) - 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 - } - - repos := &ActionsEnabledOnOrgRepos{} - resp, err := s.client.Do(ctx, req, repos) - if err != nil { - return nil, resp, err - } - - return repos, resp, nil -} - -// SetEnabledReposInOrg replaces the list of selected repositories that are enabled for GitHub Actions in an organization.. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-selected-repositories-enabled-for-github-actions-in-an-organization -func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner) - - req, err := s.client.NewRequest("PUT", u, struct { - IDs []int64 `json:"selected_repository_ids"` - }{IDs: repositoryIDs}) - if err != nil { - return nil, err - } - - resp, err := s.client.Do(ctx, req, nil) - if err != nil { - return resp, err - } - - return resp, nil -} - -// AddEnabledReposInOrg adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#enable-a-selected-repository-for-github-actions-in-an-organization -func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) - - req, err := s.client.NewRequest("PUT", u, nil) - if err != nil { - return nil, err - } - - resp, err := s.client.Do(ctx, req, nil) - if err != nil { - return resp, err - } - - return resp, nil -} - -// RemoveEnabledRepoInOrg removes a single repository from the list of enabled repos for GitHub Actions in an organization. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#disable-a-selected-repository-for-github-actions-in-an-organization -func (s *ActionsService) RemoveEnabledRepoInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID) - - req, err := s.client.NewRequest("DELETE", u, nil) - if err != nil { - return nil, err - } - - resp, err := s.client.Do(ctx, req, nil) - if err != nil { - return resp, err - } - - return resp, nil -} - // GetOrganizationRunner gets a specific self-hosted runner for an organization using its runner ID. // // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#get-a-self-hosted-runner-for-an-organization diff --git a/github/actions_runners_test.go b/github/actions_runners_test.go index d059d6e82ac..581e459442d 100644 --- a/github/actions_runners_test.go +++ b/github/actions_runners_test.go @@ -446,133 +446,6 @@ func TestActionsService_ListOrganizationRunners(t *testing.T) { }) } -func TestActionsService_ListEnabledReposInOrg(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testFormValues(t, r, values{ - "page": "1", - }) - fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":2}, {"id": 3}]}`) - }) - - ctx := context.Background() - opt := &ListOptions{ - Page: 1, - } - got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) - if err != nil { - t.Errorf("Actions.ListEnabledReposInOrg returned error: %v", err) - } - - want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{ - {ID: Int64(2)}, - {ID: Int64(3)}, - }} - if !cmp.Equal(got, want) { - t.Errorf("Actions.ListEnabledReposInOrg returned %+v, want %+v", got, want) - } - - const methodName = "ListEnabledReposInOrg" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestActionsService_SetEnabledReposInOrg(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "PUT") - testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n") - w.WriteHeader(http.StatusNoContent) - }) - - ctx := context.Background() - _, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) - if err != nil { - t.Errorf("Actions.SetEnabledReposInOrg returned error: %v", err) - } - - const methodName = "SetEnabledReposInOrg" - - testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234}) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) - }) -} - -func TestActionsService_AddEnabledReposInOrg(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "PUT") - w.WriteHeader(http.StatusNoContent) - }) - - ctx := context.Background() - _, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123) - if err != nil { - t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err) - } - - const methodName = "AddEnabledReposInOrg" - - testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.AddEnabledReposInOrg(ctx, "o", 123) - }) -} - -func TestActionsService_RemoveEnabledRepoInOrg(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "DELETE") - w.WriteHeader(http.StatusNoContent) - }) - - ctx := context.Background() - _, err := client.Actions.RemoveEnabledRepoInOrg(ctx, "o", 123) - if err != nil { - t.Errorf("Actions.RemoveEnabledRepoInOrg returned error: %v", err) - } - - const methodName = "RemoveEnabledRepoInOrg" - - testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.RemoveEnabledRepoInOrg(ctx, "\n", 123) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.RemoveEnabledRepoInOrg(ctx, "o", 123) - }) -} - func TestActionsService_GetOrganizationRunner(t *testing.T) { client, mux, _, teardown := setup() defer teardown() diff --git a/github/github-accessors.go b/github/github-accessors.go index 083d6139148..03ebc9a111c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -150,6 +150,30 @@ func (a *ActionsPermissions) GetSelectedActionsURL() string { return *a.SelectedActionsURL } +// GetAllowedActions returns the AllowedActions field if it's non-nil, zero value otherwise. +func (a *ActionsPermissionsEnterprise) GetAllowedActions() string { + if a == nil || a.AllowedActions == nil { + return "" + } + return *a.AllowedActions +} + +// GetEnabledOrganizations returns the EnabledOrganizations field if it's non-nil, zero value otherwise. +func (a *ActionsPermissionsEnterprise) GetEnabledOrganizations() string { + if a == nil || a.EnabledOrganizations == nil { + return "" + } + return *a.EnabledOrganizations +} + +// GetSelectedActionsURL returns the SelectedActionsURL field if it's non-nil, zero value otherwise. +func (a *ActionsPermissionsEnterprise) GetSelectedActionsURL() string { + if a == nil || a.SelectedActionsURL == nil { + return "" + } + return *a.SelectedActionsURL +} + // GetAllowedActions returns the AllowedActions field if it's non-nil, zero value otherwise. func (a *ActionsPermissionsRepository) GetAllowedActions() string { if a == nil || a.AllowedActions == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 90d15369d0b..45f1f1cfef9 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -185,6 +185,36 @@ func TestActionsPermissions_GetSelectedActionsURL(tt *testing.T) { a.GetSelectedActionsURL() } +func TestActionsPermissionsEnterprise_GetAllowedActions(tt *testing.T) { + var zeroValue string + a := &ActionsPermissionsEnterprise{AllowedActions: &zeroValue} + a.GetAllowedActions() + a = &ActionsPermissionsEnterprise{} + a.GetAllowedActions() + a = nil + a.GetAllowedActions() +} + +func TestActionsPermissionsEnterprise_GetEnabledOrganizations(tt *testing.T) { + var zeroValue string + a := &ActionsPermissionsEnterprise{EnabledOrganizations: &zeroValue} + a.GetEnabledOrganizations() + a = &ActionsPermissionsEnterprise{} + a.GetEnabledOrganizations() + a = nil + a.GetEnabledOrganizations() +} + +func TestActionsPermissionsEnterprise_GetSelectedActionsURL(tt *testing.T) { + var zeroValue string + a := &ActionsPermissionsEnterprise{SelectedActionsURL: &zeroValue} + a.GetSelectedActionsURL() + a = &ActionsPermissionsEnterprise{} + a.GetSelectedActionsURL() + a = nil + a.GetSelectedActionsURL() +} + func TestActionsPermissionsRepository_GetAllowedActions(tt *testing.T) { var zeroValue string a := &ActionsPermissionsRepository{AllowedActions: &zeroValue} diff --git a/github/github-stringify_test.go b/github/github-stringify_test.go index c14412c2ceb..1dfd878fd59 100644 --- a/github/github-stringify_test.go +++ b/github/github-stringify_test.go @@ -39,6 +39,18 @@ func TestActionsPermissions_String(t *testing.T) { } } +func TestActionsPermissionsEnterprise_String(t *testing.T) { + v := ActionsPermissionsEnterprise{ + EnabledOrganizations: String(""), + AllowedActions: String(""), + SelectedActionsURL: String(""), + } + want := `github.ActionsPermissionsEnterprise{EnabledOrganizations:"", AllowedActions:"", SelectedActionsURL:""}` + if got := v.String(); got != want { + t.Errorf("ActionsPermissionsEnterprise.String = %v, want %v", got, want) + } +} + func TestActionsPermissionsRepository_String(t *testing.T) { v := ActionsPermissionsRepository{ Enabled: Bool(false), diff --git a/github/orgs_actions_allowed.go b/github/orgs_actions_allowed.go index e3b35b1df1f..c467c11546b 100644 --- a/github/orgs_actions_allowed.go +++ b/github/orgs_actions_allowed.go @@ -7,57 +7,22 @@ package github import ( "context" - "fmt" ) -// ActionsAllowed represents selected actions that are allowed. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions -type ActionsAllowed struct { - GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"` - VerifiedAllowed *bool `json:"verified_allowed,omitempty"` - PatternsAllowed []string `json:"patterns_allowed,omitempty"` -} - -func (a ActionsAllowed) String() string { - return Stringify(a) -} - // GetActionsAllowed gets the actions that are allowed in an organization. // // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-allowed-actions-and-reusable-workflows-for-an-organization +// Deprecated: please use `client.Actions.GetActionsAllowed` instead. func (s *OrganizationsService) GetActionsAllowed(ctx context.Context, org string) (*ActionsAllowed, *Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org) - - req, err := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - actionsAllowed := new(ActionsAllowed) - resp, err := s.client.Do(ctx, req, actionsAllowed) - if err != nil { - return nil, resp, err - } - - return actionsAllowed, resp, nil + s2 := (*ActionsService)(s) + return s2.GetActionsAllowed(ctx, org) } // EditActionsAllowed sets the actions that are allowed in an organization. // // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-allowed-actions-and-reusable-workflows-for-an-organization +// Deprecated: please use `client.Actions.EditActionsAllowed` instead. func (s *OrganizationsService) EditActionsAllowed(ctx context.Context, org string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org) - req, err := s.client.NewRequest("PUT", u, actionsAllowed) - if err != nil { - return nil, nil, err - } - - p := new(ActionsAllowed) - resp, err := s.client.Do(ctx, req, p) - if err != nil { - return nil, resp, err - } - - return p, resp, nil + s2 := (*ActionsService)(s) + return s2.EditActionsAllowed(ctx, org, actionsAllowed) } diff --git a/github/orgs_actions_allowed_test.go b/github/orgs_actions_allowed_test.go index 6461dcf9a39..de4c6d5a0bc 100644 --- a/github/orgs_actions_allowed_test.go +++ b/github/orgs_actions_allowed_test.go @@ -91,41 +91,3 @@ func TestOrganizationsService_EditActionsAllowed(t *testing.T) { return resp, err }) } - -func TestActionsAllowed_Marshal(t *testing.T) { - testJSONMarshal(t, &ActionsAllowed{}, "{}") - - u := &ActionsAllowed{ - GithubOwnedAllowed: Bool(false), - VerifiedAllowed: Bool(false), - PatternsAllowed: []string{"s"}, - } - - want := `{ - "github_owned_allowed": false, - "verified_allowed": false, - "patterns_allowed": [ - "s" - ] - }` - - testJSONMarshal(t, u, want) -} - -func TestActionsPermissions_Marshal(t *testing.T) { - testJSONMarshal(t, &ActionsPermissions{}, "{}") - - u := &ActionsPermissions{ - EnabledRepositories: String("e"), - AllowedActions: String("a"), - SelectedActionsURL: String("sau"), - } - - want := `{ - "enabled_repositories": "e", - "allowed_actions": "a", - "selected_actions_url": "sau" - }` - - testJSONMarshal(t, u, want) -} diff --git a/github/orgs_actions_permissions.go b/github/orgs_actions_permissions.go index 6d1db2ee0a3..607ffca7981 100644 --- a/github/orgs_actions_permissions.go +++ b/github/orgs_actions_permissions.go @@ -7,57 +7,22 @@ package github import ( "context" - "fmt" ) -// ActionsPermissions represents a policy for repositories and allowed actions in an organization. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions -type ActionsPermissions struct { - EnabledRepositories *string `json:"enabled_repositories,omitempty"` - AllowedActions *string `json:"allowed_actions,omitempty"` - SelectedActionsURL *string `json:"selected_actions_url,omitempty"` -} - -func (a ActionsPermissions) String() string { - return Stringify(a) -} - // GetActionsPermissions gets the GitHub Actions permissions policy for repositories and allowed actions in an organization. // // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-github-actions-permissions-for-an-organization +// Deprecated: please use `client.Actions.GetActionsPermissions` instead. func (s *OrganizationsService) GetActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions", org) - - req, err := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - permissions := new(ActionsPermissions) - resp, err := s.client.Do(ctx, req, permissions) - if err != nil { - return nil, resp, err - } - - return permissions, resp, nil + s2 := (*ActionsService)(s) + return s2.GetActionsPermissions(ctx, org) } // EditActionsPermissions sets the permissions policy for repositories and allowed actions in an organization. // // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-github-actions-permissions-for-an-organization +// Deprecated: please use `client.Actions.EditActionsPermissions` instead. func (s *OrganizationsService) EditActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) { - u := fmt.Sprintf("orgs/%v/actions/permissions", org) - req, err := s.client.NewRequest("PUT", u, actionsPermissions) - if err != nil { - return nil, nil, err - } - - p := new(ActionsPermissions) - resp, err := s.client.Do(ctx, req, p) - if err != nil { - return nil, resp, err - } - - return p, resp, nil + s2 := (*ActionsService)(s) + return s2.EditActionsPermissions(ctx, org, actionsPermissions) }