fix repo create --confirm

Respect the --confirm flag when deciding whether to prompt for gitignore
and license creation during `repo create`

Fixes #3989
This commit is contained in:
Des Preston 2021-07-14 09:57:59 -04:00
parent 161de77fd7
commit 17b58bf0b2
2 changed files with 76 additions and 13 deletions

View file

@ -221,21 +221,23 @@ func createRun(opts *CreateOptions) error {
return err
}
// GitIgnore and License templates not added when a template repository is passed.
if gitIgnoreTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
if err != nil {
return err
// GitIgnore and License templates not added when a template repository
// is passed, or when the confirm flag is set.
if opts.Template == "" && opts.IO.CanPrompt() && !opts.ConfirmSubmit {
if gitIgnoreTemplate == "" {
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
if err != nil {
return err
}
gitIgnoreTemplate = gt
}
gitIgnoreTemplate = gt
}
if repoLicenseTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
if err != nil {
return err
if repoLicenseTemplate == "" {
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
if err != nil {
return err
}
repoLicenseTemplate = lt
}
repoLicenseTemplate = lt
}
}

View file

@ -817,3 +817,64 @@ func TestRepoCreate_WithGitIgnore_Org(t *testing.T) {
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
}
}
func TestRepoCreate_WithConfirmFlag(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, "")
reg := &httpmock.Registry{}
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"
}
}
} } }`),
)
reg.Register(
httpmock.REST("GET", "users/OWNER"),
httpmock.StringResponse(`{ "node_id": "OWNERID" }`),
)
httpClient := &http.Client{Transport: reg}
in := "OWNER/REPO --confirm --private"
output, err := runCommand(httpClient, in, true)
if err != nil {
t.Errorf("error running command `repo create %v`: %v", in, 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 {
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)
}
}