remove implied org functionality from secret set

This commit is contained in:
vilmibm 2020-12-10 13:22:11 -08:00
parent 1675bd9249
commit 4e8a680575
3 changed files with 20 additions and 44 deletions

View file

@ -8,6 +8,7 @@ import (
"strings"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/secret/shared"
)
@ -45,7 +46,8 @@ func getPubKey(client *api.Client, host, path string) (*PubKey, error) {
return &pk, nil
}
func getOrgPublicKey(client *api.Client, host, orgName string) (*PubKey, error) {
func getOrgPublicKey(client *api.Client, orgName string) (*PubKey, error) {
host := ghinstance.OverridableDefault()
return getPubKey(client, host, fmt.Sprintf("orgs/%s/actions/secrets/public-key", orgName))
}
@ -64,10 +66,11 @@ func putSecret(client *api.Client, host, path string, payload SecretPayload) err
return client.REST(host, "PUT", path, requestBody, nil)
}
func putOrgSecret(client *api.Client, pk *PubKey, host string, opts SetOptions, eValue string) error {
func putOrgSecret(client *api.Client, pk *PubKey, opts SetOptions, eValue string) error {
secretName := opts.SecretName
orgName := opts.OrgName
visibility := opts.Visibility
host := ghinstance.OverridableDefault()
var repositoryIDs []int
var err error

View file

@ -12,7 +12,6 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/secret/shared"
"github.com/cli/cli/pkg/cmdutil"
@ -102,8 +101,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
return setRun(opts)
},
}
cmd.Flags().StringVar(&opts.OrgName, "org", "", "List secrets for an organization")
cmd.Flags().Lookup("org").NoOptDefVal = "@owner"
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "List secrets for an organization")
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `all`, `private`, or `selected`")
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of repository names for `selected` visibility")
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 {
}
client := api.NewClientFromHTTP(c)
orgName := opts.OrgName
var baseRepo ghrepo.Interface
if opts.OrgName == "" || opts.OrgName == "@owner" {
if orgName == "" {
baseRepo, err = opts.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine base repo: %w", err)
}
}
host := ghinstance.OverridableDefault()
if opts.OrgName == "@owner" {
opts.OrgName = baseRepo.RepoOwner()
host = baseRepo.RepoHost()
}
var pk *PubKey
if opts.OrgName != "" {
pk, err = getOrgPublicKey(client, host, opts.OrgName)
pk, err = getOrgPublicKey(client, opts.OrgName)
} else {
pk, err = getRepoPubKey(client, baseRepo)
}
@ -155,7 +149,7 @@ func setRun(opts *SetOptions) error {
encoded := base64.StdEncoding.EncodeToString(eBody)
if opts.OrgName != "" {
err = putOrgSecret(client, pk, host, *opts, encoded)
err = putOrgSecret(client, pk, *opts, encoded)
} else {
err = putRepoSecret(client, pk, baseRepo, opts.SecretName, encoded)
}

View file

@ -28,12 +28,12 @@ func TestNewCmdSet(t *testing.T) {
}{
{
name: "invalid visibility",
cli: "cool_secret --org -v'mistyVeil'",
cli: "cool_secret --org coolOrg -v'mistyVeil'",
wantsErr: true,
},
{
name: "invalid visibility",
cli: "cool_secret --org -v'selected'",
cli: "cool_secret --org coolOrg -v'selected'",
wantsErr: true,
},
{
@ -58,8 +58,8 @@ func TestNewCmdSet(t *testing.T) {
wantsErr: true,
},
{
name: "explicit org with selected repo",
cli: "--org=coolOrg -vselected -rcoolRepo cool_secret",
name: "org with selected repo",
cli: "-ocoolOrg -vselected -rcoolRepo cool_secret",
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.VisSelected,
@ -69,7 +69,7 @@ func TestNewCmdSet(t *testing.T) {
},
},
{
name: "explicit org with selected repos",
name: "org with selected repos",
cli: `--org=coolOrg -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
wants: SetOptions{
SecretName: "cool_secret",
@ -89,24 +89,14 @@ func TestNewCmdSet(t *testing.T) {
OrgName: "",
},
},
{
name: "implicit org",
cli: `cool_secret --org -b"@cool.json"`,
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.VisPrivate,
Body: "@cool.json",
OrgName: "@owner",
},
},
{
name: "vis all",
cli: `cool_secret --org -b"@cool.json" -vall`,
cli: `cool_secret --org coolOrg -b"@cool.json" -vall`,
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.VisAll,
Body: "@cool.json",
OrgName: "@owner",
OrgName: "coolOrg",
},
},
{
@ -210,19 +200,12 @@ func Test_setRun_org(t *testing.T) {
wantRepositories []int
}{
{
name: "explicit org name",
name: "all vis",
opts: &SetOptions{
OrgName: "UmbrellaCorporation",
Visibility: shared.VisAll,
},
},
{
name: "implicit org name",
opts: &SetOptions{
OrgName: "@owner",
Visibility: shared.VisPrivate,
},
},
{
name: "selected visibility",
opts: &SetOptions{
@ -238,11 +221,7 @@ func Test_setRun_org(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
impliedOrgName := "NeoUmbrella"
orgName := tt.opts.OrgName
if orgName == "@owner" {
orgName = impliedOrgName
}
reg.Register(httpmock.REST("GET",
fmt.Sprintf("orgs/%s/actions/secrets/public-key", orgName)),
@ -260,7 +239,7 @@ func Test_setRun_org(t *testing.T) {
io, _, _, _ := iostreams.Test()
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
return ghrepo.FromFullName(fmt.Sprintf("%s/repo", impliedOrgName))
return ghrepo.FromFullName("owner/repo")
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil