Skip to content

Commit 633c8c0

Browse files
committed
factor out title body prompting
1 parent 7bb2c38 commit 633c8c0

File tree

2 files changed

+117
-77
lines changed

2 files changed

+117
-77
lines changed

command/pr_create.go

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"runtime"
77

8-
"github.com/AlecAivazis/survey/v2"
98
"github.com/github/gh-cli/api"
109
"github.com/github/gh-cli/context"
1110
"github.com/github/gh-cli/git"
@@ -55,86 +54,25 @@ func prCreate(cmd *cobra.Command, _ []string) error {
5554

5655
interactive := title == "" || body == ""
5756

58-
inProgress := struct {
59-
Body string
60-
Title string
61-
}{}
62-
6357
if interactive {
64-
confirmed := false
65-
editor := determineEditor()
66-
67-
for !confirmed {
68-
titleQuestion := &survey.Question{
69-
Name: "title",
70-
Prompt: &survey.Input{
71-
Message: "PR Title",
72-
Default: inProgress.Title,
73-
},
74-
}
75-
bodyQuestion := &survey.Question{
76-
Name: "body",
77-
Prompt: &survey.Editor{
78-
Message: fmt.Sprintf("PR Body (%s)", editor),
79-
FileName: "*.md",
80-
Default: inProgress.Body,
81-
AppendDefault: true,
82-
Editor: editor,
83-
},
84-
}
85-
86-
qs := []*survey.Question{}
87-
if title == "" {
88-
qs = append(qs, titleQuestion)
89-
}
90-
if body == "" {
91-
qs = append(qs, bodyQuestion)
92-
}
93-
94-
err := survey.Ask(qs, &inProgress)
95-
if err != nil {
96-
return errors.Wrap(err, "could not prompt")
97-
}
98-
confirmAnswers := struct {
99-
Confirmation string
100-
}{}
101-
confirmQs := []*survey.Question{
102-
{
103-
Name: "confirmation",
104-
Prompt: &survey.Select{
105-
Message: "Submit?",
106-
Options: []string{
107-
"Yes",
108-
"Edit",
109-
"Cancel",
110-
},
111-
},
112-
},
113-
}
114-
115-
err = survey.Ask(confirmQs, &confirmAnswers)
116-
if err != nil {
117-
return errors.Wrap(err, "could not prompt")
118-
}
119-
120-
switch confirmAnswers.Confirmation {
121-
case "Yes":
122-
confirmed = true
123-
case "Edit":
124-
continue
125-
case "Cancel":
126-
cmd.Println("Discarding pull request")
127-
return nil
128-
}
58+
tb, err := titleBodySurvey(cmd, title, body)
59+
if err != nil {
60+
return errors.Wrap(err, "could not collect title and/or body")
12961
}
130-
}
13162

132-
if title == "" {
133-
title = inProgress.Title
134-
}
135-
if body == "" {
136-
body = inProgress.Body
63+
if tb == nil {
64+
// editing was canceled, we can just leave
65+
return nil
66+
}
67+
68+
if title == "" {
69+
title = tb.Title
70+
}
71+
if body == "" {
72+
body = tb.Body
73+
}
13774
}
75+
13876
base, err := cmd.Flags().GetString("base")
13977
if err != nil {
14078
return errors.Wrap(err, "could not parse base")

command/title_body_survey.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"github.com/AlecAivazis/survey/v2"
6+
"github.com/pkg/errors"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type titleBody struct {
11+
Body string
12+
Title string
13+
}
14+
15+
const _confirmed = 0
16+
const _unconfirmed = 1
17+
const _cancel = 2
18+
19+
func confirm() (int, error) {
20+
confirmAnswers := struct {
21+
Confirmation int
22+
}{}
23+
confirmQs := []*survey.Question{
24+
{
25+
Name: "confirmation",
26+
Prompt: &survey.Select{
27+
Message: "Submit?",
28+
Options: []string{
29+
"Yes",
30+
"Edit",
31+
"Cancel",
32+
},
33+
},
34+
},
35+
}
36+
37+
err := survey.Ask(confirmQs, &confirmAnswers)
38+
if err != nil {
39+
return -1, errors.Wrap(err, "could not prompt")
40+
}
41+
42+
return confirmAnswers.Confirmation, nil
43+
}
44+
45+
func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody string) (*titleBody, error) {
46+
inProgress := titleBody{}
47+
48+
confirmed := false
49+
editor := determineEditor()
50+
51+
for !confirmed {
52+
titleQuestion := &survey.Question{
53+
Name: "title",
54+
Prompt: &survey.Input{
55+
Message: "Title",
56+
Default: inProgress.Title,
57+
},
58+
}
59+
bodyQuestion := &survey.Question{
60+
Name: "body",
61+
Prompt: &survey.Editor{
62+
Message: fmt.Sprintf("Body (%s)", editor),
63+
FileName: "*.md",
64+
Default: inProgress.Body,
65+
AppendDefault: true,
66+
Editor: editor,
67+
},
68+
}
69+
70+
qs := []*survey.Question{}
71+
if providedTitle == "" {
72+
qs = append(qs, titleQuestion)
73+
}
74+
if providedBody == "" {
75+
qs = append(qs, bodyQuestion)
76+
}
77+
78+
err := survey.Ask(qs, &inProgress)
79+
if err != nil {
80+
return nil, errors.Wrap(err, "could not prompt")
81+
}
82+
83+
confirmA, err := confirm()
84+
if err != nil {
85+
return nil, errors.Wrap(err, "unable to confirm")
86+
}
87+
switch confirmA {
88+
case _confirmed:
89+
confirmed = true
90+
case _unconfirmed:
91+
continue
92+
case _cancel:
93+
cmd.Println("Discarding.")
94+
return nil, nil
95+
default:
96+
panic("reached unreachable case")
97+
}
98+
99+
}
100+
101+
return &inProgress, nil
102+
}

0 commit comments

Comments
 (0)