diff --git a/command/pr_create.go b/command/pr_create.go index 2330386ee..441b9645d 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -101,10 +101,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse body: %w", err) } - defs, err := computeDefaults(baseBranch, headBranch) - if err != nil { - fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %w\n", utils.Yellow("!"), err) - } + defs, defaultsErr := computeDefaults(baseBranch, headBranch) isWeb, err := cmd.Flags().GetBool("web") if err != nil { @@ -119,7 +116,13 @@ func prCreate(cmd *cobra.Command, _ []string) error { action := SubmitAction if isWeb { action = PreviewAction + if (title == "" || body == "") && defaultsErr != nil { + return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr) + } } else if autofill { + if defaultsErr != nil { + return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr) + } action = SubmitAction title = defs.Title body = defs.Body @@ -128,6 +131,9 @@ func prCreate(cmd *cobra.Command, _ []string) error { utils.Cyan(headBranch), utils.Cyan(baseBranch), ghrepo.FullName(baseRepo)) + if (title == "" || body == "") && defaultsErr != nil { + fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %s\n", utils.Yellow("!"), defaultsErr) + } } // TODO: only drop into interactive mode if stdin & stdout are a tty diff --git a/command/pr_create_test.go b/command/pr_create_test.go index ba2c3a802..6fbf8d1b3 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -370,3 +370,81 @@ func TestPRCreate_survey_autofill(t *testing.T) { eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n") } + +func TestPRCreate_defaults_error_autofill(t *testing.T) { + initBlankContext("OWNER/REPO", "feature") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + cs, cmdTeardown := initCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git status + cs.Stub("") // git log + + _, err := RunCommand(prCreateCmd, "pr create -f") + + eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature") +} + +func TestPRCreate_defaults_error_web(t *testing.T) { + initBlankContext("OWNER/REPO", "feature") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + cs, cmdTeardown := initCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git status + cs.Stub("") // git log + + _, err := RunCommand(prCreateCmd, "pr create -w") + + eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature") +} + +func TestPRCreate_defaults_error_interactive(t *testing.T) { + initBlankContext("OWNER/REPO", "feature") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "createPullRequest": { "pullRequest": { + "URL": "https://github.com/OWNER/REPO/pull/12" + } } } } + `)) + + cs, cmdTeardown := initCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git status + cs.Stub("") // git log + cs.Stub("") // git rev-parse + cs.Stub("") // git push + cs.Stub("") // browser open + + as, surveyTeardown := initAskStubber() + defer surveyTeardown() + + as.Stub([]*QuestionStub{ + &QuestionStub{ + Name: "title", + Default: true, + }, + &QuestionStub{ + Name: "body", + Value: "social distancing", + }, + }) + as.Stub([]*QuestionStub{ + &QuestionStub{ + Name: "confirmation", + Value: 0, + }, + }) + + output, err := RunCommand(prCreateCmd, `pr create`) + eq(t, err, nil) + + stderr := string(output.Stderr()) + eq(t, strings.Contains(stderr, "warning: could not compute title or body defaults: could not find any commits"), true) +}