From 423f58e81ba002dda2fa586de4d9ef94a115b4de Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 19:47:05 -0400 Subject: [PATCH 01/10] feat(orgs-actions-secrets): Added GetPublicKey support. --- github/orgs_actions_secrets.go | 27 +++++++++++++++++++++++++++ github/orgs_actions_secrets_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 github/orgs_actions_secrets.go create mode 100644 github/orgs_actions_secrets_test.go diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go new file mode 100644 index 00000000000..42850743269 --- /dev/null +++ b/github/orgs_actions_secrets.go @@ -0,0 +1,27 @@ +package github + +import ( + "context" + "fmt" +) + +type OrgsActionsPublicKey struct { + KeyID *string `json:"key_id"` + Key *string `json:"key"` +} + +func (s *OrganizationsService) GetPublicKey(ctx context.Context, owner string) (*OrgsActionsPublicKey, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/public-key", owner) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + pubKey := new(OrgsActionsPublicKey) + resp, err := s.client.Do(ctx, req, pubKey) + if err != nil { + return nil, resp, err + } + + return pubKey, resp, nil +} \ No newline at end of file diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go new file mode 100644 index 00000000000..43fa7b00ba0 --- /dev/null +++ b/github/orgs_actions_secrets_test.go @@ -0,0 +1,29 @@ +package github + +import ( + "context" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestOrganizationsService_GetPublicKey(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/public-key", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"key_id":"012345678912345678","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) + }) + + key, _, err := client.Organizations.GetPublicKey(context.Background(), "o") + if err != nil { + t.Errorf("OrgsActions.GetPublicKey returned error: %v", err) + } + + want := &OrgsActionsPublicKey{KeyID: String("012345678912345678"), Key: String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234")} + if !reflect.DeepEqual(key, want) { + t.Errorf("OrgsActions.GetPublicKey returned %+v, want %+v", key, want) + } +} \ No newline at end of file From d1560620cd4179d51b8d0f315fdb220fed5f8831 Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 20:32:23 -0400 Subject: [PATCH 02/10] feat(orgs-actions-secrets): Added ListSecrets support. --- github/github-accessors.go | 16 +++++++++ github/orgs_actions_secrets.go | 50 ++++++++++++++++++++++++++--- github/orgs_actions_secrets_test.go | 38 +++++++++++++++++++--- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 884f1acc6de..96e582b1897 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6956,6 +6956,22 @@ func (o *OrganizationInstallations) GetTotalCount() int { return *o.TotalCount } +// GetKey returns the Key field if it's non-nil, zero value otherwise. +func (o *OrganizationPublicKey) GetKey() string { + if o == nil || o.Key == nil { + return "" + } + return *o.Key +} + +// GetKeyID returns the KeyID field if it's non-nil, zero value otherwise. +func (o *OrganizationPublicKey) GetKeyID() string { + if o == nil || o.KeyID == nil { + return "" + } + return *o.KeyID +} + // GetAction returns the Action field if it's non-nil, zero value otherwise. func (o *OrgBlockEvent) GetAction() string { if o == nil || o.Action == nil { diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index 42850743269..b58fe723022 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -5,23 +5,65 @@ import ( "fmt" ) -type OrgsActionsPublicKey struct { +// PublicKey represents the public key that should be used to encrypt secrets. +type OrganizationPublicKey struct { KeyID *string `json:"key_id"` Key *string `json:"key"` } -func (s *OrganizationsService) GetPublicKey(ctx context.Context, owner string) (*OrgsActionsPublicKey, *Response, error) { +// GetPublicKey gets a public key that should be used for secret encryption. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#get-an-organization-public-key +func (s *OrganizationsService) GetPublicKey(ctx context.Context, owner string) (*OrganizationPublicKey, *Response, error) { u := fmt.Sprintf("orgs/%v/actions/secrets/public-key", owner) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err } - pubKey := new(OrgsActionsPublicKey) + pubKey := new(OrganizationPublicKey) resp, err := s.client.Do(ctx, req, pubKey) if err != nil { return nil, resp, err } return pubKey, resp, nil -} \ No newline at end of file +} + +type OrganizationSecret struct { + Name string `json:"name"` + CreatedAt Timestamp `json:"created_at"` + UpdatedAt Timestamp `json:"updated_at"` + Visibility string `json:"visibility"` + SelectedRepositoriesUrl string `json:"selected_repositories_url"` +} + +type OrganizationSecrets struct { + TotalCount int `json:"total_count"` + Secrets []*OrganizationSecret `json:"secrets"` +} + +// ListSecrets lists all secrets available in an Organization +// without revealing their encrypted values. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#list-organization-secrets +func (s *OrganizationsService) ListSecrets(ctx context.Context, owner string, opts *ListOptions) (*OrganizationSecrets, *Response, error) { + u := fmt.Sprintf("orgs/%s/actions/secrets", 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 + } + + secrets := new(OrganizationSecrets) + resp, err := s.client.Do(ctx, req, &secrets) + if err != nil { + return nil, resp, err + } + + return secrets, resp, nil +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 43fa7b00ba0..3db0b927051 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -6,6 +6,7 @@ import ( "net/http" "reflect" "testing" + "time" ) func TestOrganizationsService_GetPublicKey(t *testing.T) { @@ -19,11 +20,40 @@ func TestOrganizationsService_GetPublicKey(t *testing.T) { key, _, err := client.Organizations.GetPublicKey(context.Background(), "o") if err != nil { - t.Errorf("OrgsActions.GetPublicKey returned error: %v", err) + t.Errorf("Organizations.GetPublicKey returned error: %v", err) } - want := &OrgsActionsPublicKey{KeyID: String("012345678912345678"), Key: String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234")} + want := &OrganizationPublicKey{KeyID: String("012345678912345678"), Key: String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234")} if !reflect.DeepEqual(key, want) { - t.Errorf("OrgsActions.GetPublicKey returned %+v, want %+v", key, want) + t.Errorf("Organizations.GetPublicKey returned %+v, want %+v", key, want) } -} \ No newline at end of file +} + +func TestOrganizationsService_ListSecrets(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"per_page": "2", "page": "2"}) + fmt.Fprint(w, `{"total_count":3,"secrets":[{"name":"GIST_ID","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"private"},{"name":"DEPLOY_TOKEN","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"all"},{"name":"GH_TOKEN","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories"}]}`) + }) + + opts := &ListOptions{Page: 2, PerPage: 2} + secrets, _, err := client.Organizations.ListSecrets(context.Background(), "o", opts) + if err != nil { + t.Errorf("Organizations.ListSecrets returned error: %v", err) + } + + want := &OrganizationSecrets{ + TotalCount: 3, + Secrets: []*OrganizationSecret{ + {Name: "GIST_ID", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "private"}, + {Name: "DEPLOY_TOKEN", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "all"}, + {Name: "GH_TOKEN", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "selected", SelectedRepositoriesUrl: "https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories"}, + }, + } + if !reflect.DeepEqual(secrets, want) { + t.Errorf("Organizations.ListSecrets returned %+v, want %+v", secrets, want) + } +} From 140741b295d9b11b5d8a6fd18840e62b5bce2062 Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 20:49:46 -0400 Subject: [PATCH 03/10] feat(orgs-actions-secrets): Added GetSecret support. --- github/orgs_actions_secrets.go | 19 +++++++++++++++++++ github/orgs_actions_secrets_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index b58fe723022..9bc979b7f59 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -67,3 +67,22 @@ func (s *OrganizationsService) ListSecrets(ctx context.Context, owner string, op return secrets, resp, nil } + +// GetSecret gets a single secret without revealing its encrypted value. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#get-an-organization-secret +func (s *OrganizationsService) GetSecret(ctx context.Context, owner, name string) (*OrganizationSecret, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v", owner, name) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + secret := new(OrganizationSecret) + resp, err := s.client.Do(ctx, req, secret) + if err != nil { + return nil, resp, err + } + + return secret, resp, nil +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 3db0b927051..719055f5d47 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -57,3 +57,30 @@ func TestOrganizationsService_ListSecrets(t *testing.T) { t.Errorf("Organizations.ListSecrets returned %+v, want %+v", secrets, want) } } + +func TestOrganizationsService_GetSecret(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/GH_TOKEN", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"name":"GH_TOKEN","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories"}`) + }) + + secret, _, err := client.Organizations.GetSecret(context.Background(), "o", "GH_TOKEN") + if err != nil { + t.Errorf("Organizations.GetSecret returned error: %v", err) + } + + want := &OrganizationSecret{ + Name: "GH_TOKEN", + CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, + UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, + Visibility: "selected", + SelectedRepositoriesUrl: "https://api.github.com/orgs/octo-org/actions/secrets/SUPER_SECRET/repositories", + } + + if !reflect.DeepEqual(secret, want) { + t.Errorf("Organizations.GetSecret returned %+v, want %+v", secret, want) + } +} From 164140076937856b68408d9d41d59c2dca5a0e77 Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 21:23:37 -0400 Subject: [PATCH 04/10] feat(orgs-actions-secrets): Added CreateOrUpdateSecret support. --- github/orgs_actions_secrets.go | 28 ++++++++++++++++++++++++++++ github/orgs_actions_secrets_test.go | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index 9bc979b7f59..f85177dec30 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -86,3 +86,31 @@ func (s *OrganizationsService) GetSecret(ctx context.Context, owner, name string return secret, resp, nil } + +type OrganizationEncryptedSecret struct { + Name string `json:"-"` + KeyID string `json:"key_id"` + EncryptedValue string `json:"encrypted_value"` + Visibility string `json:"visibility"` + SelectedRepositoryIDs []string `json:"selected_repository_ids,omitempty"` +} + +// OrganizationEncryptedSecret represents an Organization secret that is encrypted using a public key. +// +// The value of EncryptedValue must be your secret, encrypted with +// LibSodium (see documentation here: https://libsodium.gitbook.io/doc/bindings_for_other_languages) +// using the public key retrieved using the GetPublicKey method. + +// CreateOrUpdateSecret creates or updates a secret with an encrypted value. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#create-or-update-an-organization-secret +func (s *OrganizationsService) CreateOrUpdateSecret(ctx context.Context, owner string, eSecret *OrganizationEncryptedSecret) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v", owner, eSecret.Name) + + req, err := s.client.NewRequest("PUT", u, eSecret) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 719055f5d47..b2732506d39 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -84,3 +84,26 @@ func TestOrganizationsService_GetSecret(t *testing.T) { t.Errorf("Organizations.GetSecret returned %+v, want %+v", secret, want) } } + +func TestOrgnizationsService_CreateOrUpdateSecret(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["A","B","C"]}`+"\n") + }) + + input := &OrganizationEncryptedSecret{ + Name: "NAME", + EncryptedValue: "QIv=", + KeyID: "1234", + Visibility: "selected", + SelectedRepositoryIDs: []string{"A", "B", "C"}, + } + _, err := client.Organizations.CreateOrUpdateSecret(context.Background(), "o", input) + if err != nil { + t.Errorf("Organizations.CreateOrUpdateSecret returned error: %v", err) + } +} From db1a44546203ccbe5ba58f3c685ca5e7503354af Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 21:28:04 -0400 Subject: [PATCH 05/10] feat(orgs-actions-secrets): Added DeleteSecret support. --- github/orgs_actions_secrets.go | 14 ++++++++++++++ github/orgs_actions_secrets_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index f85177dec30..c7ab18ca8ee 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -114,3 +114,17 @@ func (s *OrganizationsService) CreateOrUpdateSecret(ctx context.Context, owner s return s.client.Do(ctx, req, nil) } + +// DeleteSecret deletes a secret in a repository using the secret name. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#delete-an-organization-secret +func (s *OrganizationsService) DeleteSecret(ctx context.Context, owner, name string) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v", owner, name) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index b2732506d39..698d647d09a 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -107,3 +107,17 @@ func TestOrgnizationsService_CreateOrUpdateSecret(t *testing.T) { t.Errorf("Organizations.CreateOrUpdateSecret returned error: %v", err) } } + +func TestOrgnizationsService_DeleteSecret(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Organizations.DeleteSecret(context.Background(), "o", "NAME") + if err != nil { + t.Errorf("Organizations.DeleteSecret returned error: %v", err) + } +} From 1d45b2bd1e4dba9f4446f7e7c3371dfe0349557f Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 21:59:58 -0400 Subject: [PATCH 06/10] feat(orgs-actions-secrets): Added ListSecretSelectedRepositories support. --- github/github-accessors.go | 8 +++++++ github/orgs_actions_secrets.go | 36 ++++++++++++++++++++++++----- github/orgs_actions_secrets_test.go | 24 +++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 96e582b1897..badca1b6790 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6972,6 +6972,14 @@ func (o *OrganizationPublicKey) GetKeyID() string { return *o.KeyID } +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (o *OrganizationSecretSelectedRepositories) GetTotalCount() int64 { + if o == nil || o.TotalCount == nil { + return 0 + } + return *o.TotalCount +} + // GetAction returns the Action field if it's non-nil, zero value otherwise. func (o *OrgBlockEvent) GetAction() string { if o == nil || o.Action == nil { diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index c7ab18ca8ee..e157aef0fff 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -87,6 +87,11 @@ func (s *OrganizationsService) GetSecret(ctx context.Context, owner, name string return secret, resp, nil } +// OrganizationEncryptedSecret represents an Organization secret that is encrypted using a public key. +// +// The value of EncryptedValue must be your secret, encrypted with +// LibSodium (see documentation here: https://libsodium.gitbook.io/doc/bindings_for_other_languages) +// using the public key retrieved using the GetPublicKey method. type OrganizationEncryptedSecret struct { Name string `json:"-"` KeyID string `json:"key_id"` @@ -95,12 +100,6 @@ type OrganizationEncryptedSecret struct { SelectedRepositoryIDs []string `json:"selected_repository_ids,omitempty"` } -// OrganizationEncryptedSecret represents an Organization secret that is encrypted using a public key. -// -// The value of EncryptedValue must be your secret, encrypted with -// LibSodium (see documentation here: https://libsodium.gitbook.io/doc/bindings_for_other_languages) -// using the public key retrieved using the GetPublicKey method. - // CreateOrUpdateSecret creates or updates a secret with an encrypted value. // // GitHub API docs: https://developer.github.com/v3/actions/secrets/#create-or-update-an-organization-secret @@ -128,3 +127,28 @@ func (s *OrganizationsService) DeleteSecret(ctx context.Context, owner, name str return s.client.Do(ctx, req, nil) } + +// OrganizationSecretSelectedRepositories represents all repositories that have been selected to have access to an OrganizationSecret +type OrganizationSecretSelectedRepositories struct { + TotalCount *int64 `json:"total_count,omitempty"` + Repositories []*Repository `json:"repositories,omitempty"` +} + +// Lists all repositories that have been selected when the visibility for repository access to a secret is set to selected. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#list-selected-repositories-for-an-organization-secret +func (s *OrganizationsService) ListSecretSelectedRepositories(ctx context.Context, owner, name string) (*OrganizationSecretSelectedRepositories, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories", owner, name) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + secretSelectedRepositories := new(OrganizationSecretSelectedRepositories) + resp, err := s.client.Do(ctx, req, secretSelectedRepositories) + if err != nil { + return nil, resp, err + } + + return secretSelectedRepositories, resp, nil +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 698d647d09a..370581e29a5 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -121,3 +121,27 @@ func TestOrgnizationsService_DeleteSecret(t *testing.T) { t.Errorf("Organizations.DeleteSecret returned error: %v", err) } } + +func TestOrganizationsService_ListSecretSelectedRepositories(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/SECRET_NAME/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":1},{"id":2}]}`) + }) + + secretSelectedRepositories, _, err := client.Organizations.ListSecretSelectedRepositories(context.Background(), "o", "SECRET_NAME") + if err != nil { + t.Errorf("Organizations.ListSecretSelectedRepositories returned error: %v", err) + } + + want := &OrganizationSecretSelectedRepositories{ + TotalCount: Int64(2), + Repositories: []*Repository{{ID: Int64(1)}, {ID: Int64(2)}}, + } + + if !reflect.DeepEqual(secretSelectedRepositories, want) { + t.Errorf("Organizations.ListSecretSelectedRepositories returned %+v, want %+v", secretSelectedRepositories, want) + } +} From 35de99cacf2c6e55482a8afe3a18996c53297c9d Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Thu, 21 May 2020 22:12:52 -0400 Subject: [PATCH 07/10] feat(orgs-actions-secrets): Added SetSecretSelectedRepositories support. --- github/orgs_actions_secrets.go | 14 ++++++++++++++ github/orgs_actions_secrets_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index e157aef0fff..7ab08edd8be 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -152,3 +152,17 @@ func (s *OrganizationsService) ListSecretSelectedRepositories(ctx context.Contex return secretSelectedRepositories, resp, nil } + +// Replaces all repositories for an organization secret when the visibility for repository access is set to selected. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#set-selected-repositories-for-an-organization-secret +func (s *OrganizationsService) SetSecretSelectedRepositories(ctx context.Context, owner, name string, repositoryIDs []int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories", owner, name) + + req, err := s.client.NewRequest("PUT", u, repositoryIDs) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 370581e29a5..52a96805738 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -145,3 +145,17 @@ func TestOrganizationsService_ListSecretSelectedRepositories(t *testing.T) { t.Errorf("Organizations.ListSecretSelectedRepositories returned %+v, want %+v", secretSelectedRepositories, want) } } + +func TestOrganizationsService_SetSecretSelectedRepositories(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/SECRET_NAME/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") + }) + _, err := client.Organizations.SetSecretSelectedRepositories(context.Background(), "o", "SECRET_NAME", []int64{64780797}) + if err != nil { + t.Errorf("Organizations.SetSecretSelectedRepositories returned error: %v", err) + } +} From e6ad93c343e4939e05a0972f728d7f9ea437c6ca Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Fri, 22 May 2020 08:30:50 -0400 Subject: [PATCH 08/10] feat(orgs-actions-secrets): Added AddSelectedRepositoryToSecret support. --- github/orgs_actions_secrets.go | 14 ++++++++++++++ github/orgs_actions_secrets_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index 7ab08edd8be..8f93ee83e7e 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -166,3 +166,17 @@ func (s *OrganizationsService) SetSecretSelectedRepositories(ctx context.Context return s.client.Do(ctx, req, nil) } + +// Adds a repository to an organization secret when the visibility for repository access is set to selected. +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#add-selected-repository-to-an-organization-secret +func (s *OrganizationsService) AddSelectedRepositoryToSecret(ctx context.Context, owner, name string, repositoryID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories/%v", owner, name, repositoryID) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 52a96805738..cec07f8bef3 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -159,3 +159,17 @@ func TestOrganizationsService_SetSecretSelectedRepositories(t *testing.T) { t.Errorf("Organizations.SetSecretSelectedRepositories returned error: %v", err) } } + +func TestOrganizationsService_AddSelectedRepositoryToSecret(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/SECRET_NAME/repositories/64780797", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + }) + + _, err := client.Organizations.AddSelectedRepositoryToSecret(context.Background(), "o", "SECRET_NAME", int64(64780797)) + if err != nil { + t.Errorf("Organizations.AddSelectedRepositoryToSecret returned error: %v", err) + } +} From c90fe4cd58614f95c0bb1ffae68ecce545ab2c3b Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Fri, 22 May 2020 08:36:06 -0400 Subject: [PATCH 09/10] feat(orgs-actions-secrets): Added RemoveSelectedRepositoryFromSecret support. --- github/orgs_actions_secrets.go | 14 ++++++++++++++ github/orgs_actions_secrets_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/github/orgs_actions_secrets.go b/github/orgs_actions_secrets.go index 8f93ee83e7e..7f30e864eac 100644 --- a/github/orgs_actions_secrets.go +++ b/github/orgs_actions_secrets.go @@ -180,3 +180,17 @@ func (s *OrganizationsService) AddSelectedRepositoryToSecret(ctx context.Context return s.client.Do(ctx, req, nil) } + +// Removes a repository from an organization secret when the visibility for repository access is set to selected +// +// GitHub API docs: https://developer.github.com/v3/actions/secrets/#remove-selected-repository-from-an-organization-secret +func (s *OrganizationsService) RemoveSelectedRepositoryFromSecret(ctx context.Context, owner, name string, repositoryID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories/%v", owner, name, repositoryID) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index cec07f8bef3..6c4f4d56dda 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -173,3 +173,17 @@ func TestOrganizationsService_AddSelectedRepositoryToSecret(t *testing.T) { t.Errorf("Organizations.AddSelectedRepositoryToSecret returned error: %v", err) } } + +func TestOrganizationsService_RemoveSelectedRepositoryFromSecret(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/secrets/SECRET_NAME/repositories/64780797", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Organizations.RemoveSelectedRepositoryFromSecret(context.Background(), "o", "SECRET_NAME", int64(64780797)) + if err != nil { + t.Errorf("Organizations.RemoveSelectedRepositoryFromSecret returned error: %v", err) + } +} From e78eb9e9329b956500b0ef80e29c539b14ad8a09 Mon Sep 17 00:00:00 2001 From: CJ Taylor Date: Fri, 22 May 2020 08:46:00 -0400 Subject: [PATCH 10/10] feat(orgs-actions-secrets): Added http.Status header in tests for PUT/DELETE operations. --- github/orgs_actions_secrets_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/github/orgs_actions_secrets_test.go b/github/orgs_actions_secrets_test.go index 6c4f4d56dda..43d711c5bbc 100644 --- a/github/orgs_actions_secrets_test.go +++ b/github/orgs_actions_secrets_test.go @@ -93,6 +93,7 @@ func TestOrgnizationsService_CreateOrUpdateSecret(t *testing.T) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["A","B","C"]}`+"\n") + w.WriteHeader(http.StatusCreated) }) input := &OrganizationEncryptedSecret{ @@ -114,6 +115,7 @@ func TestOrgnizationsService_DeleteSecret(t *testing.T) { mux.HandleFunc("/orgs/o/actions/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) }) _, err := client.Organizations.DeleteSecret(context.Background(), "o", "NAME") @@ -180,6 +182,7 @@ func TestOrganizationsService_RemoveSelectedRepositoryFromSecret(t *testing.T) { mux.HandleFunc("/orgs/o/actions/secrets/SECRET_NAME/repositories/64780797", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) }) _, err := client.Organizations.RemoveSelectedRepositoryFromSecret(context.Background(), "o", "SECRET_NAME", int64(64780797))