Skip to content

Commit

Permalink
reorganized urls for enterprise and orgs endpoints for action permiss…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
RickleAndMortimer committed Sep 25, 2023
1 parent 1da39d7 commit 54e213a
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (a ActionsPermissionsEnterprise) String() string {
// GetActionsPermissions 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 *EnterpriseService) GetActionsPermissions(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) {
func (s *ActionsService) GetEnterpriseActionsPermissions(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) {
u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise)

req, err := s.client.NewRequest("GET", u, nil)
Expand All @@ -52,7 +52,7 @@ func (s *EnterpriseService) GetActionsPermissions(ctx context.Context, enterpris
// EditActionsPermissions 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 *EnterpriseService) EditActionsPermissions(ctx context.Context, enterprise string, actionsPermissionsEnterprise ActionsPermissionsEnterprise) (*ActionsPermissionsEnterprise, *Response, error) {
func (s *ActionsService) EditEnterpriseActionsPermissions(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ func TestEnterpriseService_GetActionsPermissions(t *testing.T) {
})

ctx := context.Background()
ent, _, err := client.Enterprise.GetActionsPermissions(ctx, "e")
ent, _, err := client.Actions.GetEnterpriseActionsPermissions(ctx, "e")
if err != nil {
t.Errorf("Enterprise.GetActionsPermissions returned error: %v", err)
t.Errorf("Actions.GetActionsPermissions returned error: %v", err)
}
want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("all")}
if !cmp.Equal(ent, want) {
t.Errorf("Enterprise.GetActionsPermissions returned %+v, want %+v", ent, want)
t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", ent, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Enterprise.GetActionsPermissions(ctx, "e")
got, resp, err := client.Actions.GetEnterpriseActionsPermissions(ctx, "e")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand All @@ -68,24 +68,24 @@ func TestEnterpriseService_EditActionsPermissions(t *testing.T) {
})

ctx := context.Background()
ent, _, err := client.Enterprise.EditActionsPermissions(ctx, "e", *input)
ent, _, err := client.Actions.EditEnterpriseActionsPermissions(ctx, "e", *input)
if err != nil {
t.Errorf("Enterprise.EditActionsPermissions returned error: %v", err)
t.Errorf("Actions.EditActionsPermissions returned error: %v", err)
}

want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")}
if !cmp.Equal(ent, want) {
t.Errorf("Enterprise.EditActionsPermissions returned %+v, want %+v", ent, want)
t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", ent, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Enterprise.EditActionsPermissions(ctx, "e", *input)
got, resp, err := client.Actions.EditEnterpriseActionsPermissions(ctx, "e", *input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ type ActionsEnabledOnOrgRepos struct {
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 *OrganizationsService) GetActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) {
func (s *ActionsService) GetOrgsActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) {
u := fmt.Sprintf("orgs/%v/actions/permissions", org)

req, err := s.client.NewRequest("GET", u, nil)
Expand All @@ -52,7 +65,7 @@ func (s *OrganizationsService) GetActionsPermissions(ctx context.Context, org st
// 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 *OrganizationsService) EditActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) {
func (s *ActionsService) EditOrgsActionsPermissions(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 {
Expand Down Expand Up @@ -150,3 +163,42 @@ func (s *ActionsService) RemoveEnabledRepoInOrg(ctx context.Context, owner strin

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) GetActionsAllowedForOrg(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) EditActionsAllowedForOrg(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
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestOrganizationsService_GetActionsPermissions(t *testing.T) {
func TestOrganizationsService_GetOrgsActionsPermissions(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand All @@ -25,31 +25,31 @@ func TestOrganizationsService_GetActionsPermissions(t *testing.T) {
})

ctx := context.Background()
org, _, err := client.Organizations.GetActionsPermissions(ctx, "o")
org, _, err := client.Actions.GetOrgsActionsPermissions(ctx, "o")
if err != nil {
t.Errorf("Organizations.GetActionsPermissions returned error: %v", err)
t.Errorf("Actions.GetActionsPermissions returned error: %v", err)
}
want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("all")}
if !cmp.Equal(org, want) {
t.Errorf("Organizations.GetActionsPermissions returned %+v, want %+v", org, want)
t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", org, want)
}

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

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

func TestOrganizationsService_EditActionsPermissions(t *testing.T) {
func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand All @@ -68,24 +68,24 @@ func TestOrganizationsService_EditActionsPermissions(t *testing.T) {
})

ctx := context.Background()
org, _, err := client.Organizations.EditActionsPermissions(ctx, "o", *input)
org, _, err := client.Actions.EditOrgsActionsPermissions(ctx, "o", *input)
if err != nil {
t.Errorf("Organizations.EditActionsPermissions returned error: %v", err)
t.Errorf("Actions.EditActionsPermissions returned error: %v", err)
}

want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")}
if !cmp.Equal(org, want) {
t.Errorf("Organizations.EditActionsPermissions returned %+v, want %+v", org, want)
t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", org, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.EditActionsPermissions(ctx, "o", *input)
got, resp, err := client.Actions.EditOrgsActionsPermissions(ctx, "o", *input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down Expand Up @@ -219,3 +219,118 @@ func TestActionsService_RemoveEnabledRepoInOrg(t *testing.T) {
return client.Actions.RemoveEnabledRepoInOrg(ctx, "o", 123)
})
}

func TestOrganizationsService_GetActionsAllowedForOrg(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.GetActionsAllowedForOrg(ctx, "o")
if err != nil {
t.Errorf("Actions.GetActionsAllowedForOrg returned error: %v", err)
}
want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
if !cmp.Equal(org, want) {
t.Errorf("Actions.GetActionsAllowedForOrg returned %+v, want %+v", org, want)
}

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

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

func TestOrganizationsService_EditActionsAllowedForOrg(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.EditActionsAllowedForOrg(ctx, "o", *input)
if err != nil {
t.Errorf("Actions.EditActionsAllowedForOrg returned error: %v", err)
}

want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
if !cmp.Equal(org, want) {
t.Errorf("Actions.EditActionsAllowedForOrg returned %+v, want %+v", org, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.EditActionsAllowedForOrg(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)
}
Loading

0 comments on commit 54e213a

Please sign in to comment.