Skip to content

Commit 24a732e

Browse files
Add ActionsAllowed and ActionsPermissions to Organization (google#1811)
1 parent ed5a8f3 commit 24a732e

7 files changed

+398
-0
lines changed

github/github-accessors.go

+40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-stringify_test.go

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/orgs_actions_allowed.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// ActionsAllowed represents selected actions that are allowed in an organization.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-allowed-actions-for-an-organization
16+
type ActionsAllowed struct {
17+
GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"`
18+
VerifiedAllowed *bool `json:"verified_allowed,omitempty"`
19+
PatternsAllowed []string `json:"patterns_allowed,omitempty"`
20+
}
21+
22+
func (a ActionsAllowed) String() string {
23+
return Stringify(a)
24+
}
25+
26+
// GetActionsAllowed gets the actions that are allowed in an organization.
27+
//
28+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-allowed-actions-for-an-organization
29+
func (s *OrganizationsService) GetActionsAllowed(ctx context.Context, org string) (*ActionsAllowed, *Response, error) {
30+
u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org)
31+
32+
req, err := s.client.NewRequest("GET", u, nil)
33+
if err != nil {
34+
return nil, nil, err
35+
}
36+
37+
actionsAllowed := new(ActionsAllowed)
38+
resp, err := s.client.Do(ctx, req, actionsAllowed)
39+
if err != nil {
40+
return nil, resp, err
41+
}
42+
43+
return actionsAllowed, resp, nil
44+
}
45+
46+
// EditActionsAllowed sets the actions that are allowed in an organization.
47+
//
48+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-allowed-actions-for-an-organization
49+
func (s *OrganizationsService) EditActionsAllowed(ctx context.Context, org string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) {
50+
u := fmt.Sprintf("orgs/%v/actions/permissions/selected-actions", org)
51+
req, err := s.client.NewRequest("PUT", u, actionsAllowed)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
p := new(ActionsAllowed)
56+
resp, err := s.client.Do(ctx, req, p)
57+
return p, resp, err
58+
}

github/orgs_actions_allowed_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2021 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"encoding/json"
11+
"fmt"
12+
"net/http"
13+
"reflect"
14+
"testing"
15+
)
16+
17+
func TestOrganizationsService_GetActionsAllowed(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
24+
})
25+
26+
ctx := context.Background()
27+
org, _, err := client.Organizations.GetActionsAllowed(ctx, "o")
28+
if err != nil {
29+
t.Errorf("Organizations.GetActionsAllowed returned error: %v", err)
30+
}
31+
want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
32+
if !reflect.DeepEqual(org, want) {
33+
t.Errorf("Organizations.GetActionsAllowed returned %+v, want %+v", org, want)
34+
}
35+
36+
const methodName = "GetActionsAllowed"
37+
testBadOptions(t, methodName, func() (err error) {
38+
_, _, err = client.Organizations.GetActionsAllowed(ctx, "\n")
39+
return err
40+
})
41+
42+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
43+
got, resp, err := client.Organizations.GetActionsAllowed(ctx, "o")
44+
if got != nil {
45+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
46+
}
47+
return resp, err
48+
})
49+
}
50+
51+
func TestOrganizationsService_EditActionsAllowed(t *testing.T) {
52+
client, mux, _, teardown := setup()
53+
defer teardown()
54+
input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
55+
56+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
57+
v := new(ActionsAllowed)
58+
json.NewDecoder(r.Body).Decode(v)
59+
60+
testMethod(t, r, "PUT")
61+
if !reflect.DeepEqual(v, input) {
62+
t.Errorf("Request body = %+v, want %+v", v, input)
63+
}
64+
65+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
66+
})
67+
68+
ctx := context.Background()
69+
org, _, err := client.Organizations.EditActionsAllowed(ctx, "o", *input)
70+
if err != nil {
71+
t.Errorf("Organizations.EditActionsAllowed returned error: %v", err)
72+
}
73+
74+
want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
75+
if !reflect.DeepEqual(org, want) {
76+
t.Errorf("Organizations.EditActionsAllowed returned %+v, want %+v", org, want)
77+
}
78+
79+
const methodName = "EditActionsAllowed"
80+
testBadOptions(t, methodName, func() (err error) {
81+
_, _, err = client.Organizations.EditActionsAllowed(ctx, "\n", *input)
82+
return err
83+
})
84+
}

github/orgs_actions_permissions.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// ActionsPermissions represents a policy for repositories and allowed actions in an organization.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#permissions
16+
type ActionsPermissions struct {
17+
EnabledRepositories *string `json:"enabled_repositories,omitempty"`
18+
AllowedActions *string `json:"allowed_actions,omitempty"`
19+
SelectedActionsURL *string `json:"selected_actions_url,omitempty"`
20+
}
21+
22+
func (a ActionsPermissions) String() string {
23+
return Stringify(a)
24+
}
25+
26+
// GetActionsPermissions gets the GitHub Actions permissions policy for repositories and allowed actions in an organization.
27+
//
28+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-github-actions-permissions-for-an-organization
29+
func (s *OrganizationsService) GetActionsPermissions(ctx context.Context, org string) (*ActionsPermissions, *Response, error) {
30+
u := fmt.Sprintf("orgs/%v/actions/permissions", org)
31+
32+
req, err := s.client.NewRequest("GET", u, nil)
33+
if err != nil {
34+
return nil, nil, err
35+
}
36+
37+
permissions := new(ActionsPermissions)
38+
resp, err := s.client.Do(ctx, req, permissions)
39+
if err != nil {
40+
return nil, resp, err
41+
}
42+
43+
return permissions, resp, nil
44+
}
45+
46+
// EditActionsPermissions sets the permissions policy for repositories and allowed actions in an organization.
47+
//
48+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-github-actions-permissions-for-an-organization
49+
func (s *OrganizationsService) EditActionsPermissions(ctx context.Context, org string, actionsPermissions ActionsPermissions) (*ActionsPermissions, *Response, error) {
50+
u := fmt.Sprintf("orgs/%v/actions/permissions", org)
51+
req, err := s.client.NewRequest("PUT", u, actionsPermissions)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
p := new(ActionsPermissions)
56+
resp, err := s.client.Do(ctx, req, p)
57+
return p, resp, err
58+
}

0 commit comments

Comments
 (0)