Skip to content

Commit 4e8a680

Browse files
committed
remove implied org functionality from secret set
1 parent 1675bd9 commit 4e8a680

File tree

3 files changed

+20
-44
lines changed

3 files changed

+20
-44
lines changed

pkg/cmd/secret/set/http.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/cli/cli/api"
11+
"github.com/cli/cli/internal/ghinstance"
1112
"github.com/cli/cli/internal/ghrepo"
1213
"github.com/cli/cli/pkg/cmd/secret/shared"
1314
)
@@ -45,7 +46,8 @@ func getPubKey(client *api.Client, host, path string) (*PubKey, error) {
4546
return &pk, nil
4647
}
4748

48-
func getOrgPublicKey(client *api.Client, host, orgName string) (*PubKey, error) {
49+
func getOrgPublicKey(client *api.Client, orgName string) (*PubKey, error) {
50+
host := ghinstance.OverridableDefault()
4951
return getPubKey(client, host, fmt.Sprintf("orgs/%s/actions/secrets/public-key", orgName))
5052
}
5153

@@ -64,10 +66,11 @@ func putSecret(client *api.Client, host, path string, payload SecretPayload) err
6466
return client.REST(host, "PUT", path, requestBody, nil)
6567
}
6668

67-
func putOrgSecret(client *api.Client, pk *PubKey, host string, opts SetOptions, eValue string) error {
69+
func putOrgSecret(client *api.Client, pk *PubKey, opts SetOptions, eValue string) error {
6870
secretName := opts.SecretName
6971
orgName := opts.OrgName
7072
visibility := opts.Visibility
73+
host := ghinstance.OverridableDefault()
7174

7275
var repositoryIDs []int
7376
var err error

pkg/cmd/secret/set/set.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/MakeNowJust/heredoc"
1414
"github.com/cli/cli/api"
15-
"github.com/cli/cli/internal/ghinstance"
1615
"github.com/cli/cli/internal/ghrepo"
1716
"github.com/cli/cli/pkg/cmd/secret/shared"
1817
"github.com/cli/cli/pkg/cmdutil"
@@ -102,8 +101,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
102101
return setRun(opts)
103102
},
104103
}
105-
cmd.Flags().StringVar(&opts.OrgName, "org", "", "List secrets for an organization")
106-
cmd.Flags().Lookup("org").NoOptDefVal = "@owner"
104+
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "List secrets for an organization")
107105
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `all`, `private`, or `selected`")
108106
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of repository names for `selected` visibility")
109107
cmd.Flags().StringVarP(&opts.Body, "body", "b", "-", "Provide either a literal string or a file path; prepend file paths with an @. Reads from STDIN if not provided.")
@@ -123,23 +121,19 @@ func setRun(opts *SetOptions) error {
123121
}
124122
client := api.NewClientFromHTTP(c)
125123

