@@ -2,7 +2,6 @@ package set
22
33import (
44 "bytes"
5- "encoding/base64"
65 "encoding/json"
76 "fmt"
87 "sort"
@@ -11,14 +10,13 @@ import (
1110
1211 "github.com/cli/cli/v2/api"
1312 "github.com/cli/cli/v2/internal/ghrepo"
14- "github.com/cli/cli/v2/pkg/cmd/secret/shared"
1513)
1614
1715type SecretPayload struct {
18- EncryptedValue string `json:"encrypted_value"`
19- Visibility string `json:"visibility,omitempty"`
20- Repositories []int `json:"selected_repository_ids,omitempty"`
21- KeyID string `json:"key_id"`
16+ EncryptedValue string `json:"encrypted_value"`
17+ Visibility string `json:"visibility,omitempty"`
18+ Repositories []int64 `json:"selected_repository_ids,omitempty"`
19+ KeyID string `json:"key_id"`
2220}
2321
2422// The Codespaces Secret API currently expects repositories IDs as strings
@@ -29,7 +27,6 @@ type CodespacesSecretPayload struct {
2927}
3028
3129type PubKey struct {
32- Raw [32 ]byte
3330 ID string `json:"key_id"`
3431 Key string
3532}
@@ -40,17 +37,6 @@ func getPubKey(client *api.Client, host, path string) (*PubKey, error) {
4037 if err != nil {
4138 return nil , err
4239 }
43-
44- if pk .Key == "" {
45- return nil , fmt .Errorf ("failed to find public key at %s/%s" , host , path )
46- }
47-
48- decoded , err := base64 .StdEncoding .DecodeString (pk .Key )
49- if err != nil {
50- return nil , fmt .Errorf ("failed to decode public key: %w" , err )
51- }
52-
53- copy (pk .Raw [:], decoded [0 :32 ])
5440 return & pk , nil
5541}
5642
@@ -82,33 +68,7 @@ func putSecret(client *api.Client, host, path string, payload interface{}) error
8268 return client .REST (host , "PUT" , path , requestBody , nil )
8369}
8470
85- func putOrgSecret (client * api.Client , host string , pk * PubKey , opts SetOptions , eValue string ) error {
86- secretName := opts .SecretName
87- orgName := opts .OrgName
88- visibility := opts .Visibility
89-
90- var repositoryIDs []int
91- var err error
92- if orgName != "" && visibility == shared .Selected {
93- repos := make ([]ghrepo.Interface , 0 , len (opts .RepositoryNames ))
94- for _ , repositoryName := range opts .RepositoryNames {
95- var repo ghrepo.Interface
96- if strings .Contains (repositoryName , "/" ) {
97- repo , err = ghrepo .FromFullNameWithHost (repositoryName , host )
98- if err != nil {
99- return fmt .Errorf ("invalid repository name: %w" , err )
100- }
101- } else {
102- repo = ghrepo .NewWithHost (opts .OrgName , repositoryName , host )
103- }
104- repos = append (repos , repo )
105- }
106- repositoryIDs , err = mapRepoToID (client , host , repos )
107- if err != nil {
108- return fmt .Errorf ("failed to look up IDs for repositories %v: %w" , opts .RepositoryNames , err )
109- }
110- }
111-
71+ func putOrgSecret (client * api.Client , host string , pk * PubKey , orgName , visibility , secretName , eValue string , repositoryIDs []int64 ) error {
11272 payload := SecretPayload {
11373 EncryptedValue : eValue ,
11474 KeyID : pk .ID ,
@@ -120,35 +80,21 @@ func putOrgSecret(client *api.Client, host string, pk *PubKey, opts SetOptions,
12080 return putSecret (client , host , path , payload )
12181}
12282
123- func putUserSecret (client * api.Client , host string , pk * PubKey , opts SetOptions , eValue string ) error {
83+ func putUserSecret (client * api.Client , host string , pk * PubKey , key , eValue string , repositoryIDs [] int64 ) error {
12484 payload := CodespacesSecretPayload {
12585 EncryptedValue : eValue ,
12686 KeyID : pk .ID ,
12787 }
12888
129- if len (opts .RepositoryNames ) > 0 {
130- repos := make ([]ghrepo.Interface , len (opts .RepositoryNames ))
131- for i , repo := range opts .RepositoryNames {
132- // For user secrets, repository names should be fully qualifed (e.g. "owner/repo")
133- repoNWO , err := ghrepo .FromFullNameWithHost (repo , host )
134- if err != nil {
135- return err
136- }
137- repos [i ] = repoNWO
138- }
139-
140- repositoryIDs , err := mapRepoToID (client , host , repos )
141- if err != nil {
142- return fmt .Errorf ("failed to look up repository IDs: %w" , err )
143- }
89+ if len (repositoryIDs ) > 0 {
14490 repositoryStringIDs := make ([]string , len (repositoryIDs ))
14591 for i , id := range repositoryIDs {
146- repositoryStringIDs [i ] = strconv .Itoa (id )
92+ repositoryStringIDs [i ] = strconv .FormatInt (id , 10 )
14793 }
14894 payload .Repositories = repositoryStringIDs
14995 }
15096
151- path := fmt .Sprintf ("user/codespaces/secrets/%s" , opts . SecretName )
97+ path := fmt .Sprintf ("user/codespaces/secrets/%s" , key )
15298 return putSecret (client , host , path , payload )
15399}
154100
@@ -171,7 +117,7 @@ func putRepoSecret(client *api.Client, pk *PubKey, repo ghrepo.Interface, secret
171117}
172118
173119// This does similar logic to `api.RepoNetwork`, but without the overfetching.
174- func mapRepoToID (client * api.Client , host string , repositories []ghrepo.Interface ) ([]int , error ) {
120+ func mapRepoToID (client * api.Client , host string , repositories []ghrepo.Interface ) ([]int64 , error ) {
175121 queries := make ([]string , 0 , len (repositories ))
176122 for i , repo := range repositories {
177123 queries = append (queries , fmt .Sprintf (`
@@ -184,7 +130,7 @@ func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interfac
184130 query := fmt .Sprintf (`query MapRepositoryNames { %s }` , strings .Join (queries , "" ))
185131
186132 graphqlResult := make (map [string ]* struct {
187- DatabaseID int `json:"databaseId"`
133+ DatabaseID int64 `json:"databaseId"`
188134 })
189135
190136 if err := client .GraphQL (host , query , nil , & graphqlResult ); err != nil {
@@ -197,10 +143,9 @@ func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interfac
197143 }
198144 sort .Strings (repoKeys )
199145
200- result := make ([]int , len (repositories ))
146+ result := make ([]int64 , len (repositories ))
201147 for i , k := range repoKeys {
202148 result [i ] = graphqlResult [k ].DatabaseID
203149 }
204-
205150 return result , nil
206151}
0 commit comments