Skip to content

Commit 0621dee

Browse files
authored
Add support for repository actions access level / permission (#2578)
Fixes: #2575.
1 parent 6c430ac commit 0621dee

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_actions_access.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 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+
// RepositoryActionsAccessLevel represents the repository actions access level.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-the-level-of-access-for-workflows-outside-of-the-repository
16+
type RepositoryActionsAccessLevel struct {
17+
// AccessLevel specifies the level of access that workflows outside of the repository have
18+
// to actions and reusable workflows within the repository.
19+
// Possible values are: "none", "organization" "enterprise".
20+
AccessLevel *string `json:"access_level,omitempty"`
21+
}
22+
23+
// GetActionsAccessLevel gets the level of access that workflows outside of the repository have
24+
// to actions and reusable workflows in the repository.
25+
//
26+
// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#get-the-level-of-access-for-workflows-outside-of-the-repository
27+
func (s *RepositoriesService) GetActionsAccessLevel(ctx context.Context, owner, repo string) (*RepositoryActionsAccessLevel, *Response, error) {
28+
u := fmt.Sprintf("repos/%v/%v/actions/permissions/access", owner, repo)
29+
req, err := s.client.NewRequest("GET", u, nil)
30+
if err != nil {
31+
return nil, nil, err
32+
}
33+
34+
raal := new(RepositoryActionsAccessLevel)
35+
resp, err := s.client.Do(ctx, req, raal)
36+
if err != nil {
37+
return nil, resp, err
38+
}
39+
40+
return raal, resp, nil
41+
}
42+
43+
// EditActionsAccessLevel sets the level of access that workflows outside of the repository have
44+
// to actions and reusable workflows in the repository.
45+
//
46+
// GitHub API docs: https://docs.github.com/en/rest/actions/permissions#set-the-level-of-access-for-workflows-outside-of-the-repository
47+
func (s *RepositoriesService) EditActionsAccessLevel(ctx context.Context, owner, repo string, repositoryActionsAccessLevel RepositoryActionsAccessLevel) (*Response, error) {
48+
u := fmt.Sprintf("repos/%v/%v/actions/permissions/access", owner, repo)
49+
req, err := s.client.NewRequest("PUT", u, repositoryActionsAccessLevel)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return s.client.Do(ctx, req, nil)
55+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2022 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+
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestRepositoriesService_GetActionsAccessLevel(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/repos/o/r/actions/permissions/access", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "GET")
24+
fmt.Fprintf(w, `{"access_level": "none"}`)
25+
})
26+
27+
ctx := context.Background()
28+
org, _, err := client.Repositories.GetActionsAccessLevel(ctx, "o", "r")
29+
if err != nil {
30+
t.Errorf("Repositories.GetActionsAccessLevel returned error: %v", err)
31+
}
32+
want := &RepositoryActionsAccessLevel{AccessLevel: String("none")}
33+
if !cmp.Equal(org, want) {
34+
t.Errorf("Repositories.GetActionsAccessLevel returned %+v, want %+v", org, want)
35+
}
36+
37+
const methodName = "GetActionsAccessLevel"
38+
testBadOptions(t, methodName, func() (err error) {
39+
_, _, err = client.Repositories.GetActionsAccessLevel(ctx, "\n", "\n")
40+
return err
41+
})
42+
43+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
44+
got, resp, err := client.Repositories.GetActionsAccessLevel(ctx, "o", "r")
45+
if got != nil {
46+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
47+
}
48+
return resp, err
49+
})
50+
}
51+
52+
func TestRepositoriesService_EditActionsAccessLevel(t *testing.T) {
53+
client, mux, _, teardown := setup()
54+
defer teardown()
55+
56+
input := &RepositoryActionsAccessLevel{AccessLevel: String("organization")}
57+
58+
mux.HandleFunc("/repos/o/r/actions/permissions/access", func(w http.ResponseWriter, r *http.Request) {
59+
v := new(RepositoryActionsAccessLevel)
60+
json.NewDecoder(r.Body).Decode(v)
61+
62+
testMethod(t, r, "PUT")
63+
if !cmp.Equal(v, input) {
64+
t.Errorf("Request body = %+v, want %+v", v, input)
65+
}
66+
})
67+
68+
ctx := context.Background()
69+
_, err := client.Repositories.EditActionsAccessLevel(ctx, "o", "r", *input)
70+
if err != nil {
71+
t.Errorf("Repositories.EditActionsAccessLevel returned error: %v", err)
72+
}
73+
74+
const methodName = "EditActionsAccessLevel"
75+
testBadOptions(t, methodName, func() (err error) {
76+
_, err = client.Repositories.EditActionsAccessLevel(ctx, "\n", "\n", *input)
77+
return err
78+
})
79+
80+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
81+
resp, err := client.Repositories.EditActionsAccessLevel(ctx, "o", "r", *input)
82+
return resp, err
83+
})
84+
}
85+
86+
func TestRepositoryActionsAccessLevel_Marshal(t *testing.T) {
87+
testJSONMarshal(t, &ActionsPermissions{}, "{}")
88+
89+
u := &RepositoryActionsAccessLevel{
90+
AccessLevel: String("enterprise"),
91+
}
92+
93+
want := `{
94+
"access_level": "enterprise"
95+
}`
96+
97+
testJSONMarshal(t, u, want)
98+
}

0 commit comments

Comments
 (0)