Skip to content

Commit 8769299

Browse files
committed
titlebody prepopulation
1 parent 58f6bef commit 8769299

File tree

5 files changed

+135
-14
lines changed

5 files changed

+135
-14
lines changed

command/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
358358
interactive := title == "" || body == ""
359359

360360
if interactive {
361-
tb, err := titleBodySurvey(cmd, title, body, templateFiles)
361+
tb, err := titleBodySurvey(cmd, title, body, defaults{}, templateFiles)
362362
if err != nil {
363363
return fmt.Errorf("could not collect title and/or body: %w", err)
364364
}

command/pr_create.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,39 @@ import (
1515
"github.com/spf13/cobra"
1616
)
1717

18+
type defaults struct {
19+
Title string
20+
Body string
21+
}
22+
23+
func computeDefaults(baseRef, headRef string) (defaults, error) {
24+
commits, err := git.Commits(baseRef, headRef)
25+
if err != nil {
26+
return defaults{}, err
27+
}
28+
29+
out := defaults{}
30+
31+
if len(commits) == 1 {
32+
out.Title = commits[0].Title
33+
body, err := git.CommitBody(commits[0].Sha)
34+
if err != nil {
35+
return defaults{}, err
36+
}
37+
out.Body = body
38+
} else {
39+
out.Title = headRef // TODO format or something?
40+
41+
body := fmt.Sprintf("---\n%d commits:\n\n", len(commits))
42+
for _, c := range commits {
43+
body += fmt.Sprintf("- %s %s\n", c.Sha[0:5], c.Title)
44+
}
45+
out.Body = body
46+
}
47+
48+
return out, nil
49+
}
50+
1851
func prCreate(cmd *cobra.Command, _ []string) error {
1952
ctx := contextForCommand(cmd)
2053
remotes, err := ctx.Remotes()
@@ -68,6 +101,11 @@ func prCreate(cmd *cobra.Command, _ []string) error {
68101
return fmt.Errorf("could not parse body: %w", err)
69102
}
70103

104+
defs, err := computeDefaults(baseBranch, headBranch)
105+
if err != nil {
106+
return fmt.Errorf("could not compute title or body defaults: %w", err)
107+
}
108+
71109
isWeb, err := cmd.Flags().GetBool("web")
72110
if err != nil {
73111
return fmt.Errorf("could not parse web: %q", err)
@@ -91,7 +129,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
91129
templateFiles = githubtemplate.Find(rootDir, "PULL_REQUEST_TEMPLATE")
92130
}
93131

94-
tb, err := titleBodySurvey(cmd, title, body, templateFiles)
132+
tb, err := titleBodySurvey(cmd, title, body, defs, templateFiles)
95133
if err != nil {
96134
return fmt.Errorf("could not collect title and/or body: %w", err)
97135
}

command/pr_create_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func TestPrCreateHelperProcess(*testing.T) {
2323

2424
args := test.GetTestHelperProcessArgs()
2525
switch args[1] {
26+
case "log":
27+
fmt.Println("123,cool,\"\"")
2628
case "status":
2729
switch args[0] {
2830
case "clean":
@@ -100,9 +102,9 @@ func TestPRCreate_web(t *testing.T) {
100102
eq(t, output.String(), "")
101103
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n")
102104

103-
eq(t, len(ranCommands), 3)
105+
eq(t, len(ranCommands), 4)
104106
eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature")
105-
eq(t, ranCommands[2][len(ranCommands[2])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
107+
eq(t, ranCommands[3][len(ranCommands[3])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
106108
}
107109

108110
func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
@@ -214,3 +216,31 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
214216

215217
// goal: only care that gql is formatted properly
216218
}
219+
220+
/*
221+
We aren't testing the survey code paths /at all/.
222+
223+
so if we want to test those code paths, some cases:
224+
225+
- user supplies no -t/-b and wants to preview in browser
226+
- user supplies no -t/-b and wants to submit directly
227+
- user supplies no -t/-b and wants to edit the title
228+
- user supplies no -t/-b and wants to edit the body
229+
230+
for defaults:
231+
232+
- one commit
233+
- multiple commits
234+
235+
checking that defaults are generated appropriately each time.
236+
237+
it seems that each survey prompt needs to be an injectable hook.
238+
*/
239+
240+
func PRCreate_survey_preview_defaults(t *testing.T) {
241+
// there are going to be calls to:
242+
// - git status
243+
// - git push
244+
// - git rev-parse
245+
// - git log
246+
}

command/title_body_survey.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
CancelAction
2424
)
2525

26-
func confirm() (Action, error) {
26+
var ConfirmSubmission = func() (Action, error) {
2727
confirmAnswers := struct {
2828
Confirmation int
2929
}{}
@@ -49,7 +49,7 @@ func confirm() (Action, error) {
4949
return Action(confirmAnswers.Confirmation), nil
5050
}
5151

52-
func selectTemplate(templatePaths []string) (string, error) {
52+
var SelectTemplate = func(templatePaths []string) (string, error) {
5353
templateResponse := struct {
5454
Index int
5555
}{}
@@ -77,17 +77,26 @@ func selectTemplate(templatePaths []string) (string, error) {
7777
return string(templateContents), nil
7878
}
7979

80-
func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody string, templatePaths []string) (*titleBody, error) {
80+
var SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
81+
return survey.Ask(qs, response, opts...)
82+
}
83+
84+
func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) {
8185
var inProgress titleBody
86+
inProgress.Title = defs.Title
8287
templateContents := ""
8388

84-
if providedBody == "" && len(templatePaths) > 0 {
85-
var err error
86-
templateContents, err = selectTemplate(templatePaths)
87-
if err != nil {
88-
return nil, err
89+
if providedBody == "" {
90+
if len(templatePaths) > 0 {
91+
var err error
92+
templateContents, err = SelectTemplate(templatePaths)
93+
if err != nil {
94+
return nil, err
95+
}
96+
inProgress.Body = templateContents
97+
} else {
98+
inProgress.Body = defs.Body
8999
}
90-
inProgress.Body = templateContents
91100
}
92101

93102
titleQuestion := &survey.Question{
@@ -127,7 +136,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody stri
127136
inProgress.Body = templateContents
128137
}
129138

130-
confirmA, err := confirm()
139+
confirmA, err := ConfirmSubmission()
131140
if err != nil {
132141
return nil, fmt.Errorf("unable to confirm: %w", err)
133142
}

git/git.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,50 @@ func UncommittedChangeCount() (int, error) {
7171
return count, nil
7272
}
7373

74+
type Commit struct {
75+
Sha string
76+
Title string
77+
}
78+
79+
func Commits(baseRef, headRef string) ([]*Commit, error) {
80+
logCmd := GitCommand(
81+
"log", "--pretty=format:%H,%s",
82+
fmt.Sprintf("%s..%s", baseRef, headRef))
83+
output, err := utils.PrepareCmd(logCmd).Output()
84+
if err != nil {
85+
return []*Commit{}, err
86+
}
87+
88+
commits := []*Commit{}
89+
sha := 0
90+
title := 1
91+
for _, line := range outputLines(output) {
92+
split := strings.SplitN(line, ",", 2)
93+
if len(split) != 2 {
94+
continue
95+
}
96+
commits = append(commits, &Commit{
97+
Sha: split[sha],
98+
Title: split[title],
99+
})
100+
}
101+
102+
if len(commits) == 0 {
103+
return commits, fmt.Errorf("could not find any commits between %s and %s", baseRef, headRef)
104+
}
105+
106+
return commits, nil
107+
}
108+
109+
func CommitBody(sha string) (string, error) {
110+
showCmd := GitCommand("show", "-s", "--pretty=format:%b", sha)
111+
output, err := utils.PrepareCmd(showCmd).Output()
112+
if err != nil {
113+
return "", err
114+
}
115+
return string(output), nil
116+
}
117+
74118
// Push publishes a git ref to a remote and sets up upstream configuration
75119
func Push(remote string, ref string) error {
76120
pushCmd := GitCommand("push", "--set-upstream", remote, ref)

0 commit comments

Comments
 (0)