diff --git a/api/client.go b/api/client.go index 21cb3febc..0ff48032a 100644 --- a/api/client.go +++ b/api/client.go @@ -220,7 +220,6 @@ func (c Client) REST(hostname string, method string, p string, body io.Reader, d if err != nil { return err } - err = json.Unmarshal(b, &data) if err != nil { return err diff --git a/api/queries_repo.go b/api/queries_repo.go index 9dcc6d8af..dad4f4562 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -455,6 +455,8 @@ type RepositoryV3 struct { Owner struct { Login string } + Private bool + HTMLUrl string `json:"html_url"` Parent *RepositoryV3 hostname string } diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index b28b04899..afcc66154 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -202,7 +202,11 @@ func createRun(opts *CreateOptions) error { return err } - gt, lt := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host) + gt, lt, err := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host) + if err != nil { + return err + } + gitIgnoreTemplate = gt repoLicenseTemplate = lt } else { @@ -311,6 +315,7 @@ func createRun(opts *CreateOptions) error { if err != nil { return err } + remoteURL := ghrepo.FormatRemoteURL(repo, protocol) if inLocalRepo { @@ -354,21 +359,24 @@ func createRun(opts *CreateOptions) error { return nil } -func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string) { +func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string, error) { - var answers []string var addBoth bool var initialQs []*survey.Question - optionToSkip := &survey.Question{ - Name: "optionToSkip", + addGitIgnoreLicense := &survey.Question{ + Name: "addGitIgnoreLicense", Prompt: &survey.Confirm{ Message: "Would you like to add a .gitignore or a license?", Default: false, }, } - initialQs = append(initialQs, optionToSkip) - survey.Ask(initialQs, &addBoth) + + initialQs = append(initialQs, addGitIgnoreLicense) + err := prompt.SurveyAsk(initialQs, &addBoth) + if err != nil { + return "", "", err + } if addBoth { var addQs []*survey.Question @@ -382,7 +390,11 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s } addQs = append(addQs, gitIgnoreLicenseQuestion) - survey.Ask(addQs, &answers) + var answers []string + err = prompt.SurveyAsk(addQs, &answers) + if err != nil { + return "", "", err + } wantGitIgnore, wantLicense := false, false @@ -439,14 +451,17 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s RepoLicense string }{} - survey.Ask(qs, &templateAnswers) - return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense] + err = prompt.SurveyAsk(qs, &templateAnswers) + if err != nil { + return "", "", err + } + return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense], nil } - return "", "" + return "", "", nil } - return "", "" + return "", "", nil } func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error { diff --git a/pkg/cmd/repo/create/create_test.go b/pkg/cmd/repo/create/create_test.go index 703be2720..1e3db1a28 100644 --- a/pkg/cmd/repo/create/create_test.go +++ b/pkg/cmd/repo/create/create_test.go @@ -455,19 +455,15 @@ func TestRepoCreate_withoutNameArg(t *testing.T) { Name: "repoVisibility", Value: "PRIVATE", }, + }) + + as.Stub([]*prompt.QuestionStub{ { - Name: "optionToSkip", - Value: true, - }, - { - Name: "gitIgnoreLicense", - Value: "license", - }, - { - Name: "repoLicense", - Value: "Apache License 2.0", + Name: "addGitIgnoreLicense", + Value: false, }, }) + as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -506,3 +502,100 @@ func TestRepoCreate_withoutNameArg(t *testing.T) { t.Errorf("expected %q, got %q", "OWNERID", ownerId) } } + +func TestRepoCreate_withoutNameArgWithGitIgnoreLicense(t *testing.T) { + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) + + cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") + cs.Register(`git rev-parse --show-toplevel`, 0, "") + + 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: "addGitIgnoreLicense", + Value: true, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "gitIgnoreLicense", + Value: []string{"license"}, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoLicense", + Value: "Apache License 2.0", + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "confirmSubmit", + Value: true, + }, + }) + + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST("GET", "users/OWNER"), + httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) + reg.Register( + httpmock.REST("GET", "licenses"), + httpmock.StringResponse(`[{"key":"apache-2.0", "name":"Apache License 2.0"}]`)) + reg.Register( + httpmock.REST("POST", "user/repos"), + httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`)) + httpClient := &http.Client{Transport: reg} + + output, err := runCommand(httpClient, "", true) + 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()) + + var reqBody struct { + Name string + Visibility string + OwnerId string + LicenseTemplate string + } + + if len(reg.Requests) != 3 { + t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests)) + } + + bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body) + _ = json.Unmarshal(bodyBytes, &reqBody) + if repoName := reqBody.Name; repoName != "REPO" { + t.Errorf("expected %q, got %q", "REPO", repoName) + } + if repoVisibility := reqBody.Visibility; repoVisibility != "PRIVATE" { + t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) + } + if ownerId := reqBody.OwnerId; ownerId != "OWNERID" { + t.Errorf("expected %q, got %q", "OWNERID", ownerId) + } +} diff --git a/pkg/cmd/repo/create/http.go b/pkg/cmd/repo/create/http.go index 230634d9e..f76678a48 100644 --- a/pkg/cmd/repo/create/http.go +++ b/pkg/cmd/repo/create/http.go @@ -120,8 +120,7 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem if err != nil { return nil, err } - fmt.Println(&responseV3, "==========response v3===========") - return &responseV3, nil + return api.InitRepoV3Hostname(&responseV3, hostname), nil } err := apiClient.GraphQL(hostname, `