Skip to content

Commit 458d5cb

Browse files
committed
Respect interactive repo name input on create
1 parent e930122 commit 458d5cb

File tree

2 files changed

+108
-20
lines changed

2 files changed

+108
-20
lines changed

pkg/cmd/repo/create/create.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,17 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
111111

112112
func createRun(opts *CreateOptions) error {
113113
projectDir, projectDirErr := git.ToplevelDir()
114-
115-
var repoToCreate ghrepo.Interface
116-
117114
isNameAnArg := false
118115
isDescEmpty := opts.Description == ""
119116
isVisibilityPassed := false
120117

121118
if opts.Name != "" {
122119
isNameAnArg = true
123-
if strings.Contains(opts.Name, "/") {
124-
var err error
125-
repoToCreate, err = ghrepo.FromFullName(opts.Name)
126-
if err != nil {
127-
return fmt.Errorf("argument error: %w", err)
128-
}
129-
} else {
130-
repoToCreate = ghrepo.New("", opts.Name)
131-
}
132120
} else {
133121
if projectDirErr != nil {
134122
return projectDirErr
135123
}
136-
repoToCreate = ghrepo.New("", path.Base(projectDir))
124+
opts.Name = path.Base(projectDir)
137125
}
138126

139127
enabledFlagCount := 0
@@ -159,7 +147,7 @@ func createRun(opts *CreateOptions) error {
159147

160148
// Trigger interactive prompt if name is not passed
161149
if !isNameAnArg {
162-
newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, repoToCreate.RepoName())
150+
newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, opts.Name)
163151
if err != nil {
164152
return err
165153
}
@@ -183,6 +171,18 @@ func createRun(opts *CreateOptions) error {
183171
}
184172
}
185173

174+
var repoToCreate ghrepo.Interface
175+
176+
if strings.Contains(opts.Name, "/") {
177+
var err error
178+
repoToCreate, err = ghrepo.FromFullName(opts.Name)
179+
if err != nil {
180+
return fmt.Errorf("argument error: %w", err)
181+
}
182+
} else {
183+
repoToCreate = ghrepo.New("", opts.Name)
184+
}
185+
186186
// Find template repo ID
187187
if opts.Template != "" {
188188
httpClient, err := opts.HttpClient()
@@ -303,6 +303,7 @@ func createRun(opts *CreateOptions) error {
303303
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", utils.GreenCheck(), path)
304304
}
305305
}
306+
306307
return nil
307308
}
308309
fmt.Fprintln(opts.IO.Out, "Discarding...")
@@ -312,14 +313,14 @@ func createRun(opts *CreateOptions) error {
312313
func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) {
313314
qs := []*survey.Question{}
314315

315-
repoOwnerQuestion := &survey.Question{
316-
Name: "repoOwner",
316+
repoNameQuestion := &survey.Question{
317+
Name: "repoName",
317318
Prompt: &survey.Input{
318319
Message: "Repository name",
319320
Default: repoName,
320321
},
321322
}
322-
qs = append(qs, repoOwnerQuestion)
323+
qs = append(qs, repoNameQuestion)
323324

324325
if isDescEmpty {
325326
repoDescriptionQuestion := &survey.Question{
@@ -344,7 +345,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
344345
}
345346

346347
answers := struct {
347-
RepoOwner string
348+
RepoName string
348349
RepoDescription string
349350
RepoVisibility string
350351
}{}
@@ -355,8 +356,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
355356
return "", "", "", err
356357
}
357358

358-
return answers.RepoOwner, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
359-
359+
return answers.RepoName, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
360360
}
361361

362362
func confirmSubmission(repoName string, repoOwner string, isConfirmFlagPassed *bool) (bool, error) {

pkg/cmd/repo/create/create_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,91 @@ func TestRepoCreate_template(t *testing.T) {
389389
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
390390
}
391391
}
392+
393+
func TestRepoCreate_withoutNameArg(t *testing.T) {
394+
reg := &httpmock.Registry{}
395+
reg.Register(
396+
httpmock.REST("GET", "users/OWNER"),
397+
httpmock.StringResponse(`{ "node_id": "OWNERID" }`))
398+
reg.Register(
399+
httpmock.GraphQL(`mutation RepositoryCreate\b`),
400+
httpmock.StringResponse(`
401+
{ "data": { "createRepository": {
402+
"repository": {
403+
"id": "REPOID",
404+
"url": "https://github.com/OWNER/REPO",
405+
"name": "REPO",
406+
"owner": {
407+
"login": "OWNER"
408+
}
409+
}
410+
} } }`))
411+
httpClient := &http.Client{Transport: reg}
412+
413+
var seenCmd *exec.Cmd
414+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
415+
seenCmd = cmd
416+
return &test.OutputStub{}
417+
})
418+
defer restoreCmd()
419+
420+
as, surveyTearDown := prompt.InitAskStubber()
421+
defer surveyTearDown()
422+
423+
as.Stub([]*prompt.QuestionStub{
424+
{
425+
Name: "repoName",
426+
Value: "OWNER/REPO",
427+
},
428+
{
429+
Name: "repoDescription",
430+
Value: "DESCRIPTION",
431+
},
432+
{
433+
Name: "repoVisibility",
434+
Value: "PRIVATE",
435+
},
436+
})
437+
as.Stub([]*prompt.QuestionStub{
438+
{
439+
Name: "confirmSubmit",
440+
Value: true,
441+
},
442+
})
443+
444+
output, err := runCommand(httpClient, "")
445+
if err != nil {
446+
t.Errorf("error running command `repo create`: %v", err)
447+
}
448+
449+
assert.Equal(t, "", output.String())
450+
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
451+
452+
if seenCmd == nil {
453+
t.Fatal("expected a command to run")
454+
}
455+
assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " "))
456+
457+
var reqBody struct {
458+
Query string
459+
Variables struct {
460+
Input map[string]interface{}
461+
}
462+
}
463+
464+
if len(reg.Requests) != 2 {
465+
t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests))
466+
}
467+
468+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
469+
_ = json.Unmarshal(bodyBytes, &reqBody)
470+
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {
471+
t.Errorf("expected %q, got %q", "REPO", repoName)
472+
}
473+
if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" {
474+
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
475+
}
476+
if ownerId := reqBody.Variables.Input["ownerId"].(string); ownerId != "OWNERID" {
477+
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
478+
}
479+
}

0 commit comments

Comments
 (0)