Skip to content

Commit 17b58bf

Browse files
committed
fix repo create --confirm
Respect the --confirm flag when deciding whether to prompt for gitignore and license creation during `repo create` Fixes cli#3989
1 parent 161de77 commit 17b58bf

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

pkg/cmd/repo/create/create.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,23 @@ func createRun(opts *CreateOptions) error {
221221
return err
222222
}
223223

224-
// GitIgnore and License templates not added when a template repository is passed.
225-
if gitIgnoreTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
226-
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
227-
if err != nil {
228-
return err
224+
// GitIgnore and License templates not added when a template repository
225+
// is passed, or when the confirm flag is set.
226+
if opts.Template == "" && opts.IO.CanPrompt() && !opts.ConfirmSubmit {
227+
if gitIgnoreTemplate == "" {
228+
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
229+
if err != nil {
230+
return err
231+
}
232+
gitIgnoreTemplate = gt
229233
}
230-
gitIgnoreTemplate = gt
231-
}
232-
233-
if repoLicenseTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
234-
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
235-
if err != nil {
236-
return err
234+
if repoLicenseTemplate == "" {
235+
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
236+
if err != nil {
237+
return err
238+
}
239+
repoLicenseTemplate = lt
237240
}
238-
repoLicenseTemplate = lt
239241
}
240242
}
241243

pkg/cmd/repo/create/create_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,64 @@ func TestRepoCreate_WithGitIgnore_Org(t *testing.T) {
817817
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
818818
}
819819
}
820+
821+
func TestRepoCreate_WithConfirmFlag(t *testing.T) {
822+
cs, cmdTeardown := run.Stub()
823+
defer cmdTeardown(t)
824+
825+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
826+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
827+
828+
reg := &httpmock.Registry{}
829+
830+
reg.Register(
831+
httpmock.GraphQL(`mutation RepositoryCreate\b`),
832+
httpmock.StringResponse(`
833+
{ "data": { "createRepository": {
834+
"repository": {
835+
"id": "REPOID",
836+
"url": "https://github.com/OWNER/REPO",
837+
"name": "REPO",
838+
"owner": {
839+
"login": "OWNER"
840+
}
841+
}
842+
} } }`),
843+
)
844+
845+
reg.Register(
846+
httpmock.REST("GET", "users/OWNER"),
847+
httpmock.StringResponse(`{ "node_id": "OWNERID" }`),
848+
)
849+
850+
httpClient := &http.Client{Transport: reg}
851+
852+
in := "OWNER/REPO --confirm --private"
853+
output, err := runCommand(httpClient, in, true)
854+
if err != nil {
855+
t.Errorf("error running command `repo create %v`: %v", in, err)
856+
}
857+
858+
assert.Equal(t, "", output.String())
859+
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
860+
861+
var reqBody struct {
862+
Query string
863+
Variables struct {
864+
Input map[string]interface{}
865+
}
866+
}
867+
868+
if len(reg.Requests) != 2 {
869+
t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests))
870+
}
871+
872+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
873+
_ = json.Unmarshal(bodyBytes, &reqBody)
874+
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {
875+
t.Errorf("expected %q, got %q", "REPO", repoName)
876+
}
877+
if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" {
878+
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
879+
}
880+
}

0 commit comments

Comments
 (0)