Use int64 repository IDs for Codespaces user secrets

This commit is contained in:
Josh Gross 2023-01-27 14:36:35 -05:00
parent 2561f80369
commit f6431ca001
No known key found for this signature in database
2 changed files with 6 additions and 22 deletions

View file

@ -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)
}

View file

@ -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")