From da258fb4f4cdbc28741bc5a8135ae75098e9c82c Mon Sep 17 00:00:00 2001 From: nate smith Date: Thu, 28 Oct 2021 12:26:01 -0500 Subject: [PATCH] confirm when inferring repository --- pkg/cmd/repo/rename/rename.go | 26 +++++++++++++ pkg/cmd/repo/rename/rename_test.go | 61 ++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/repo/rename/rename.go b/pkg/cmd/repo/rename/rename.go index fee37b2be..d476a9600 100644 --- a/pkg/cmd/repo/rename/rename.go +++ b/pkg/cmd/repo/rename/rename.go @@ -22,6 +22,7 @@ type RenameOptions struct { Config func() (config.Config, error) BaseRepo func() (ghrepo.Interface, error) Remotes func() (context.Remotes, error) + DoConfirm bool HasRepoOverride bool newRepoSelector string } @@ -34,6 +35,8 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co Config: f.Config, } + var confirm bool + cmd := &cobra.Command{ Use: "rename []", Short: "Rename a repository", @@ -51,6 +54,13 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co return cmdutil.FlagErrorf("new name argument required when not running interactively\n") } + if len(args) == 1 && !confirm && !opts.HasRepoOverride { + if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("--confirm required when passing a single argument") + } + opts.DoConfirm = true + } + if runf != nil { return runf(opts) } @@ -59,6 +69,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co } cmdutil.EnableRepoOverride(cmd, f) + cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "skip confirmation prompt") return cmd } @@ -88,6 +99,21 @@ func renameRun(opts *RenameOptions) error { } } + if opts.DoConfirm { + var confirmed bool + p := &survey.Confirm{ + Message: fmt.Sprintf("Rename %s to %s?", ghrepo.FullName(currRepo), newRepoName), + Default: false, + } + err = prompt.SurveyAskOne(p, &confirmed) + if err != nil { + return fmt.Errorf("failed to prompt: %w", err) + } + if !confirmed { + return nil + } + } + err = runRename(httpClient, currRepo, newRepoName) if err != nil { return fmt.Errorf("API called failed: %s", err) diff --git a/pkg/cmd/repo/rename/rename_test.go b/pkg/cmd/repo/rename/rename_test.go index 63dad9245..e91317f82 100644 --- a/pkg/cmd/repo/rename/rename_test.go +++ b/pkg/cmd/repo/rename/rename_test.go @@ -32,15 +32,37 @@ func TestNewCmdRename(t *testing.T) { input: "", errMsg: "new name argument required when not running interactively\n", wantErr: true, - tty: false, }, { - name: "one argument", - input: "REPO", + name: "one argument no tty confirmed", + input: "REPO --confirm", output: RenameOptions{ newRepoSelector: "REPO", }, }, + { + name: "one argument no tty", + input: "REPO", + errMsg: "--confirm required when passing a single argument", + wantErr: true, + }, + { + name: "one argument tty confirmed", + input: "REPO --confirm", + tty: true, + output: RenameOptions{ + newRepoSelector: "REPO", + }, + }, + { + name: "one argument tty", + input: "REPO", + tty: true, + output: RenameOptions{ + newRepoSelector: "REPO", + DoConfirm: true, + }, + }, { name: "full flag argument", input: "--repo OWNER/REPO NEW_REPO", @@ -153,6 +175,39 @@ func TestRenameRun(t *testing.T) { cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "") }, }, + { + name: "confirmation with yes", + tty: true, + opts: RenameOptions{ + newRepoSelector: "NEW_REPO", + DoConfirm: true, + }, + wantOut: "āœ“ Renamed repository OWNER/NEW_REPO\nāœ“ Updated the \"origin\" remote\n", + askStubs: func(q *prompt.AskStubber) { + q.StubOne(true) + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("PATCH", "repos/OWNER/REPO"), + httpmock.StatusStringResponse(204, "{}")) + }, + execStubs: func(cs *run.CommandStubber) { + cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "") + }, + }, + + { + name: "confirmation with no", + tty: true, + opts: RenameOptions{ + newRepoSelector: "NEW_REPO", + DoConfirm: true, + }, + askStubs: func(q *prompt.AskStubber) { + q.StubOne(false) + }, + wantOut: "", + }, } for _, tt := range testCases {