Skip to content

Commit

Permalink
Merge pull request cli#3774 from browniebroke/feat/remove-env-secret
Browse files Browse the repository at this point in the history
Add support for removing environment secrets
  • Loading branch information
mislav authored Jun 3, 2021
2 parents a1cedfc + 4b79edf commit b166376
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkg/cmd/secret/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RemoveOptions struct {

SecretName string
OrgName string
EnvName string
}

func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Command {
Expand All @@ -31,12 +32,16 @@ func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Co

cmd := &cobra.Command{
Use: "remove <secret-name>",
Short: "Remove an organization or repository secret",
Short: "Remove an organization, environment, or repository secret",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo

if err := cmdutil.MutuallyExclusive("specify only one of `--org` or `--env`", opts.OrgName != "", opts.EnvName != ""); err != nil {
return err
}

opts.SecretName = args[0]

if runF != nil {
Expand All @@ -46,7 +51,8 @@ func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Co
return removeRun(opts)
},
}
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "List secrets for an organization")
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Remove a secret for an organization")
cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Remove a secret for an environment")

return cmd
}
Expand All @@ -59,6 +65,7 @@ func removeRun(opts *RemoveOptions) error {
client := api.NewClientFromHTTP(c)

orgName := opts.OrgName
envName := opts.EnvName

var baseRepo ghrepo.Interface
if orgName == "" {
Expand All @@ -69,10 +76,12 @@ func removeRun(opts *RemoveOptions) error {
}

var path string
if orgName == "" {
path = fmt.Sprintf("repos/%s/actions/secrets/%s", ghrepo.FullName(baseRepo), opts.SecretName)
} else {
if orgName != "" {
path = fmt.Sprintf("orgs/%s/actions/secrets/%s", orgName, opts.SecretName)
} else if envName != "" {
path = fmt.Sprintf("repos/%s/environments/%s/secrets/%s", ghrepo.FullName(baseRepo), envName, opts.SecretName)
} else {
path = fmt.Sprintf("repos/%s/actions/secrets/%s", ghrepo.FullName(baseRepo), opts.SecretName)
}

cfg, err := opts.Config()
Expand All @@ -96,7 +105,11 @@ func removeRun(opts *RemoveOptions) error {
target = ghrepo.FullName(baseRepo)
}
cs := opts.IO.ColorScheme()
fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, target)
if envName != "" {
fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s environment on %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, envName, target)
} else {
fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, target)
}
}

return nil
Expand Down
39 changes: 39 additions & 0 deletions pkg/cmd/secret/remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func TestNewCmdRemove(t *testing.T) {
OrgName: "anOrg",
},
},
{
name: "env",
cli: "cool --env anEnv",
wants: RemoveOptions{
SecretName: "cool",
EnvName: "anEnv",
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -72,6 +80,7 @@ func TestNewCmdRemove(t *testing.T) {

assert.Equal(t, tt.wants.SecretName, gotOpts.SecretName)
assert.Equal(t, tt.wants.OrgName, gotOpts.OrgName)
assert.Equal(t, tt.wants.EnvName, gotOpts.EnvName)
})
}

Expand Down Expand Up @@ -106,6 +115,36 @@ func Test_removeRun_repo(t *testing.T) {
reg.Verify(t)
}

func Test_removeRun_env(t *testing.T) {
reg := &httpmock.Registry{}

reg.Register(
httpmock.REST("DELETE", "repos/owner/repo/environments/development/secrets/cool_secret"),
httpmock.StatusStringResponse(204, "No Content"))

io, _, _, _ := iostreams.Test()

opts := &RemoveOptions{
IO: io,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.FromFullName("owner/repo")
},
SecretName: "cool_secret",
EnvName: "development",
}

err := removeRun(opts)
assert.NoError(t, err)

reg.Verify(t)
}

func Test_removeRun_org(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit b166376

Please sign in to comment.