Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.5k
Add support for setting actions permissions on a repository#2315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
16fa0b56ea25d06086a7fa0c4e00ab1857e32583c4913e7f40c83efa634f395ddff22bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // 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. | ||
| // The actionpermissions command utilizes go-github as a cli tool for | ||
| // changing GitHub Actions related permission settings for a repository. | ||
| package main | ||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "github.com/google/go-github/v43/github" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
| var ( | ||
| name = flag.String("name", "", "repo to change Actions permissions.") | ||
| owner = flag.String("owner", "", "owner of targeted repo.") | ||
| ) | ||
| func main() { | ||
| flag.Parse() | ||
| token := os.Getenv("GITHUB_AUTH_TOKEN") | ||
| if token == "" { | ||
| log.Fatal("Unauthorized: No token present") | ||
| } | ||
| if *name == "" { | ||
| log.Fatal("No name: repo name must be given") | ||
| } | ||
| if *owner == "" { | ||
| log.Fatal("No owner: owner of repo must be given") | ||
| } | ||
| ctx := context.Background() | ||
| ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) | ||
| tc := oauth2.NewClient(ctx, ts) | ||
| client := github.NewClient(tc) | ||
| actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String()) | ||
| actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("selected")} | ||
| _, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String()) | ||
| actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, *owner, *name) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String()) | ||
| actionsAllowed = &github.ActionsAllowed{GithubOwnedAllowed: github.Bool(true), VerifiedAllowed: github.Bool(false), PatternsAllowed: []string{"a/b"}} | ||
| _, _, err = client.Repositories.EditActionsAllowed(ctx, *owner, *name, *actionsAllowed) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String()) | ||
| actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("all")} | ||
| _, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String()) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // 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 a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-allowed-actions-and-workflows-for-a-repository | ||
| func (s *RepositoriesService) GetActionsAllowed(ctx context.Context, org, repo string) (*ActionsAllowed, *Response, error) { | ||
| u := fmt.Sprintf("repos/%v/%v/actions/permissions/selected-actions", org, repo) | ||
| 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 a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-allowed-actions-and-workflows-for-a-repository | ||
| func (s *RepositoriesService) EditActionsAllowed(ctx context.Context, org, repo string, actionsAllowed ActionsAllowed) (*ActionsAllowed, *Response, error) { | ||
| u := fmt.Sprintf("repos/%v/%v/actions/permissions/selected-actions", org, repo) | ||
| req, err := s.client.NewRequest("PUT", u, actionsAllowed) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| p := new(ActionsAllowed) | ||
| resp, err := s.client.Do(ctx, req, p) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
| return p, resp, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // 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 TestRepositoryService_GetActionsAllowed(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
| mux.HandleFunc("/repos/o/r/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.Repositories.GetActionsAllowed(ctx, "o", "r") | ||
| if err != nil { | ||
| t.Errorf("Repositories.GetActionsAllowed returned error: %v", err) | ||
| } | ||
| want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} | ||
| if !cmp.Equal(org, want) { | ||
| t.Errorf("Repositories.GetActionsAllowed returned %+v, want %+v", org, want) | ||
| } | ||
| const methodName = "GetActionsAllowed" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Repositories.GetActionsAllowed(ctx, "\n", "\n") | ||
| return err | ||
| }) | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.Repositories.GetActionsAllowed(ctx, "o", "r") | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } | ||
| func TestRepositoriesService_EditActionsAllowed(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
| input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} | ||
| mux.HandleFunc("/repos/o/r/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() | ||
| org, _, err := client.Repositories.EditActionsAllowed(ctx, "o", "r", *input) | ||
| if err != nil { | ||
| t.Errorf("Repositories.EditActionsAllowed returned error: %v", err) | ||
| } | ||
| want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} | ||
| if !cmp.Equal(org, want) { | ||
| t.Errorf("Repositories.EditActionsAllowed returned %+v, want %+v", org, want) | ||
| } | ||
| const methodName = "EditActionsAllowed" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.Repositories.EditActionsAllowed(ctx, "\n", "\n", *input) | ||
| return err | ||
| }) | ||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.Repositories.EditActionsAllowed(ctx, "o", "r", *input) | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } | ||
gmlewis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // 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" | ||
| ) | ||
| // ActionsPermissionsRepository represents a policy for repositories and allowed actions in a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-github-actions-permissions-for-a-repository--parameters | ||
| type ActionsPermissionsRepository struct { | ||
| Enabled *bool `json:"enabled,omitempty"` | ||
| AllowedActions *string `json:"allowed_actions,omitempty"` | ||
| SelectedActionsURL *string `json:"selected_actions_url,omitempty"` | ||
| } | ||
| func (a ActionsPermissionsRepository) String() string { | ||
| return Stringify(a) | ||
| } | ||
| // GetActionsPermissions gets the GitHub Actions permissions policy for repositories and allowed actions in a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-github-actions-permissions-for-a-repository | ||
| func (s *RepositoriesService) GetActionsPermissions(ctx context.Context, owner, repo string) (*ActionsPermissionsRepository, *Response, error) { | ||
| u := fmt.Sprintf("repos/%v/%v/actions/permissions", owner, repo) | ||
| req, err := s.client.NewRequest("GET", u, nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| permissions := new(ActionsPermissionsRepository) | ||
| resp, err := s.client.Do(ctx, req, permissions) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
| return permissions, resp, nil | ||
| } | ||
| // EditActionsPermissions sets the permissions policy for repositories and allowed actions in a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-github-actions-permissions-for-a-repository | ||
| func (s *RepositoriesService) EditActionsPermissions(ctx context.Context, owner, repo string, actionsPermissionsRepository ActionsPermissionsRepository) (*ActionsPermissionsRepository, *Response, error) { | ||
| u := fmt.Sprintf("repos/%v/%v/actions/permissions", owner, repo) | ||
| req, err := s.client.NewRequest("PUT", u, actionsPermissionsRepository) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| permissions := new(ActionsPermissionsRepository) | ||
gmlewis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| resp, err := s.client.Do(ctx, req, permissions) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
| return permissions, resp, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.