Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98a2281

Browse files
committed
more nuanced error handling
1 parent af46fcb commit 98a2281

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

command/pr_create.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
101101
return fmt.Errorf("could not parse body: %w", err)
102102
}
103103

104-
defs, err := computeDefaults(baseBranch, headBranch)
105-
if err != nil {
106-
fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %w\n", utils.Yellow("!"), err)
107-
}
104+
defs, defaultsErr := computeDefaults(baseBranch, headBranch)
108105

109106
isWeb, err := cmd.Flags().GetBool("web")
110107
if err != nil {
@@ -119,7 +116,13 @@ func prCreate(cmd *cobra.Command, _ []string) error {
119116
action := SubmitAction
120117
if isWeb {
121118
action = PreviewAction
119+
if (title == "" || body == "") && defaultsErr != nil {
120+
return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr)
121+
}
122122
} else if autofill {
123+
if defaultsErr != nil {
124+
return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr)
125+
}
123126
action = SubmitAction
124127
title = defs.Title
125128
body = defs.Body
@@ -128,6 +131,9 @@ func prCreate(cmd *cobra.Command, _ []string) error {
128131
utils.Cyan(headBranch),
129132
utils.Cyan(baseBranch),
130133
ghrepo.FullName(baseRepo))
134+
if (title == "" || body == "") && defaultsErr != nil {
135+
fmt.Fprintf(colorableErr(cmd), "%s warning: could not compute title or body defaults: %s\n", utils.Yellow("!"), defaultsErr)
136+
}
131137
}
132138

133139
// TODO: only drop into interactive mode if stdin & stdout are a tty

command/pr_create_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,81 @@ func TestPRCreate_survey_autofill(t *testing.T) {
370370

371371
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
372372
}
373+
374+
func TestPRCreate_defaults_error_autofill(t *testing.T) {
375+
initBlankContext("OWNER/REPO", "feature")
376+
http := initFakeHTTP()
377+
http.StubRepoResponse("OWNER", "REPO")
378+
379+
cs, cmdTeardown := initCmdStubber()
380+
defer cmdTeardown()
381+
382+
cs.Stub("") // git status
383+
cs.Stub("") // git log
384+
385+
_, err := RunCommand(prCreateCmd, "pr create -f")
386+
387+
eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature")
388+
}
389+
390+
func TestPRCreate_defaults_error_web(t *testing.T) {
391+
initBlankContext("OWNER/REPO", "feature")
392+
http := initFakeHTTP()
393+
http.StubRepoResponse("OWNER", "REPO")
394+
395+
cs, cmdTeardown := initCmdStubber()
396+
defer cmdTeardown()
397+
398+
cs.Stub("") // git status
399+
cs.Stub("") // git log
400+
401+
_, err := RunCommand(prCreateCmd, "pr create -w")
402+
403+
eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature")
404+
}
405+
406+
func TestPRCreate_defaults_error_interactive(t *testing.T) {
407+
initBlankContext("OWNER/REPO", "feature")
408+
http := initFakeHTTP()
409+
http.StubRepoResponse("OWNER", "REPO")
410+
http.StubResponse(200, bytes.NewBufferString(`
411+
{ "data": { "createPullRequest": { "pullRequest": {
412+
"URL": "https://github.com/OWNER/REPO/pull/12"
413+
} } } }
414+
`))
415+
416+
cs, cmdTeardown := initCmdStubber()
417+
defer cmdTeardown()
418+
419+
cs.Stub("") // git status
420+
cs.Stub("") // git log
421+
cs.Stub("") // git rev-parse
422+
cs.Stub("") // git push
423+
cs.Stub("") // browser open
424+
425+
as, surveyTeardown := initAskStubber()
426+
defer surveyTeardown()
427+
428+
as.Stub([]*QuestionStub{
429+
&QuestionStub{
430+
Name: "title",
431+
Default: true,
432+
},
433+
&QuestionStub{
434+
Name: "body",
435+
Value: "social distancing",
436+
},
437+
})
438+
as.Stub([]*QuestionStub{
439+
&QuestionStub{
440+
Name: "confirmation",
441+
Value: 0,
442+
},
443+
})
444+
445+
output, err := RunCommand(prCreateCmd, `pr create`)
446+
eq(t, err, nil)
447+
448+
stderr := string(output.Stderr())
449+
eq(t, strings.Contains(stderr, "warning: could not compute title or body defaults: could not find any commits"), true)
450+
}

0 commit comments

Comments
 (0)