From 458d5cb42f40385cf44bfadb676901e23962bbb3 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Mon, 21 Sep 2020 11:40:44 +0200 Subject: [PATCH] Respect interactive repo name input on create --- pkg/cmd/repo/create/create.go | 40 +++++++------- pkg/cmd/repo/create/create_test.go | 88 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index fb37e27ed..77ddcaf33 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -111,29 +111,17 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co func createRun(opts *CreateOptions) error { projectDir, projectDirErr := git.ToplevelDir() - - var repoToCreate ghrepo.Interface - isNameAnArg := false isDescEmpty := opts.Description == "" isVisibilityPassed := false if opts.Name != "" { isNameAnArg = true - if strings.Contains(opts.Name, "/") { - var err error - repoToCreate, err = ghrepo.FromFullName(opts.Name) - if err != nil { - return fmt.Errorf("argument error: %w", err) - } - } else { - repoToCreate = ghrepo.New("", opts.Name) - } } else { if projectDirErr != nil { return projectDirErr } - repoToCreate = ghrepo.New("", path.Base(projectDir)) + opts.Name = path.Base(projectDir) } enabledFlagCount := 0 @@ -159,7 +147,7 @@ func createRun(opts *CreateOptions) error { // Trigger interactive prompt if name is not passed if !isNameAnArg { - newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, repoToCreate.RepoName()) + newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, opts.Name) if err != nil { return err } @@ -183,6 +171,18 @@ func createRun(opts *CreateOptions) error { } } + var repoToCreate ghrepo.Interface + + if strings.Contains(opts.Name, "/") { + var err error + repoToCreate, err = ghrepo.FromFullName(opts.Name) + if err != nil { + return fmt.Errorf("argument error: %w", err) + } + } else { + repoToCreate = ghrepo.New("", opts.Name) + } + // Find template repo ID if opts.Template != "" { httpClient, err := opts.HttpClient() @@ -303,6 +303,7 @@ func createRun(opts *CreateOptions) error { fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", utils.GreenCheck(), path) } } + return nil } fmt.Fprintln(opts.IO.Out, "Discarding...") @@ -312,14 +313,14 @@ func createRun(opts *CreateOptions) error { func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) { qs := []*survey.Question{} - repoOwnerQuestion := &survey.Question{ - Name: "repoOwner", + repoNameQuestion := &survey.Question{ + Name: "repoName", Prompt: &survey.Input{ Message: "Repository name", Default: repoName, }, } - qs = append(qs, repoOwnerQuestion) + qs = append(qs, repoNameQuestion) if isDescEmpty { repoDescriptionQuestion := &survey.Question{ @@ -344,7 +345,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s } answers := struct { - RepoOwner string + RepoName string RepoDescription string RepoVisibility string }{} @@ -355,8 +356,7 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s return "", "", "", err } - return answers.RepoOwner, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil - + return answers.RepoName, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil } func confirmSubmission(repoName string, repoOwner string, isConfirmFlagPassed *bool) (bool, error) { diff --git a/pkg/cmd/repo/create/create_test.go b/pkg/cmd/repo/create/create_test.go index 303119f36..7282b350d 100644 --- a/pkg/cmd/repo/create/create_test.go +++ b/pkg/cmd/repo/create/create_test.go @@ -389,3 +389,91 @@ func TestRepoCreate_template(t *testing.T) { t.Errorf("expected %q, got %q", "OWNERID", ownerId) } } + +func TestRepoCreate_withoutNameArg(t *testing.T) { + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST("GET", "users/OWNER"), + httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) + reg.Register( + httpmock.GraphQL(`mutation RepositoryCreate\b`), + httpmock.StringResponse(` + { "data": { "createRepository": { + "repository": { + "id": "REPOID", + "url": "https://github.com/OWNER/REPO", + "name": "REPO", + "owner": { + "login": "OWNER" + } + } + } } }`)) + httpClient := &http.Client{Transport: reg} + + var seenCmd *exec.Cmd + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { + seenCmd = cmd + return &test.OutputStub{} + }) + defer restoreCmd() + + as, surveyTearDown := prompt.InitAskStubber() + defer surveyTearDown() + + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoName", + Value: "OWNER/REPO", + }, + { + Name: "repoDescription", + Value: "DESCRIPTION", + }, + { + Name: "repoVisibility", + Value: "PRIVATE", + }, + }) + as.Stub([]*prompt.QuestionStub{ + { + Name: "confirmSubmit", + Value: true, + }, + }) + + output, err := runCommand(httpClient, "") + if err != nil { + t.Errorf("error running command `repo create`: %v", err) + } + + assert.Equal(t, "", output.String()) + assert.Equal(t, "āœ“ Created repository OWNER/REPO on GitHub\nāœ“ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " ")) + + var reqBody struct { + Query string + Variables struct { + Input map[string]interface{} + } + } + + if len(reg.Requests) != 2 { + t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests)) + } + + bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) + _ = json.Unmarshal(bodyBytes, &reqBody) + if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { + t.Errorf("expected %q, got %q", "REPO", repoName) + } + if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { + t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) + } + if ownerId := reqBody.Variables.Input["ownerId"].(string); ownerId != "OWNERID" { + t.Errorf("expected %q, got %q", "OWNERID", ownerId) + } +}