124+
orgName := opts.OrgName
125+
126126
var baseRepo ghrepo.Interface
127-
if opts.OrgName == "" || opts.OrgName == "@owner" {
127+
if orgName == "" {
128128
baseRepo, err = opts.BaseRepo()
129129
if err != nil {
130130
return fmt.Errorf("could not determine base repo: %w", err)
131131
}
132132
}
133133

134-
host := ghinstance.OverridableDefault()
135-
if opts.OrgName == "@owner" {
136-
opts.OrgName = baseRepo.RepoOwner()
137-
host = baseRepo.RepoHost()
138-
}
139-
140134
var pk *PubKey
141135
if opts.OrgName != "" {
142-
pk, err = getOrgPublicKey(client, host, opts.OrgName)
136+
pk, err = getOrgPublicKey(client, opts.OrgName)
143137
} else {
144138
pk, err = getRepoPubKey(client, baseRepo)
145139
}
@@ -155,7 +149,7 @@ func setRun(opts *SetOptions) error {
155149
encoded := base64.StdEncoding.EncodeToString(eBody)
156150

157151
if opts.OrgName != "" {
158-
err = putOrgSecret(client, pk, host, *opts, encoded)
152+
err = putOrgSecret(client, pk, *opts, encoded)
159153
} else {
160154
err = putRepoSecret(client, pk, baseRepo, opts.SecretName, encoded)
161155
}

pkg/cmd/secret/set/set_test.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func TestNewCmdSet(t *testing.T) {
2828
}{
2929
{
3030
name: "invalid visibility",
31-
cli: "cool_secret --org -v'mistyVeil'",
31+
cli: "cool_secret --org coolOrg -v'mistyVeil'",
3232
wantsErr: true,
3333
},
3434
{
3535
name: "invalid visibility",
36-
cli: "cool_secret --org -v'selected'",
36+
cli: "cool_secret --org coolOrg -v'selected'",
3737
wantsErr: true,
3838
},
3939
{
@@ -58,8 +58,8 @@ func TestNewCmdSet(t *testing.T) {
5858
wantsErr: true,
5959
},
6060
{
61-
name: "explicit org with selected repo",
62-
cli: "--org=coolOrg -vselected -rcoolRepo cool_secret",
61+
name: "org with selected repo",
62+
cli: "-ocoolOrg -vselected -rcoolRepo cool_secret",
6363
wants: SetOptions{
6464
SecretName: "cool_secret",
6565
Visibility: shared.VisSelected,
@@ -69,7 +69,7 @@ func TestNewCmdSet(t *testing.T) {
6969
},
7070
},
7171
{
72-
name: "explicit org with selected repos",
72+
name: "org with selected repos",
7373
cli: `--org=coolOrg -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
7474
wants: SetOptions{
7575
SecretName: "cool_secret",
@@ -89,24 +89,14 @@ func TestNewCmdSet(t *testing.T) {
8989
OrgName: "",
9090
},
9191
},
92-
{
93-
name: "implicit org",
94-
cli: `cool_secret --org -b"@cool.json"`,
95-
wants: SetOptions{
96-
SecretName: "cool_secret",
97-
Visibility: shared.VisPrivate,
98-
Body: "@cool.json",
99-
OrgName: "@owner",
100-
},
101-
},
10292
{
10393
name: "vis all",
104-
cli: `cool_secret --org -b"@cool.json" -vall`,
94+
cli: `cool_secret --org coolOrg -b"@cool.json" -vall`,
10595
wants: SetOptions{
10696
SecretName: "cool_secret",
10797
Visibility: shared.VisAll,
10898
Body: "@cool.json",
109-
OrgName: "@owner",
99+
OrgName: "coolOrg",
110100
},
111101
},
112102
{
@@ -210,19 +200,12 @@ func Test_setRun_org(t *testing.T) {
210200
wantRepositories []int
211201
}{
212202
{
213-
name: "explicit org name",
203+
name: "all vis",
214204
opts: &SetOptions{
215205
OrgName: "UmbrellaCorporation",
216206
Visibility: shared.VisAll,
217207
},
218208
},
219-
{
220-
name: "implicit org name",
221-
opts: &SetOptions{
222-
OrgName: "@owner",
223-
Visibility: shared.VisPrivate,
224-
},
225-
},
226209
{
227210
name: "selected visibility",
228211
opts: &SetOptions{
@@ -238,11 +221,7 @@ func Test_setRun_org(t *testing.T) {
238221
t.Run(tt.name, func(t *testing.T) {
239222
reg := &httpmock.Registry{}
240223

241-
impliedOrgName := "NeoUmbrella"
242224
orgName := tt.opts.OrgName
243-
if orgName == "@owner" {
244-
orgName = impliedOrgName
245-
}
246225

247226
reg.Register(httpmock.REST("GET",
248227
fmt.Sprintf("orgs/%s/actions/secrets/public-key", orgName)),
@@ -260,7 +239,7 @@ func Test_setRun_org(t *testing.T) {
260239
io, _, _, _ := iostreams.Test()
261240

262241
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
263-
return ghrepo.FromFullName(fmt.Sprintf("%s/repo", impliedOrgName))
242+
return ghrepo.FromFullName("owner/repo")
264243
}
265244
tt.opts.HttpClient = func() (*http.Client, error) {
266245
return &http.Client{Transport: reg}, nil

0 commit comments

Comments
 (0)