Skip to content

Commit

Permalink
Change DependabotSecretsSelectedRepoIDs to []string (#2401)
Browse files Browse the repository at this point in the history
Fixes #2400.
  • Loading branch information
gmlewis committed Aug 13, 2022
1 parent 388289c commit ce1f6b9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion github/actions_secrets.go
Expand Up @@ -186,7 +186,7 @@ func (s *ActionsService) GetEnvSecret(ctx context.Context, repoID int, env, secr
return s.getSecret(ctx, url)
}

// SelectedRepoIDs are the repository IDs that have access to the secret.
// SelectedRepoIDs are the repository IDs that have access to the actions secrets.
type SelectedRepoIDs []int64

// EncryptedSecret represents a secret that is encrypted using a public key.
Expand Down
26 changes: 21 additions & 5 deletions github/dependabot_secrets.go
Expand Up @@ -110,7 +110,20 @@ func (s *DependabotService) GetOrgSecret(ctx context.Context, org, name string)
return s.getSecret(ctx, url)
}

func (s *DependabotService) putSecret(ctx context.Context, url string, eSecret *EncryptedSecret) (*Response, error) {
// DependabotEncryptedSecret represents a secret that is encrypted using a public key for Dependabot.
//
// 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 DependabotEncryptedSecret struct {
Name string `json:"-"`
KeyID string `json:"key_id"`
EncryptedValue string `json:"encrypted_value"`
Visibility string `json:"visibility,omitempty"`
SelectedRepositoryIDs DependabotSecretsSelectedRepoIDs `json:"selected_repository_ids,omitempty"`
}

func (s *DependabotService) putSecret(ctx context.Context, url string, eSecret *DependabotEncryptedSecret) (*Response, error) {
req, err := s.client.NewRequest("PUT", url, eSecret)
if err != nil {
return nil, err
Expand All @@ -122,15 +135,15 @@ func (s *DependabotService) putSecret(ctx context.Context, url string, eSecret *
// CreateOrUpdateRepoSecret creates or updates a repository Dependabot secret with an encrypted value.
//
// GitHub API docs: https://docs.github.com/en/rest/dependabot/secrets#create-or-update-a-repository-secret
func (s *DependabotService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *EncryptedSecret) (*Response, error) {
func (s *DependabotService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *DependabotEncryptedSecret) (*Response, error) {
url := fmt.Sprintf("repos/%v/%v/dependabot/secrets/%v", owner, repo, eSecret.Name)
return s.putSecret(ctx, url, eSecret)
}

// CreateOrUpdateOrgSecret creates or updates an organization Dependabot secret with an encrypted value.
//
// GitHub API docs: https://docs.github.com/en/rest/dependabot/secrets#create-or-update-an-organization-secret
func (s *DependabotService) CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *EncryptedSecret) (*Response, error) {
func (s *DependabotService) CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *DependabotEncryptedSecret) (*Response, error) {
url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v", org, eSecret.Name)
return s.putSecret(ctx, url, eSecret)
}
Expand Down Expand Up @@ -184,13 +197,16 @@ func (s *DependabotService) ListSelectedReposForOrgSecret(ctx context.Context, o
return result, resp, nil
}

// DependabotSecretsSelectedRepoIDs are the repository IDs that have access to the dependabot secrets.
type DependabotSecretsSelectedRepoIDs []string

// SetSelectedReposForOrgSecret sets the repositories that have access to a Dependabot secret.
//
// GitHub API docs: https://docs.github.com/en/rest/dependabot/secrets#set-selected-repositories-for-an-organization-secret
func (s *DependabotService) SetSelectedReposForOrgSecret(ctx context.Context, org, name string, ids SelectedRepoIDs) (*Response, error) {
func (s *DependabotService) SetSelectedReposForOrgSecret(ctx context.Context, org, name string, ids DependabotSecretsSelectedRepoIDs) (*Response, error) {
url := fmt.Sprintf("orgs/%v/dependabot/secrets/%v/repositories", org, name)
type repoIDs struct {
SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"`
SelectedIDs DependabotSecretsSelectedRepoIDs `json:"selected_repository_ids"`
}

req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids})
Expand Down
16 changes: 8 additions & 8 deletions github/dependabot_secrets_test.go
Expand Up @@ -178,7 +178,7 @@ func TestDependabotService_CreateOrUpdateRepoSecret(t *testing.T) {
w.WriteHeader(http.StatusCreated)
})

input := &EncryptedSecret{
input := &DependabotEncryptedSecret{
Name: "NAME",
EncryptedValue: "QIv=",
KeyID: "1234",
Expand Down Expand Up @@ -352,16 +352,16 @@ func TestDependabotService_CreateOrUpdateOrgSecret(t *testing.T) {
mux.HandleFunc("/orgs/o/dependabot/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":[1296269,1269280]}`+"\n")
testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["1296269","1269280"]}`+"\n")
w.WriteHeader(http.StatusCreated)
})

input := &EncryptedSecret{
input := &DependabotEncryptedSecret{
Name: "NAME",
EncryptedValue: "QIv=",
KeyID: "1234",
Visibility: "selected",
SelectedRepositoryIDs: SelectedRepoIDs{1296269, 1269280},
SelectedRepositoryIDs: DependabotSecretsSelectedRepoIDs{"1296269", "1269280"},
}
ctx := context.Background()
_, err := client.Dependabot.CreateOrUpdateOrgSecret(ctx, "o", input)
Expand Down Expand Up @@ -428,23 +428,23 @@ func TestDependabotService_SetSelectedReposForOrgSecret(t *testing.T) {
mux.HandleFunc("/orgs/o/dependabot/secrets/NAME/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":[64780797]}`+"\n")
testBody(t, r, `{"selected_repository_ids":["64780797"]}`+"\n")
})

ctx := context.Background()
_, err := client.Dependabot.SetSelectedReposForOrgSecret(ctx, "o", "NAME", SelectedRepoIDs{64780797})
_, err := client.Dependabot.SetSelectedReposForOrgSecret(ctx, "o", "NAME", DependabotSecretsSelectedRepoIDs{"64780797"})
if err != nil {
t.Errorf("Dependabot.SetSelectedReposForOrgSecret returned error: %v", err)
}

const methodName = "SetSelectedReposForOrgSecret"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Dependabot.SetSelectedReposForOrgSecret(ctx, "\n", "\n", SelectedRepoIDs{64780797})
_, err = client.Dependabot.SetSelectedReposForOrgSecret(ctx, "\n", "\n", DependabotSecretsSelectedRepoIDs{"64780797"})
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Dependabot.SetSelectedReposForOrgSecret(ctx, "o", "NAME", SelectedRepoIDs{64780797})
return client.Dependabot.SetSelectedReposForOrgSecret(ctx, "o", "NAME", DependabotSecretsSelectedRepoIDs{"64780797"})
})
}

Expand Down

0 comments on commit ce1f6b9

Please sign in to comment.