From f6431ca001f5b5b26bb192f35504128f032ef550 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Fri, 27 Jan 2023 14:36:35 -0500 Subject: [PATCH] Use int64 repository IDs for Codespaces user secrets --- pkg/cmd/secret/set/http.go | 22 +++------------------- pkg/cmd/secret/set/set_test.go | 6 +++--- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/pkg/cmd/secret/set/http.go b/pkg/cmd/secret/set/http.go index f36c2b59e..2ea2e8eff 100644 --- a/pkg/cmd/secret/set/http.go +++ b/pkg/cmd/secret/set/http.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "sort" - "strconv" "strings" "github.com/cli/cli/v2/api" @@ -20,13 +19,6 @@ type SecretPayload struct { KeyID string `json:"key_id"` } -// The Codespaces Secret API currently expects repositories IDs as strings -type CodespacesSecretPayload struct { - EncryptedValue string `json:"encrypted_value"` - Repositories []string `json:"selected_repository_ids,omitempty"` - KeyID string `json:"key_id"` -} - type PubKey struct { ID string `json:"key_id"` Key string @@ -59,7 +51,7 @@ func getEnvPubKey(client *api.Client, repo ghrepo.Interface, envName string) (*P ghrepo.FullName(repo), envName)) } -func putSecret(client *api.Client, host, path string, payload interface{}) error { +func putSecret(client *api.Client, host, path string, payload SecretPayload) error { payloadBytes, err := json.Marshal(payload) if err != nil { return fmt.Errorf("failed to serialize: %w", err) @@ -82,19 +74,11 @@ func putOrgSecret(client *api.Client, host string, pk *PubKey, orgName, visibili } func putUserSecret(client *api.Client, host string, pk *PubKey, key, eValue string, repositoryIDs []int64) error { - payload := CodespacesSecretPayload{ + payload := SecretPayload{ EncryptedValue: eValue, KeyID: pk.ID, + Repositories: repositoryIDs, } - - if len(repositoryIDs) > 0 { - repositoryStringIDs := make([]string, len(repositoryIDs)) - for i, id := range repositoryIDs { - repositoryStringIDs[i] = strconv.FormatInt(id, 10) - } - payload.Repositories = repositoryStringIDs - } - path := fmt.Sprintf("user/codespaces/secrets/%s", key) return putSecret(client, host, path, payload) } diff --git a/pkg/cmd/secret/set/set_test.go b/pkg/cmd/secret/set/set_test.go index d9313f81f..babc5dc4f 100644 --- a/pkg/cmd/secret/set/set_test.go +++ b/pkg/cmd/secret/set/set_test.go @@ -426,7 +426,7 @@ func Test_setRun_user(t *testing.T) { name string opts *SetOptions wantVisibility shared.Visibility - wantRepositories []string + wantRepositories []int64 }{ { name: "all vis", @@ -442,7 +442,7 @@ func Test_setRun_user(t *testing.T) { Visibility: shared.Selected, RepositoryNames: []string{"cli/cli", "github/hub"}, }, - wantRepositories: []string{"212613049", "401025"}, + wantRepositories: []int64{212613049, 401025}, }, } @@ -481,7 +481,7 @@ func Test_setRun_user(t *testing.T) { data, err := io.ReadAll(reg.Requests[len(reg.Requests)-1].Body) assert.NoError(t, err) - var payload CodespacesSecretPayload + var payload SecretPayload err = json.Unmarshal(data, &payload) assert.NoError(t, err) assert.Equal(t, payload.KeyID, "123")