Skip to content

Commit da258fb

Browse files
author
nate smith
committed
confirm when inferring repository
1 parent 296cf38 commit da258fb

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

pkg/cmd/repo/rename/rename.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type RenameOptions struct {
2222
Config func() (config.Config, error)
2323
BaseRepo func() (ghrepo.Interface, error)
2424
Remotes func() (context.Remotes, error)
25+
DoConfirm bool
2526
HasRepoOverride bool
2627
newRepoSelector string
2728
}
@@ -34,6 +35,8 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
3435
Config: f.Config,
3536
}
3637

38+
var confirm bool
39+
3740
cmd := &cobra.Command{
3841
Use: "rename [<new-name>]",
3942
Short: "Rename a repository",
@@ -51,6 +54,13 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
5154
return cmdutil.FlagErrorf("new name argument required when not running interactively\n")
5255
}
5356

57+
if len(args) == 1 && !confirm && !opts.HasRepoOverride {
58+
if !opts.IO.CanPrompt() {
59+
return cmdutil.FlagErrorf("--confirm required when passing a single argument")
60+
}
61+
opts.DoConfirm = true
62+
}
63+
5464
if runf != nil {
5565
return runf(opts)
5666
}
@@ -59,6 +69,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
5969
}
6070

6171
cmdutil.EnableRepoOverride(cmd, f)
72+
cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "skip confirmation prompt")
6273

6374
return cmd
6475
}
@@ -88,6 +99,21 @@ func renameRun(opts *RenameOptions) error {
8899
}
89100
}
90101

102+
if opts.DoConfirm {
103+
var confirmed bool
104+
p := &survey.Confirm{
105+
Message: fmt.Sprintf("Rename %s to %s?", ghrepo.FullName(currRepo), newRepoName),
106+
Default: false,
107+
}
108+
err = prompt.SurveyAskOne(p, &confirmed)
109+
if err != nil {
110+
return fmt.Errorf("failed to prompt: %w", err)
111+
}
112+
if !confirmed {
113+
return nil
114+
}
115+
}
116+
91117
err = runRename(httpClient, currRepo, newRepoName)
92118
if err != nil {
93119
return fmt.Errorf("API called failed: %s", err)

pkg/cmd/repo/rename/rename_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,35 @@ func TestNewCmdRename(t *testing.T) {
3232
input: "",
3333
errMsg: "new name argument required when not running interactively\n",
3434
wantErr: true,
35-
tty: false,
3635
},
3736
{
38-
name: "one argument",
37+
name: "one argument no tty confirmed",
38+
input: "REPO --confirm",
39+
output: RenameOptions{
40+
newRepoSelector: "REPO",
41+
},
42+
},
43+
{
44+
name: "one argument no tty",
45+
input: "REPO",
46+
errMsg: "--confirm required when passing a single argument",
47+
wantErr: true,
48+
},
49+
{
50+
name: "one argument tty confirmed",
51+
input: "REPO --confirm",
52+
tty: true,
53+
output: RenameOptions{
54+
newRepoSelector: "REPO",
55+
},
56+
},
57+
{
58+
name: "one argument tty",
3959
input: "REPO",
60+
tty: true,
4061
output: RenameOptions{
4162
newRepoSelector: "REPO",
63+
DoConfirm: true,
4264
},
4365
},
4466
{
@@ -153,6 +175,39 @@ func TestRenameRun(t *testing.T) {
153175
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
154176
},
155177
},
178+
{
179+
name: "confirmation with yes",
180+
tty: true,
181+
opts: RenameOptions{
182+
newRepoSelector: "NEW_REPO",
183+
DoConfirm: true,
184+
},
185+
wantOut: "✓ Renamed repository OWNER/NEW_REPO\n✓ Updated the \"origin\" remote\n",
186+
askStubs: func(q *prompt.AskStubber) {
187+
q.StubOne(true)
188+
},
189+
httpStubs: func(reg *httpmock.Registry) {
190+
reg.Register(
191+
httpmock.REST("PATCH", "repos/OWNER/REPO"),
192+
httpmock.StatusStringResponse(204, "{}"))
193+
},
194+
execStubs: func(cs *run.CommandStubber) {
195+
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
196+
},
197+
},
198+
199+
{
200+
name: "confirmation with no",
201+
tty: true,
202+
opts: RenameOptions{
203+
newRepoSelector: "NEW_REPO",
204+
DoConfirm: true,
205+
},
206+
askStubs: func(q *prompt.AskStubber) {
207+
q.StubOne(false)
208+
},
209+
wantOut: "",
210+
},
156211
}
157212

158213
for _, tt := range testCases {

0 commit comments

Comments
 (0)