From f1c331c3bb3a90167339b700855bdb298f0d9043 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 01:15:53 -0400 Subject: [PATCH 01/22] Add enterprise allowed actions --- github/enterprise_actions_allowed.go | 50 ++++++++++++ github/enterprise_actions_allowed_test.go | 93 +++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 github/enterprise_actions_allowed.go create mode 100644 github/enterprise_actions_allowed_test.go diff --git a/github/enterprise_actions_allowed.go b/github/enterprise_actions_allowed.go new file mode 100644 index 00000000000..629691c3abe --- /dev/null +++ b/github/enterprise_actions_allowed.go @@ -0,0 +1,50 @@ +// Copyright 2021 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" +) + +// GetActionsAllowed gets the actions that are allowed in an enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-allowed-actions-and-reusable-workflows-for-an-enterprise +func (s *EnterpriseService) GetActionsAllowed(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 +} + +// EditActionsAllowed sets the actions that are allowed in an enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-allowed-actions-and-reusable-workflows-for-an-enterprise +func (s *EnterpriseService) EditActionsAllowed(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/enterprise_actions_allowed_test.go b/github/enterprise_actions_allowed_test.go new file mode 100644 index 00000000000..b60a26b2834 --- /dev/null +++ b/github/enterprise_actions_allowed_test.go @@ -0,0 +1,93 @@ +// Copyright 2021 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 TestEnterpriseService_GetActionsAllowed(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.Enterprise.GetActionsAllowed(ctx, "e") + if err != nil { + t.Errorf("Enterprise.GetActionsAllowed returned error: %v", err) + } + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Enterprise.GetActionsAllowed returned %+v, want %+v", ent, want) + } + + const methodName = "GetActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Enterprise.GetActionsAllowed(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Enterprise.GetActionsAllowed(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestEnterpriseService_EditActionsAllowed(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) + 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.Enterprise.EditActionsAllowed(ctx, "e", *input) + if err != nil { + t.Errorf("Enterprise.EditActionsAllowed returned error: %v", err) + } + + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Enterprise.EditActionsAllowed returned %+v, want %+v", ent, want) + } + + const methodName = "EditActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Enterprise.EditActionsAllowed(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Enterprise.EditActionsAllowed(ctx, "e", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From 531926349cbca58c6164f0ad942f0162c93a47c9 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 02:09:03 -0400 Subject: [PATCH 02/22] Add enterprise actions permissions --- github/enterprise_actions_permissions.go | 63 +++++++++++++ github/enterprise_actions_permissions_test.go | 94 +++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 github/enterprise_actions_permissions.go create mode 100644 github/enterprise_actions_permissions_test.go diff --git a/github/enterprise_actions_permissions.go b/github/enterprise_actions_permissions.go new file mode 100644 index 00000000000..61662fdec68 --- /dev/null +++ b/github/enterprise_actions_permissions.go @@ -0,0 +1,63 @@ +// Copyright 2021 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" +) + +// ActionsPermissionsEnterprise represents a policy for allowed actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/en/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) +} + +// GetActionsPermissions gets the GitHub Actions permissions policy for an enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-github-actions-permissions-for-an-enterprise +func (s *EnterpriseService) GetActionsPermissions(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 +} + +// EditActionsPermissions sets the permissions policy in an enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-github-actions-permissions-for-an-enterprise +func (s *EnterpriseService) EditActionsPermissions(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 +} diff --git a/github/enterprise_actions_permissions_test.go b/github/enterprise_actions_permissions_test.go new file mode 100644 index 00000000000..0abd528bc82 --- /dev/null +++ b/github/enterprise_actions_permissions_test.go @@ -0,0 +1,94 @@ +// Copyright 2021 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 TestEnterpriseService_GetActionsPermissions(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.Enterprise.GetActionsPermissions(ctx, "e") + if err != nil { + t.Errorf("Enterprise.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) + } + + const methodName = "GetActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Enterprise.GetActionsPermissions(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Enterprise.GetActionsPermissions(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestEnterpriseService_EditActionsPermissions(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) + 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.Enterprise.EditActionsPermissions(ctx, "e", *input) + if err != nil { + t.Errorf("Enterprise.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) + } + + const methodName = "EditActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Enterprise.EditActionsPermissions(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Enterprise.EditActionsPermissions(ctx, "e", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From 437c92551888e47f466fe1d08eb898a94951846e Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 03:21:49 -0400 Subject: [PATCH 03/22] Add enterprise action permissions for orgs --- github/actions_runners.go | 89 ++++++++++++++++++++++ github/actions_runners_test.go | 127 ++++++++++++++++++++++++++++++++ github/github-accessors.go | 24 ++++++ github/github-accessors_test.go | 30 ++++++++ github/github-stringify_test.go | 12 +++ 5 files changed, 282 insertions(+) diff --git a/github/actions_runners.go b/github/actions_runners.go index 7427451ccfa..ce28c570e84 100644 --- a/github/actions_runners.go +++ b/github/actions_runners.go @@ -20,6 +20,12 @@ type RunnerApplicationDownload struct { SHA256Checksum *string `json:"sha256_checksum,omitempty"` } +// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. +type ActionsEnabledOnEnterpriseOrgs struct { + TotalCount int `json:"total_count"` + Organizations []*Organization `json:"organizations"` +} + // ActionsEnabledOnOrgRepos represents all the repositories in an organization for which Actions is enabled. type ActionsEnabledOnOrgRepos struct { TotalCount int `json:"total_count"` @@ -295,6 +301,89 @@ func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner stri return runners, resp, nil } +// ListEnabledOrgsInEnterprise lists the selected organizations that are enabled for GitHub Actions in an enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-organizations-enabled-for-github-actions-in-an-enterprise +func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnEnterpriseOrgs, *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 := &ActionsEnabledOnEnterpriseOrgs{} + 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/en/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/en/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/en/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 +} + // 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 diff --git a/github/actions_runners_test.go b/github/actions_runners_test.go index d059d6e82ac..cc674cbb5b8 100644 --- a/github/actions_runners_test.go +++ b/github/actions_runners_test.go @@ -446,6 +446,133 @@ func TestActionsService_ListOrganizationRunners(t *testing.T) { }) } +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 := &ActionsEnabledOnEnterpriseOrgs{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_ListEnabledReposInOrg(t *testing.T) { client, mux, _, teardown := setup() defer teardown() diff --git a/github/github-accessors.go b/github/github-accessors.go index a685a3efce5..5405885149b 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 10ef017b3cb..307e4db5195 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 1d2a47bbffe..1f85f596ecf 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), From 3eb4312503d80b27579d2213cf161a5b63abac28 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 04:03:21 -0400 Subject: [PATCH 04/22] Move actions permissions out of action_runners.go --- github/actions_runners.go | 178 ------------ github/actions_runners_test.go | 254 ------------------ github/enterprise_actions_permissions.go | 89 ++++++ github/enterprise_actions_permissions_test.go | 127 +++++++++ github/orgs_actions_permissions.go | 89 ++++++ github/orgs_actions_permissions_test.go | 127 +++++++++ 6 files changed, 432 insertions(+), 432 deletions(-) diff --git a/github/actions_runners.go b/github/actions_runners.go index ce28c570e84..d54d6d41ca2 100644 --- a/github/actions_runners.go +++ b/github/actions_runners.go @@ -20,18 +20,6 @@ type RunnerApplicationDownload struct { SHA256Checksum *string `json:"sha256_checksum,omitempty"` } -// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. -type ActionsEnabledOnEnterpriseOrgs struct { - TotalCount int `json:"total_count"` - Organizations []*Organization `json:"organizations"` -} - -// 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 @@ -301,172 +289,6 @@ func (s *ActionsService) ListOrganizationRunners(ctx context.Context, owner stri return runners, resp, nil } -// ListEnabledOrgsInEnterprise lists the selected organizations that are enabled for GitHub Actions in an enterprise. -// -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-organizations-enabled-for-github-actions-in-an-enterprise -func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnEnterpriseOrgs, *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 := &ActionsEnabledOnEnterpriseOrgs{} - 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/en/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/en/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/en/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 -} - -// 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 cc674cbb5b8..581e459442d 100644 --- a/github/actions_runners_test.go +++ b/github/actions_runners_test.go @@ -446,260 +446,6 @@ func TestActionsService_ListOrganizationRunners(t *testing.T) { }) } -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 := &ActionsEnabledOnEnterpriseOrgs{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_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/enterprise_actions_permissions.go b/github/enterprise_actions_permissions.go index 61662fdec68..a9d54175c28 100644 --- a/github/enterprise_actions_permissions.go +++ b/github/enterprise_actions_permissions.go @@ -23,6 +23,12 @@ func (a ActionsPermissionsEnterprise) String() string { return Stringify(a) } +// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. +type ActionsEnabledOnEnterpriseOrgs struct { + TotalCount int `json:"total_count"` + Organizations []*Organization `json:"organizations"` +} + // GetActionsPermissions gets the GitHub Actions permissions policy for an enterprise. // // GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-github-actions-permissions-for-an-enterprise @@ -61,3 +67,86 @@ func (s *EnterpriseService) EditActionsPermissions(ctx context.Context, enterpri 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/en/rest/actions/permissions#list-selected-organizations-enabled-for-github-actions-in-an-enterprise +func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnEnterpriseOrgs, *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 := &ActionsEnabledOnEnterpriseOrgs{} + 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/en/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/en/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/en/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 +} diff --git a/github/enterprise_actions_permissions_test.go b/github/enterprise_actions_permissions_test.go index 0abd528bc82..0ae84602436 100644 --- a/github/enterprise_actions_permissions_test.go +++ b/github/enterprise_actions_permissions_test.go @@ -92,3 +92,130 @@ func TestEnterpriseService_EditActionsPermissions(t *testing.T) { 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 := &ActionsEnabledOnEnterpriseOrgs{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) + }) +} diff --git a/github/orgs_actions_permissions.go b/github/orgs_actions_permissions.go index 6d1db2ee0a3..e7a215f5682 100644 --- a/github/orgs_actions_permissions.go +++ b/github/orgs_actions_permissions.go @@ -23,6 +23,12 @@ 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"` +} + // 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 @@ -61,3 +67,86 @@ func (s *OrganizationsService) EditActionsPermissions(ctx context.Context, org s 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) 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 +} diff --git a/github/orgs_actions_permissions_test.go b/github/orgs_actions_permissions_test.go index d35965eccb1..03a4177ed9f 100644 --- a/github/orgs_actions_permissions_test.go +++ b/github/orgs_actions_permissions_test.go @@ -92,3 +92,130 @@ func TestOrganizationsService_EditActionsPermissions(t *testing.T) { 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.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) + }) +} From 7f8e2afad711a5379bb7fe2a5539518fad785441 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 14:01:12 -0400 Subject: [PATCH 05/22] Update copyright Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/enterprise_actions_allowed.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/enterprise_actions_allowed.go b/github/enterprise_actions_allowed.go index 629691c3abe..78764e03ef7 100644 --- a/github/enterprise_actions_allowed.go +++ b/github/enterprise_actions_allowed.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// Copyright 2022 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. From c55315aa8c8885c586121509053c7b519458ad80 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 14:01:22 -0400 Subject: [PATCH 06/22] Update copyright Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/enterprise_actions_permissions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/enterprise_actions_permissions.go b/github/enterprise_actions_permissions.go index a9d54175c28..c658ea304c2 100644 --- a/github/enterprise_actions_permissions.go +++ b/github/enterprise_actions_permissions.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// Copyright 2022 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. From e9b5264e1e198147a336b63ec35d6d9b020b7e56 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 14:01:30 -0400 Subject: [PATCH 07/22] Update copyright Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/enterprise_actions_allowed_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/enterprise_actions_allowed_test.go b/github/enterprise_actions_allowed_test.go index b60a26b2834..cf5ccd52cca 100644 --- a/github/enterprise_actions_allowed_test.go +++ b/github/enterprise_actions_allowed_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// Copyright 2022 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. From c2cd86236ee7d6e917db66b987bceb682eb8e282 Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Tue, 25 Oct 2022 14:01:38 -0400 Subject: [PATCH 08/22] Update copyright Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/enterprise_actions_permissions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/enterprise_actions_permissions_test.go b/github/enterprise_actions_permissions_test.go index 0ae84602436..8627ce81026 100644 --- a/github/enterprise_actions_permissions_test.go +++ b/github/enterprise_actions_permissions_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// Copyright 2022 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. From 33e666cafb387f7df6d7f261fa3e861b970dbabd Mon Sep 17 00:00:00 2001 From: nxya Date: Thu, 14 Sep 2023 03:05:41 -0400 Subject: [PATCH 09/22] fixed tests with enterprise action permission endpoints --- github/enterprise_actions_allowed.go | 4 +-- github/enterprise_actions_allowed_test.go | 2 +- github/enterprise_actions_permissions.go | 26 +++++++++---------- github/enterprise_actions_permissions_test.go | 2 +- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/github/enterprise_actions_allowed.go b/github/enterprise_actions_allowed.go index 78764e03ef7..17fe8b94fa3 100644 --- a/github/enterprise_actions_allowed.go +++ b/github/enterprise_actions_allowed.go @@ -12,7 +12,7 @@ import ( // GetActionsAllowed gets the actions that are allowed in an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-allowed-actions-and-reusable-workflows-for-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 *EnterpriseService) GetActionsAllowed(ctx context.Context, enterprise string) (*ActionsAllowed, *Response, error) { u := fmt.Sprintf("enterprises/%v/actions/permissions/selected-actions", enterprise) @@ -32,7 +32,7 @@ func (s *EnterpriseService) GetActionsAllowed(ctx context.Context, enterprise st // EditActionsAllowed sets the actions that are allowed in an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-allowed-actions-and-reusable-workflows-for-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 *EnterpriseService) EditActionsAllowed(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) diff --git a/github/enterprise_actions_allowed_test.go b/github/enterprise_actions_allowed_test.go index cf5ccd52cca..b4c3bd12755 100644 --- a/github/enterprise_actions_allowed_test.go +++ b/github/enterprise_actions_allowed_test.go @@ -56,7 +56,7 @@ func TestEnterpriseService_EditActionsAllowed(t *testing.T) { mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsAllowed) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/enterprise_actions_permissions.go b/github/enterprise_actions_permissions.go index c658ea304c2..4ecdafde488 100644 --- a/github/enterprise_actions_permissions.go +++ b/github/enterprise_actions_permissions.go @@ -10,28 +10,26 @@ import ( "fmt" ) +// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. +type ActionsEnabledOnEnterpriseOrgs 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/en/rest/actions/permissions +// 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) } -// ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. -type ActionsEnabledOnEnterpriseOrgs struct { - TotalCount int `json:"total_count"` - Organizations []*Organization `json:"organizations"` -} - // GetActionsPermissions gets the GitHub Actions permissions policy for an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-github-actions-permissions-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) { u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise) @@ -51,7 +49,7 @@ func (s *EnterpriseService) GetActionsPermissions(ctx context.Context, enterpris // EditActionsPermissions sets the permissions policy in an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-github-actions-permissions-for-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) { u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise) req, err := s.client.NewRequest("PUT", u, actionsPermissionsEnterprise) @@ -70,7 +68,7 @@ func (s *EnterpriseService) EditActionsPermissions(ctx context.Context, enterpri // ListEnabledOrgsInEnterprise lists the selected organizations that are enabled for GitHub Actions in an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#list-selected-organizations-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) (*ActionsEnabledOnEnterpriseOrgs, *Response, error) { u := fmt.Sprintf("enterprises/%v/actions/permissions/organizations", owner) u, err := addOptions(u, opts) @@ -94,7 +92,7 @@ func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner // SetEnabledOrgsInEnterprise replaces the list of selected organizations that are enabled for GitHub Actions in an enterprise. // -// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-selected-organizations-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) @@ -115,7 +113,7 @@ func (s *ActionsService) SetEnabledOrgsInEnterprise(ctx context.Context, owner s // 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/en/rest/actions/permissions#enable-a-selected-organization-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) @@ -134,7 +132,7 @@ func (s *ActionsService) AddEnabledOrgInEnterprise(ctx context.Context, owner st // 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/en/rest/actions/permissions#disable-a-selected-organization-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) diff --git a/github/enterprise_actions_permissions_test.go b/github/enterprise_actions_permissions_test.go index 8627ce81026..2f877c1ce26 100644 --- a/github/enterprise_actions_permissions_test.go +++ b/github/enterprise_actions_permissions_test.go @@ -57,7 +57,7 @@ func TestEnterpriseService_EditActionsPermissions(t *testing.T) { mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsPermissionsEnterprise) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { From 1da39d7bc0f34892c5b8e62aa185e1861edf32d1 Mon Sep 17 00:00:00 2001 From: nxya Date: Thu, 14 Sep 2023 03:09:55 -0400 Subject: [PATCH 10/22] formatting --- github/enterprise_actions_permissions.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/github/enterprise_actions_permissions.go b/github/enterprise_actions_permissions.go index 4ecdafde488..dce3dabb06d 100644 --- a/github/enterprise_actions_permissions.go +++ b/github/enterprise_actions_permissions.go @@ -15,6 +15,7 @@ type ActionsEnabledOnEnterpriseOrgs 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 @@ -23,6 +24,7 @@ type ActionsPermissionsEnterprise struct { AllowedActions *string `json:"allowed_actions,omitempty"` SelectedActionsURL *string `json:"selected_actions_url,omitempty"` } + func (a ActionsPermissionsEnterprise) String() string { return Stringify(a) } From 54e213a15a539e780dc48a83d671e9f295d9709a Mon Sep 17 00:00:00 2001 From: RickleAndMortimer Date: Mon, 25 Sep 2023 18:16:02 -0400 Subject: [PATCH 11/22] reorganized urls for enterprise and orgs endpoints for action permissions --- ...s.go => actions_permissions_enterprise.go} | 4 +- ...=> actions_permissions_enterprise_test.go} | 20 +-- ...issions.go => actions_permissions_orgs.go} | 56 ++++++- ...st.go => actions_permissions_orgs_test.go} | 139 ++++++++++++++++-- github/enterprise_actions_allowed.go | 50 ------- github/enterprise_actions_allowed_test.go | 93 ------------ github/orgs_actions_allowed.go | 63 -------- github/orgs_actions_allowed_test.go | 131 ----------------- 8 files changed, 193 insertions(+), 363 deletions(-) rename github/{enterprise_actions_permissions.go => actions_permissions_enterprise.go} (93%) rename github/{enterprise_actions_permissions_test.go => actions_permissions_enterprise_test.go} (89%) rename github/{orgs_actions_permissions.go => actions_permissions_orgs.go} (70%) rename github/{orgs_actions_permissions_test.go => actions_permissions_orgs_test.go} (57%) delete mode 100644 github/enterprise_actions_allowed.go delete mode 100644 github/enterprise_actions_allowed_test.go delete mode 100644 github/orgs_actions_allowed.go delete mode 100644 github/orgs_actions_allowed_test.go diff --git a/github/enterprise_actions_permissions.go b/github/actions_permissions_enterprise.go similarity index 93% rename from github/enterprise_actions_permissions.go rename to github/actions_permissions_enterprise.go index dce3dabb06d..65718573881 100644 --- a/github/enterprise_actions_permissions.go +++ b/github/actions_permissions_enterprise.go @@ -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) @@ -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 { diff --git a/github/enterprise_actions_permissions_test.go b/github/actions_permissions_enterprise_test.go similarity index 89% rename from github/enterprise_actions_permissions_test.go rename to github/actions_permissions_enterprise_test.go index 2f877c1ce26..308cbdaf3a1 100644 --- a/github/enterprise_actions_permissions_test.go +++ b/github/actions_permissions_enterprise_test.go @@ -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) } @@ -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) } diff --git a/github/orgs_actions_permissions.go b/github/actions_permissions_orgs.go similarity index 70% rename from github/orgs_actions_permissions.go rename to github/actions_permissions_orgs.go index e7a215f5682..c9535589254 100644 --- a/github/orgs_actions_permissions.go +++ b/github/actions_permissions_orgs.go @@ -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) @@ -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 { @@ -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 +} diff --git a/github/orgs_actions_permissions_test.go b/github/actions_permissions_orgs_test.go similarity index 57% rename from github/orgs_actions_permissions_test.go rename to github/actions_permissions_orgs_test.go index 03a4177ed9f..4ab27471064 100644 --- a/github/orgs_actions_permissions_test.go +++ b/github/actions_permissions_orgs_test.go @@ -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() @@ -25,23 +25,23 @@ 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) } @@ -49,7 +49,7 @@ func TestOrganizationsService_GetActionsPermissions(t *testing.T) { }) } -func TestOrganizationsService_EditActionsPermissions(t *testing.T) { +func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -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) } @@ -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) +} diff --git a/github/enterprise_actions_allowed.go b/github/enterprise_actions_allowed.go deleted file mode 100644 index 17fe8b94fa3..00000000000 --- a/github/enterprise_actions_allowed.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2022 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" -) - -// GetActionsAllowed 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 *EnterpriseService) GetActionsAllowed(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 -} - -// EditActionsAllowed 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 *EnterpriseService) EditActionsAllowed(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/enterprise_actions_allowed_test.go b/github/enterprise_actions_allowed_test.go deleted file mode 100644 index b4c3bd12755..00000000000 --- a/github/enterprise_actions_allowed_test.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2022 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 TestEnterpriseService_GetActionsAllowed(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.Enterprise.GetActionsAllowed(ctx, "e") - if err != nil { - t.Errorf("Enterprise.GetActionsAllowed returned error: %v", err) - } - want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} - if !cmp.Equal(ent, want) { - t.Errorf("Enterprise.GetActionsAllowed returned %+v, want %+v", ent, want) - } - - const methodName = "GetActionsAllowed" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Enterprise.GetActionsAllowed(ctx, "\n") - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Enterprise.GetActionsAllowed(ctx, "e") - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestEnterpriseService_EditActionsAllowed(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.Enterprise.EditActionsAllowed(ctx, "e", *input) - if err != nil { - t.Errorf("Enterprise.EditActionsAllowed returned error: %v", err) - } - - want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} - if !cmp.Equal(ent, want) { - t.Errorf("Enterprise.EditActionsAllowed returned %+v, want %+v", ent, want) - } - - const methodName = "EditActionsAllowed" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Enterprise.EditActionsAllowed(ctx, "\n", *input) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Enterprise.EditActionsAllowed(ctx, "e", *input) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} diff --git a/github/orgs_actions_allowed.go b/github/orgs_actions_allowed.go deleted file mode 100644 index e3b35b1df1f..00000000000 --- a/github/orgs_actions_allowed.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2021 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" -) - -// 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 -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 -} - -// 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 *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 -} diff --git a/github/orgs_actions_allowed_test.go b/github/orgs_actions_allowed_test.go deleted file mode 100644 index 6461dcf9a39..00000000000 --- a/github/orgs_actions_allowed_test.go +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2021 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 TestOrganizationsService_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.Organizations.GetActionsAllowed(ctx, "o") - if err != nil { - t.Errorf("Organizations.GetActionsAllowed returned error: %v", err) - } - want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} - if !cmp.Equal(org, want) { - t.Errorf("Organizations.GetActionsAllowed returned %+v, want %+v", org, want) - } - - const methodName = "GetActionsAllowed" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Organizations.GetActionsAllowed(ctx, "\n") - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Organizations.GetActionsAllowed(ctx, "o") - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestOrganizationsService_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.Organizations.EditActionsAllowed(ctx, "o", *input) - if err != nil { - t.Errorf("Organizations.EditActionsAllowed returned error: %v", err) - } - - want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} - if !cmp.Equal(org, want) { - t.Errorf("Organizations.EditActionsAllowed returned %+v, want %+v", org, want) - } - - const methodName = "EditActionsAllowed" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Organizations.EditActionsAllowed(ctx, "\n", *input) - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Organizations.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) -} From db1d21b851446915973d452f6bcfac4715345b7c Mon Sep 17 00:00:00 2001 From: RickleAndMortimer Date: Tue, 26 Sep 2023 01:11:17 -0400 Subject: [PATCH 12/22] readded allowed permissions tests and copyright --- github/actions_permissions_enterprise.go | 55 +++++++-- github/actions_permissions_enterprise_test.go | 110 +++++++++++++++--- github/actions_permissions_orgs.go | 26 ++--- github/actions_permissions_orgs_test.go | 96 +++++++-------- 4 files changed, 202 insertions(+), 85 deletions(-) diff --git a/github/actions_permissions_enterprise.go b/github/actions_permissions_enterprise.go index 65718573881..8a5f211a429 100644 --- a/github/actions_permissions_enterprise.go +++ b/github/actions_permissions_enterprise.go @@ -1,4 +1,4 @@ -// Copyright 2022 The go-github AUTHORS. All rights reserved. +// 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. @@ -11,7 +11,7 @@ import ( ) // ActionsEnabledOnEnterpriseOrgs represents all the repositories in an enterprise for which Actions is enabled. -type ActionsEnabledOnEnterpriseOrgs struct { +type ActionsEnabledOnEnterpriseRepos struct { TotalCount int `json:"total_count"` Organizations []*Organization `json:"organizations"` } @@ -29,10 +29,10 @@ func (a ActionsPermissionsEnterprise) String() string { return Stringify(a) } -// GetActionsPermissions gets the GitHub Actions permissions policy for an enterprise. +// GetActionsPermissionsForEnterprise 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) GetEnterpriseActionsPermissions(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) { +func (s *ActionsService) GetActionsPermissionsForEnterprise(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) { u := fmt.Sprintf("enterprises/%v/actions/permissions", enterprise) req, err := s.client.NewRequest("GET", u, nil) @@ -49,10 +49,10 @@ func (s *ActionsService) GetEnterpriseActionsPermissions(ctx context.Context, en return permissions, resp, nil } -// EditActionsPermissions sets the permissions policy in an enterprise. +// EditActionsPermissionsForEnterprise 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) EditEnterpriseActionsPermissions(ctx context.Context, enterprise string, actionsPermissionsEnterprise ActionsPermissionsEnterprise) (*ActionsPermissionsEnterprise, *Response, error) { +func (s *ActionsService) EditActionsPermissionsForEnterprise(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 { @@ -71,7 +71,7 @@ func (s *ActionsService) EditEnterpriseActionsPermissions(ctx context.Context, e // 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) (*ActionsEnabledOnEnterpriseOrgs, *Response, error) { +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 { @@ -83,7 +83,7 @@ func (s *ActionsService) ListEnabledOrgsInEnterprise(ctx context.Context, owner return nil, nil, err } - orgs := &ActionsEnabledOnEnterpriseOrgs{} + orgs := &ActionsEnabledOnEnterpriseRepos{} resp, err := s.client.Do(ctx, req, orgs) if err != nil { return nil, resp, err @@ -150,3 +150,42 @@ func (s *ActionsService) RemoveEnabledOrgInEnterprise(ctx context.Context, owner return resp, nil } + +// GetActionsAllowedForEnterprise 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) GetActionsAllowedForEnterprise(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 +} + +// EditActionsAllowedForEnterprise 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) EditActionsAllowedForEnterprise(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 index 308cbdaf3a1..b1971f80b60 100644 --- a/github/actions_permissions_enterprise_test.go +++ b/github/actions_permissions_enterprise_test.go @@ -1,4 +1,4 @@ -// Copyright 2022 The go-github AUTHORS. All rights reserved. +// 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. @@ -15,7 +15,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestEnterpriseService_GetActionsPermissions(t *testing.T) { +func TestActionsService_GetActionsPermissionsForEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -25,23 +25,23 @@ func TestEnterpriseService_GetActionsPermissions(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.GetEnterpriseActionsPermissions(ctx, "e") + ent, _, err := client.Actions.GetActionsPermissionsForEnterprise(ctx, "e") if err != nil { - t.Errorf("Actions.GetActionsPermissions returned error: %v", err) + t.Errorf("Actions.GetActionsPermissionsForEnterprise returned error: %v", err) } want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("all")} if !cmp.Equal(ent, want) { - t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", ent, want) + t.Errorf("Actions.GetActionsPermissionsForEnterprise returned %+v, want %+v", ent, want) } - const methodName = "GetActionsPermissions" + const methodName = "GetActionsPermissionsForEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.GetEnterpriseActionsPermissions(ctx, "\n") + _, _, err = client.Actions.GetActionsPermissionsForEnterprise(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.GetEnterpriseActionsPermissions(ctx, "e") + got, resp, err := client.Actions.GetActionsPermissionsForEnterprise(ctx, "e") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -49,7 +49,7 @@ func TestEnterpriseService_GetActionsPermissions(t *testing.T) { }) } -func TestEnterpriseService_EditActionsPermissions(t *testing.T) { +func TestActionsService_EditActionsPermissionsForEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -68,24 +68,24 @@ func TestEnterpriseService_EditActionsPermissions(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.EditEnterpriseActionsPermissions(ctx, "e", *input) + ent, _, err := client.Actions.EditActionsPermissionsForEnterprise(ctx, "e", *input) if err != nil { - t.Errorf("Actions.EditActionsPermissions returned error: %v", err) + t.Errorf("Actions.EditActionsPermissionsForEnterprise returned error: %v", err) } want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} if !cmp.Equal(ent, want) { - t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", ent, want) + t.Errorf("Actions.EditActionsPermissionsForEnterprise returned %+v, want %+v", ent, want) } - const methodName = "EditActionsPermissions" + const methodName = "EditActionsPermissionsForEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.EditEnterpriseActionsPermissions(ctx, "\n", *input) + _, _, err = client.Actions.EditActionsPermissionsForEnterprise(ctx, "\n", *input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.EditEnterpriseActionsPermissions(ctx, "e", *input) + got, resp, err := client.Actions.EditActionsPermissionsForEnterprise(ctx, "e", *input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -114,7 +114,7 @@ func TestActionsService_ListEnabledOrgsInEnterprise(t *testing.T) { t.Errorf("Actions.ListEnabledOrgsInEnterprise returned error: %v", err) } - want := &ActionsEnabledOnEnterpriseOrgs{TotalCount: int(2), Organizations: []*Organization{ + want := &ActionsEnabledOnEnterpriseRepos{TotalCount: int(2), Organizations: []*Organization{ {ID: Int64(2)}, {ID: Int64(3)}, }} @@ -219,3 +219,81 @@ func TestActionsService_RemoveEnabledOrgInEnterprise(t *testing.T) { return client.Actions.RemoveEnabledOrgInEnterprise(ctx, "e", 123) }) } + +func TestActionsService_GetActionsAllowedForEnterprise(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.GetActionsAllowedForEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetActionsAllowedForEnterprise returned error: %v", err) + } + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.GetActionsAllowedForEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "GetActionsAllowedForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetActionsAllowedForEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetActionsAllowedForEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_EditActionsAllowedForEnterprise(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.EditActionsAllowedForEnterprise(ctx, "e", *input) + if err != nil { + t.Errorf("Actions.EditActionsAllowedForEnterprise returned error: %v", err) + } + + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(ent, want) { + t.Errorf("Actions.EditActionsAllowedForEnterprise returned %+v, want %+v", ent, want) + } + + const methodName = "EditActionsAllowedForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.EditActionsAllowedForEnterprise(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.EditActionsAllowedForEnterprise(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 index c9535589254..ff00fc405f8 100644 --- a/github/actions_permissions_orgs.go +++ b/github/actions_permissions_orgs.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// 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. @@ -45,7 +45,7 @@ func (a ActionsAllowed) String() string { // 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) GetOrgsActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) { +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) @@ -65,7 +65,7 @@ func (s *ActionsService) GetOrgsActionsPermissions(ctx context.Context, org stri // 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) EditOrgsActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) { +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 { @@ -81,10 +81,10 @@ func (s *ActionsService) EditOrgsActionsPermissions(ctx context.Context, org str return p, resp, nil } -// ListEnabledReposInOrg lists the selected repositories that are enabled for GitHub Actions in an organization. +// ListEnabledRepos 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) { +func (s *ActionsService) ListEnabledRepos(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 { @@ -105,10 +105,10 @@ func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string return repos, resp, nil } -// SetEnabledReposInOrg replaces the list of selected repositories that are enabled for GitHub Actions in an organization.. +// SetEnabledRepos 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) { +func (s *ActionsService) SetEnabledRepos(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 { @@ -126,10 +126,10 @@ func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, return resp, nil } -// AddEnabledReposInOrg adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization. +// AddEnabledRepos 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) { +func (s *ActionsService) AddEnabledRepos(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) @@ -145,10 +145,10 @@ func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, return resp, nil } -// RemoveEnabledRepoInOrg removes a single repository from the list of enabled repos for GitHub Actions in an organization. +// RemoveEnabledRepo 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) { +func (s *ActionsService) RemoveEnabledRepo(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) @@ -167,7 +167,7 @@ func (s *ActionsService) RemoveEnabledRepoInOrg(ctx context.Context, owner strin // 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) { +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) @@ -187,7 +187,7 @@ func (s *ActionsService) GetActionsAllowedForOrg(ctx context.Context, org string // 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) { +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 { diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go index 4ab27471064..da4ececc48f 100644 --- a/github/actions_permissions_orgs_test.go +++ b/github/actions_permissions_orgs_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-github AUTHORS. All rights reserved. +// 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. @@ -15,7 +15,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestOrganizationsService_GetOrgsActionsPermissions(t *testing.T) { +func TestActionsService_GetActionsPermissions(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -25,7 +25,7 @@ func TestOrganizationsService_GetOrgsActionsPermissions(t *testing.T) { }) ctx := context.Background() - org, _, err := client.Actions.GetOrgsActionsPermissions(ctx, "o") + org, _, err := client.Actions.GetActionsPermissions(ctx, "o") if err != nil { t.Errorf("Actions.GetActionsPermissions returned error: %v", err) } @@ -36,12 +36,12 @@ func TestOrganizationsService_GetOrgsActionsPermissions(t *testing.T) { const methodName = "GetActionsPermissions" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.GetOrgsActionsPermissions(ctx, "\n") + _, _, err = client.Actions.GetActionsPermissions(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.GetOrgsActionsPermissions(ctx, "o") + got, resp, err := client.Actions.GetActionsPermissions(ctx, "o") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -49,7 +49,7 @@ func TestOrganizationsService_GetOrgsActionsPermissions(t *testing.T) { }) } -func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) { +func TestActionsService_EditActionsPermissions(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -68,7 +68,7 @@ func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) { }) ctx := context.Background() - org, _, err := client.Actions.EditOrgsActionsPermissions(ctx, "o", *input) + org, _, err := client.Actions.EditActionsPermissions(ctx, "o", *input) if err != nil { t.Errorf("Actions.EditActionsPermissions returned error: %v", err) } @@ -80,12 +80,12 @@ func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) { const methodName = "EditActionsPermissions" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.EditOrgsActionsPermissions(ctx, "\n", *input) + _, _, err = client.Actions.EditActionsPermissions(ctx, "\n", *input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.EditOrgsActionsPermissions(ctx, "o", *input) + got, resp, err := client.Actions.EditActionsPermissions(ctx, "o", *input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -93,7 +93,7 @@ func TestOrganizationsService_EditOrgsActionsPermissions(t *testing.T) { }) } -func TestActionsService_ListEnabledReposInOrg(t *testing.T) { +func TestActionsService_ListEnabledRepos(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -109,9 +109,9 @@ func TestActionsService_ListEnabledReposInOrg(t *testing.T) { opt := &ListOptions{ Page: 1, } - got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) + got, _, err := client.Actions.ListEnabledRepos(ctx, "o", opt) if err != nil { - t.Errorf("Actions.ListEnabledReposInOrg returned error: %v", err) + t.Errorf("Actions.ListEnabledRepos returned error: %v", err) } want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{ @@ -119,17 +119,17 @@ func TestActionsService_ListEnabledReposInOrg(t *testing.T) { {ID: Int64(3)}, }} if !cmp.Equal(got, want) { - t.Errorf("Actions.ListEnabledReposInOrg returned %+v, want %+v", got, want) + t.Errorf("Actions.ListEnabledRepos returned %+v, want %+v", got, want) } - const methodName = "ListEnabledReposInOrg" + const methodName = "ListEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt) + _, _, err = client.Actions.ListEnabledRepos(ctx, "\n", opt) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) + got, resp, err := client.Actions.ListEnabledRepos(ctx, "o", opt) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -137,7 +137,7 @@ func TestActionsService_ListEnabledReposInOrg(t *testing.T) { }) } -func TestActionsService_SetEnabledReposInOrg(t *testing.T) { +func TestActionsService_SetEnabledRepos(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -149,24 +149,24 @@ func TestActionsService_SetEnabledReposInOrg(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) + _, err := client.Actions.SetEnabledRepos(ctx, "o", []int64{123, 1234}) if err != nil { - t.Errorf("Actions.SetEnabledReposInOrg returned error: %v", err) + t.Errorf("Actions.SetEnabledRepos returned error: %v", err) } - const methodName = "SetEnabledReposInOrg" + const methodName = "SetEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234}) + _, err = client.Actions.SetEnabledRepos(ctx, "\n", []int64{123, 1234}) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) + return client.Actions.SetEnabledRepos(ctx, "o", []int64{123, 1234}) }) } -func TestActionsService_AddEnabledReposInOrg(t *testing.T) { +func TestActionsService_AddEnabledRepos(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -176,24 +176,24 @@ func TestActionsService_AddEnabledReposInOrg(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123) + _, err := client.Actions.AddEnabledRepos(ctx, "o", 123) if err != nil { - t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err) + t.Errorf("Actions.AddEnabledRepos returned error: %v", err) } - const methodName = "AddEnabledReposInOrg" + const methodName = "AddEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) + _, err = client.Actions.AddEnabledRepos(ctx, "\n", 123) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.AddEnabledReposInOrg(ctx, "o", 123) + return client.Actions.AddEnabledRepos(ctx, "o", 123) }) } -func TestActionsService_RemoveEnabledRepoInOrg(t *testing.T) { +func TestActionsService_RemoveEnabledRepo(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -203,24 +203,24 @@ func TestActionsService_RemoveEnabledRepoInOrg(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.RemoveEnabledRepoInOrg(ctx, "o", 123) + _, err := client.Actions.RemoveEnabledRepo(ctx, "o", 123) if err != nil { - t.Errorf("Actions.RemoveEnabledRepoInOrg returned error: %v", err) + t.Errorf("Actions.RemoveEnabledRepo returned error: %v", err) } - const methodName = "RemoveEnabledRepoInOrg" + const methodName = "RemoveEnabledRepo" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.RemoveEnabledRepoInOrg(ctx, "\n", 123) + _, err = client.Actions.RemoveEnabledRepo(ctx, "\n", 123) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.RemoveEnabledRepoInOrg(ctx, "o", 123) + return client.Actions.RemoveEnabledRepo(ctx, "o", 123) }) } -func TestOrganizationsService_GetActionsAllowedForOrg(t *testing.T) { +func TestActionsService_GetActionsAllowed(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -230,23 +230,23 @@ func TestOrganizationsService_GetActionsAllowedForOrg(t *testing.T) { }) ctx := context.Background() - org, _, err := client.Actions.GetActionsAllowedForOrg(ctx, "o") + org, _, err := client.Actions.GetActionsAllowed(ctx, "o") if err != nil { - t.Errorf("Actions.GetActionsAllowedForOrg returned error: %v", err) + 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.GetActionsAllowedForOrg returned %+v, want %+v", org, want) + t.Errorf("Actions.GetActionsAllowed returned %+v, want %+v", org, want) } - const methodName = "GetActionsAllowedForOrg" + const methodName = "GetActionsAllowed" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.GetActionsAllowedForOrg(ctx, "\n") + _, _, err = client.Actions.GetActionsAllowed(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.GetActionsAllowedForOrg(ctx, "o") + got, resp, err := client.Actions.GetActionsAllowed(ctx, "o") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -254,7 +254,7 @@ func TestOrganizationsService_GetActionsAllowedForOrg(t *testing.T) { }) } -func TestOrganizationsService_EditActionsAllowedForOrg(t *testing.T) { +func TestActionsService_EditActionsAllowed(t *testing.T) { client, mux, _, teardown := setup() defer teardown() input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} @@ -272,24 +272,24 @@ func TestOrganizationsService_EditActionsAllowedForOrg(t *testing.T) { }) ctx := context.Background() - org, _, err := client.Actions.EditActionsAllowedForOrg(ctx, "o", *input) + org, _, err := client.Actions.EditActionsAllowed(ctx, "o", *input) if err != nil { - t.Errorf("Actions.EditActionsAllowedForOrg returned error: %v", err) + 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.EditActionsAllowedForOrg returned %+v, want %+v", org, want) + t.Errorf("Actions.EditActionsAllowed returned %+v, want %+v", org, want) } - const methodName = "EditActionsAllowedForOrg" + const methodName = "EditActionsAllowed" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.EditActionsAllowedForOrg(ctx, "\n", *input) + _, _, err = client.Actions.EditActionsAllowed(ctx, "\n", *input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.EditActionsAllowedForOrg(ctx, "o", *input) + got, resp, err := client.Actions.EditActionsAllowed(ctx, "o", *input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } From b64b8b812159b0538122dee1a325a5efba9a4a91 Mon Sep 17 00:00:00 2001 From: RickleAndMortimer Date: Tue, 26 Sep 2023 01:13:16 -0400 Subject: [PATCH 13/22] linting --- github/actions_permissions_enterprise_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/github/actions_permissions_enterprise_test.go b/github/actions_permissions_enterprise_test.go index b1971f80b60..cd4956035df 100644 --- a/github/actions_permissions_enterprise_test.go +++ b/github/actions_permissions_enterprise_test.go @@ -296,4 +296,3 @@ func TestActionsService_EditActionsAllowedForEnterprise(t *testing.T) { return resp, err }) } - From dde3ba297283f21483668fb6b0e7c54deea5018b Mon Sep 17 00:00:00 2001 From: RickleAndMortimer Date: Tue, 26 Sep 2023 01:50:09 -0400 Subject: [PATCH 14/22] renaming to match original names prior to the PR --- github/actions_permissions_enterprise.go | 16 +++--- github/actions_permissions_enterprise_test.go | 56 +++++++++---------- github/actions_permissions_orgs.go | 8 +-- github/actions_permissions_orgs_test.go | 32 +++++------ 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/github/actions_permissions_enterprise.go b/github/actions_permissions_enterprise.go index 8a5f211a429..8311e729438 100644 --- a/github/actions_permissions_enterprise.go +++ b/github/actions_permissions_enterprise.go @@ -29,10 +29,10 @@ func (a ActionsPermissionsEnterprise) String() string { return Stringify(a) } -// GetActionsPermissionsForEnterprise gets the GitHub Actions permissions policy for an enterprise. +// 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) GetActionsPermissionsForEnterprise(ctx context.Context, enterprise string) (*ActionsPermissionsEnterprise, *Response, error) { +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) @@ -49,10 +49,10 @@ func (s *ActionsService) GetActionsPermissionsForEnterprise(ctx context.Context, return permissions, resp, nil } -// EditActionsPermissionsForEnterprise sets the permissions policy in an enterprise. +// 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) EditActionsPermissionsForEnterprise(ctx context.Context, enterprise string, actionsPermissionsEnterprise ActionsPermissionsEnterprise) (*ActionsPermissionsEnterprise, *Response, error) { +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 { @@ -151,10 +151,10 @@ func (s *ActionsService) RemoveEnabledOrgInEnterprise(ctx context.Context, owner return resp, nil } -// GetActionsAllowedForEnterprise gets the actions that are allowed in an enterprise. +// 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) GetActionsAllowedForEnterprise(ctx context.Context, enterprise string) (*ActionsAllowed, *Response, error) { +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) @@ -171,10 +171,10 @@ func (s *ActionsService) GetActionsAllowedForEnterprise(ctx context.Context, ent return actionsAllowed, resp, nil } -// EditActionsAllowedForEnterprise sets the actions that are allowed in an enterprise. +// 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) EditActionsAllowedForEnterprise(ctx context.Context, enterprise string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { +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 { diff --git a/github/actions_permissions_enterprise_test.go b/github/actions_permissions_enterprise_test.go index cd4956035df..14e8e3a6b33 100644 --- a/github/actions_permissions_enterprise_test.go +++ b/github/actions_permissions_enterprise_test.go @@ -15,7 +15,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestActionsService_GetActionsPermissionsForEnterprise(t *testing.T) { +func TestActionsService_GetActionsPermissionsInEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -25,23 +25,23 @@ func TestActionsService_GetActionsPermissionsForEnterprise(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.GetActionsPermissionsForEnterprise(ctx, "e") + ent, _, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") if err != nil { - t.Errorf("Actions.GetActionsPermissionsForEnterprise returned error: %v", err) + t.Errorf("Actions.GetActionsPermissionsInEnterprise returned error: %v", err) } want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("all")} if !cmp.Equal(ent, want) { - t.Errorf("Actions.GetActionsPermissionsForEnterprise returned %+v, want %+v", ent, want) + t.Errorf("Actions.GetActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) } - const methodName = "GetActionsPermissionsForEnterprise" + const methodName = "GetActionsPermissionsInEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.GetActionsPermissionsForEnterprise(ctx, "\n") + _, _, err = client.Actions.GetActionsPermissionsInEnterprise(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.GetActionsPermissionsForEnterprise(ctx, "e") + got, resp, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -49,7 +49,7 @@ func TestActionsService_GetActionsPermissionsForEnterprise(t *testing.T) { }) } -func TestActionsService_EditActionsPermissionsForEnterprise(t *testing.T) { +func TestActionsService_EditActionsPermissionsInEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -68,24 +68,24 @@ func TestActionsService_EditActionsPermissionsForEnterprise(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.EditActionsPermissionsForEnterprise(ctx, "e", *input) + ent, _, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) if err != nil { - t.Errorf("Actions.EditActionsPermissionsForEnterprise returned error: %v", err) + t.Errorf("Actions.EditActionsPermissionsInEnterprise returned error: %v", err) } want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} if !cmp.Equal(ent, want) { - t.Errorf("Actions.EditActionsPermissionsForEnterprise returned %+v, want %+v", ent, want) + t.Errorf("Actions.EditActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) } - const methodName = "EditActionsPermissionsForEnterprise" + const methodName = "EditActionsPermissionsInEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.EditActionsPermissionsForEnterprise(ctx, "\n", *input) + _, _, err = client.Actions.EditActionsPermissionsInEnterprise(ctx, "\n", *input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.EditActionsPermissionsForEnterprise(ctx, "e", *input) + got, resp, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -220,7 +220,7 @@ func TestActionsService_RemoveEnabledOrgInEnterprise(t *testing.T) { }) } -func TestActionsService_GetActionsAllowedForEnterprise(t *testing.T) { +func TestActionsService_GetActionsAllowedInEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -230,23 +230,23 @@ func TestActionsService_GetActionsAllowedForEnterprise(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.GetActionsAllowedForEnterprise(ctx, "e") + ent, _, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") if err != nil { - t.Errorf("Actions.GetActionsAllowedForEnterprise returned error: %v", err) + 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.GetActionsAllowedForEnterprise returned %+v, want %+v", ent, want) + t.Errorf("Actions.GetActionsAllowedInEnterprise returned %+v, want %+v", ent, want) } - const methodName = "GetActionsAllowedForEnterprise" + const methodName = "GetActionsAllowedInEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.GetActionsAllowedForEnterprise(ctx, "\n") + _, _, err = client.Actions.GetActionsAllowedInEnterprise(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.GetActionsAllowedForEnterprise(ctx, "e") + got, resp, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -254,7 +254,7 @@ func TestActionsService_GetActionsAllowedForEnterprise(t *testing.T) { }) } -func TestActionsService_EditActionsAllowedForEnterprise(t *testing.T) { +func TestActionsService_EditActionsAllowedInEnterprise(t *testing.T) { client, mux, _, teardown := setup() defer teardown() input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} @@ -272,24 +272,24 @@ func TestActionsService_EditActionsAllowedForEnterprise(t *testing.T) { }) ctx := context.Background() - ent, _, err := client.Actions.EditActionsAllowedForEnterprise(ctx, "e", *input) + ent, _, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) if err != nil { - t.Errorf("Actions.EditActionsAllowedForEnterprise returned error: %v", err) + 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.EditActionsAllowedForEnterprise returned %+v, want %+v", ent, want) + t.Errorf("Actions.EditActionsAllowedInEnterprise returned %+v, want %+v", ent, want) } - const methodName = "EditActionsAllowedForEnterprise" + const methodName = "EditActionsAllowedInEnterprise" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.EditActionsAllowedForEnterprise(ctx, "\n", *input) + _, _, err = client.Actions.EditActionsAllowedInEnterprise(ctx, "\n", *input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.EditActionsAllowedForEnterprise(ctx, "e", *input) + got, resp, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } diff --git a/github/actions_permissions_orgs.go b/github/actions_permissions_orgs.go index ff00fc405f8..e427824bbce 100644 --- a/github/actions_permissions_orgs.go +++ b/github/actions_permissions_orgs.go @@ -84,7 +84,7 @@ func (s *ActionsService) EditActionsPermissions(ctx context.Context, org string, // ListEnabledRepos 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) ListEnabledRepos(ctx context.Context, owner string, opts *ListOptions) (*ActionsEnabledOnOrgRepos, *Response, error) { +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 { @@ -108,7 +108,7 @@ func (s *ActionsService) ListEnabledRepos(ctx context.Context, owner string, opt // SetEnabledRepos 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) SetEnabledRepos(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) { +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 { @@ -129,7 +129,7 @@ func (s *ActionsService) SetEnabledRepos(ctx context.Context, owner string, repo // AddEnabledRepos 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) AddEnabledRepos(ctx context.Context, owner string, repositoryID int64) (*Response, error) { +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) @@ -148,7 +148,7 @@ func (s *ActionsService) AddEnabledRepos(ctx context.Context, owner string, repo // RemoveEnabledRepo 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) RemoveEnabledRepo(ctx context.Context, owner string, repositoryID int64) (*Response, error) { +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) diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go index da4ececc48f..ade02cc8d93 100644 --- a/github/actions_permissions_orgs_test.go +++ b/github/actions_permissions_orgs_test.go @@ -93,7 +93,7 @@ func TestActionsService_EditActionsPermissions(t *testing.T) { }) } -func TestActionsService_ListEnabledRepos(t *testing.T) { +func TestActionsService_ListEnabledReposInOrg(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -109,7 +109,7 @@ func TestActionsService_ListEnabledRepos(t *testing.T) { opt := &ListOptions{ Page: 1, } - got, _, err := client.Actions.ListEnabledRepos(ctx, "o", opt) + got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) if err != nil { t.Errorf("Actions.ListEnabledRepos returned error: %v", err) } @@ -124,12 +124,12 @@ func TestActionsService_ListEnabledRepos(t *testing.T) { const methodName = "ListEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Actions.ListEnabledRepos(ctx, "\n", opt) + _, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Actions.ListEnabledRepos(ctx, "o", opt) + got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -137,7 +137,7 @@ func TestActionsService_ListEnabledRepos(t *testing.T) { }) } -func TestActionsService_SetEnabledRepos(t *testing.T) { +func TestActionsService_SetEnabledReposInOrg(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -149,7 +149,7 @@ func TestActionsService_SetEnabledRepos(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.SetEnabledRepos(ctx, "o", []int64{123, 1234}) + _, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) if err != nil { t.Errorf("Actions.SetEnabledRepos returned error: %v", err) } @@ -157,16 +157,16 @@ func TestActionsService_SetEnabledRepos(t *testing.T) { const methodName = "SetEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.SetEnabledRepos(ctx, "\n", []int64{123, 1234}) + _, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234}) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.SetEnabledRepos(ctx, "o", []int64{123, 1234}) + return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) }) } -func TestActionsService_AddEnabledRepos(t *testing.T) { +func TestActionsService_AddEnabledReposInOrg(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -176,7 +176,7 @@ func TestActionsService_AddEnabledRepos(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.AddEnabledRepos(ctx, "o", 123) + _, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123) if err != nil { t.Errorf("Actions.AddEnabledRepos returned error: %v", err) } @@ -184,16 +184,16 @@ func TestActionsService_AddEnabledRepos(t *testing.T) { const methodName = "AddEnabledRepos" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.AddEnabledRepos(ctx, "\n", 123) + _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.AddEnabledRepos(ctx, "o", 123) + return client.Actions.AddEnabledReposInOrg(ctx, "o", 123) }) } -func TestActionsService_RemoveEnabledRepo(t *testing.T) { +func TestActionsService_RemoveEnabledReposInOrg(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -203,7 +203,7 @@ func TestActionsService_RemoveEnabledRepo(t *testing.T) { }) ctx := context.Background() - _, err := client.Actions.RemoveEnabledRepo(ctx, "o", 123) + _, err := client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) if err != nil { t.Errorf("Actions.RemoveEnabledRepo returned error: %v", err) } @@ -211,12 +211,12 @@ func TestActionsService_RemoveEnabledRepo(t *testing.T) { const methodName = "RemoveEnabledRepo" testBadOptions(t, methodName, func() (err error) { - _, err = client.Actions.RemoveEnabledRepo(ctx, "\n", 123) + _, err = client.Actions.RemoveEnabledReposInOrg(ctx, "\n", 123) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.Actions.RemoveEnabledRepo(ctx, "o", 123) + return client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) }) } From 18b5850fbd6f4d0ab14fda93194fc2ece8e3b496 Mon Sep 17 00:00:00 2001 From: nxya Date: Tue, 26 Sep 2023 19:01:25 -0400 Subject: [PATCH 15/22] readded old orgs_actions_permissions as deprecated code --- github/actions_permissions_orgs.go | 8 +++---- github/actions_permissions_orgs_test.go | 8 +++---- github/orgs_actions_allowed.go | 28 +++++++++++++++++++++++++ github/orgs_actions_permisssions.go | 28 +++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 github/orgs_actions_allowed.go create mode 100644 github/orgs_actions_permisssions.go diff --git a/github/actions_permissions_orgs.go b/github/actions_permissions_orgs.go index e427824bbce..801f1d97437 100644 --- a/github/actions_permissions_orgs.go +++ b/github/actions_permissions_orgs.go @@ -81,7 +81,7 @@ func (s *ActionsService) EditActionsPermissions(ctx context.Context, org string, return p, resp, nil } -// ListEnabledRepos lists the selected repositories that are enabled for GitHub Actions in an organization. +// 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) { @@ -105,7 +105,7 @@ func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string return repos, resp, nil } -// SetEnabledRepos replaces the list of selected repositories that are enabled for GitHub Actions in an organization.. +// 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) { @@ -126,7 +126,7 @@ func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, return resp, nil } -// AddEnabledRepos adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization. +// 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) { @@ -145,7 +145,7 @@ func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, return resp, nil } -// RemoveEnabledRepo removes a single repository from the list of enabled repos for GitHub Actions in an organization. +// 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) { diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go index ade02cc8d93..36a241f6ea2 100644 --- a/github/actions_permissions_orgs_test.go +++ b/github/actions_permissions_orgs_test.go @@ -178,10 +178,10 @@ func TestActionsService_AddEnabledReposInOrg(t *testing.T) { ctx := context.Background() _, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123) if err != nil { - t.Errorf("Actions.AddEnabledRepos returned error: %v", err) + t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err) } - const methodName = "AddEnabledRepos" + const methodName = "AddEnabledReposInOrg" testBadOptions(t, methodName, func() (err error) { _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) @@ -205,10 +205,10 @@ func TestActionsService_RemoveEnabledReposInOrg(t *testing.T) { ctx := context.Background() _, err := client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) if err != nil { - t.Errorf("Actions.RemoveEnabledRepo returned error: %v", err) + t.Errorf("Actions.RemoveEnabledReposInOrg returned error: %v", err) } - const methodName = "RemoveEnabledRepo" + const methodName = "RemoveEnabledReposInOrg" testBadOptions(t, methodName, func() (err error) { _, err = client.Actions.RemoveEnabledReposInOrg(ctx, "\n", 123) diff --git a/github/orgs_actions_allowed.go b/github/orgs_actions_allowed.go new file mode 100644 index 00000000000..e2cfd0165a4 --- /dev/null +++ b/github/orgs_actions_allowed.go @@ -0,0 +1,28 @@ +// Copyright 2021 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" +) + +// 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) { + 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) { + s2 := (*ActionsService)(s) + return s2.EditActionsAllowed(ctx, org, actionsAllowed) +} diff --git a/github/orgs_actions_permisssions.go b/github/orgs_actions_permisssions.go new file mode 100644 index 00000000000..87cfee20fc4 --- /dev/null +++ b/github/orgs_actions_permisssions.go @@ -0,0 +1,28 @@ +// Copyright 2021 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" +) + +// 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) { + 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) { + s2 := (*ActionsService)(s) + return s2.EditActionsPermissions(ctx, org, actionsPermissions) +} From 055da6710056f709ae00ea2c591cd3481604bb74 Mon Sep 17 00:00:00 2001 From: nxya Date: Tue, 26 Sep 2023 19:02:12 -0400 Subject: [PATCH 16/22] file typo --- github/orgs_actions_permissions.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 github/orgs_actions_permissions.go diff --git a/github/orgs_actions_permissions.go b/github/orgs_actions_permissions.go new file mode 100644 index 00000000000..87cfee20fc4 --- /dev/null +++ b/github/orgs_actions_permissions.go @@ -0,0 +1,28 @@ +// Copyright 2021 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" +) + +// 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) { + 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) { + s2 := (*ActionsService)(s) + return s2.EditActionsPermissions(ctx, org, actionsPermissions) +} From c9ce0206f38feed74a9b1e8465d037560ae5146d Mon Sep 17 00:00:00 2001 From: nxya Date: Tue, 26 Sep 2023 19:02:48 -0400 Subject: [PATCH 17/22] remove incorrectly named file --- github/orgs_actions_permisssions.go | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 github/orgs_actions_permisssions.go diff --git a/github/orgs_actions_permisssions.go b/github/orgs_actions_permisssions.go deleted file mode 100644 index 87cfee20fc4..00000000000 --- a/github/orgs_actions_permisssions.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2021 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" -) - -// 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) { - 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) { - s2 := (*ActionsService)(s) - return s2.EditActionsPermissions(ctx, org, actionsPermissions) -} From 324285e12122ace1fbaa36810869bd41b187e4cf Mon Sep 17 00:00:00 2001 From: RickleAndMortimer Date: Tue, 26 Sep 2023 19:45:46 -0400 Subject: [PATCH 18/22] readded org_actions tests --- github/orgs_actions_allowed_test.go | 93 ++++++++++++++++++++++++ github/orgs_actions_permissions_test.go | 94 +++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 github/orgs_actions_allowed_test.go create mode 100644 github/orgs_actions_permissions_test.go diff --git a/github/orgs_actions_allowed_test.go b/github/orgs_actions_allowed_test.go new file mode 100644 index 00000000000..de4c6d5a0bc --- /dev/null +++ b/github/orgs_actions_allowed_test.go @@ -0,0 +1,93 @@ +// Copyright 2021 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 TestOrganizationsService_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.Organizations.GetActionsAllowed(ctx, "o") + if err != nil { + t.Errorf("Organizations.GetActionsAllowed returned error: %v", err) + } + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(org, want) { + t.Errorf("Organizations.GetActionsAllowed returned %+v, want %+v", org, want) + } + + const methodName = "GetActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.GetActionsAllowed(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.GetActionsAllowed(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestOrganizationsService_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.Organizations.EditActionsAllowed(ctx, "o", *input) + if err != nil { + t.Errorf("Organizations.EditActionsAllowed returned error: %v", err) + } + + want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} + if !cmp.Equal(org, want) { + t.Errorf("Organizations.EditActionsAllowed returned %+v, want %+v", org, want) + } + + const methodName = "EditActionsAllowed" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.EditActionsAllowed(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.EditActionsAllowed(ctx, "o", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/orgs_actions_permissions_test.go b/github/orgs_actions_permissions_test.go new file mode 100644 index 00000000000..d35965eccb1 --- /dev/null +++ b/github/orgs_actions_permissions_test.go @@ -0,0 +1,94 @@ +// Copyright 2021 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 TestOrganizationsService_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.Organizations.GetActionsPermissions(ctx, "o") + if err != nil { + t.Errorf("Organizations.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) + } + + const methodName = "GetActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.GetActionsPermissions(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.GetActionsPermissions(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestOrganizationsService_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.Organizations.EditActionsPermissions(ctx, "o", *input) + if err != nil { + t.Errorf("Organizations.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) + } + + const methodName = "EditActionsPermissions" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.EditActionsPermissions(ctx, "\n", *input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.EditActionsPermissions(ctx, "o", *input) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From b2f9d6c42bfb011153ad0621f68f28632e3776f6 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:14:56 -0400 Subject: [PATCH 19/22] Update github/orgs_actions_allowed.go --- github/orgs_actions_allowed.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_actions_allowed.go b/github/orgs_actions_allowed.go index e2cfd0165a4..6758b4de193 100644 --- a/github/orgs_actions_allowed.go +++ b/github/orgs_actions_allowed.go @@ -12,7 +12,7 @@ import ( // 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 +// Deprecated: please use `client.Actions.GetActionsAllowed` instead. func (s *OrganizationsService) GetActionsAllowed(ctx context.Context, org string) (*ActionsAllowed, *Response, error) { s2 := (*ActionsService)(s) return s2.GetActionsAllowed(ctx, org) From 11a21aafffde9029c907825b5864b126005d5b6d Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:15:25 -0400 Subject: [PATCH 20/22] Update github/orgs_actions_allowed.go --- github/orgs_actions_allowed.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_actions_allowed.go b/github/orgs_actions_allowed.go index 6758b4de193..c467c11546b 100644 --- a/github/orgs_actions_allowed.go +++ b/github/orgs_actions_allowed.go @@ -21,7 +21,7 @@ func (s *OrganizationsService) GetActionsAllowed(ctx context.Context, org string // 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 +// Deprecated: please use `client.Actions.EditActionsAllowed` instead. func (s *OrganizationsService) EditActionsAllowed(ctx context.Context, org string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { s2 := (*ActionsService)(s) return s2.EditActionsAllowed(ctx, org, actionsAllowed) From ea8a4604f6110671c8d45c27273d17ba9c9a727e Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:16:16 -0400 Subject: [PATCH 21/22] Update github/orgs_actions_permissions.go --- github/orgs_actions_permissions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_actions_permissions.go b/github/orgs_actions_permissions.go index 87cfee20fc4..92d0ce40560 100644 --- a/github/orgs_actions_permissions.go +++ b/github/orgs_actions_permissions.go @@ -12,7 +12,7 @@ import ( // 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 +// Deprecated: please use `client.Actions.GetActionsPermissions` instead. func (s *OrganizationsService) GetActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) { s2 := (*ActionsService)(s) return s2.GetActionsPermissions(ctx, org) From 0d02f81f1f789d6f29ab20c4f44dcea28ff5c3ab Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:16:26 -0400 Subject: [PATCH 22/22] Update github/orgs_actions_permissions.go --- github/orgs_actions_permissions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_actions_permissions.go b/github/orgs_actions_permissions.go index 92d0ce40560..607ffca7981 100644 --- a/github/orgs_actions_permissions.go +++ b/github/orgs_actions_permissions.go @@ -21,7 +21,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 -// Deprecated: please use `client.Actions.EditActionsPermissions` instead +// Deprecated: please use `client.Actions.EditActionsPermissions` instead. func (s *OrganizationsService) EditActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) { s2 := (*ActionsService)(s) return s2.EditActionsPermissions(ctx, org, actionsPermissions)