titlebody prepopulation
This commit is contained in:
parent
58f6bef8a7
commit
8769299731
5 changed files with 135 additions and 14 deletions
|
|
@ -358,7 +358,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
|
|||
interactive := title == "" || body == ""
|
||||
|
||||
if interactive {
|
||||
tb, err := titleBodySurvey(cmd, title, body, templateFiles)
|
||||
tb, err := titleBodySurvey(cmd, title, body, defaults{}, templateFiles)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not collect title and/or body: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,39 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type defaults struct {
|
||||
Title string
|
||||
Body string
|
||||
}
|
||||
|
||||
func computeDefaults(baseRef, headRef string) (defaults, error) {
|
||||
commits, err := git.Commits(baseRef, headRef)
|
||||
if err != nil {
|
||||
return defaults{}, err
|
||||
}
|
||||
|
||||
out := defaults{}
|
||||
|
||||
if len(commits) == 1 {
|
||||
out.Title = commits[0].Title
|
||||
body, err := git.CommitBody(commits[0].Sha)
|
||||
if err != nil {
|
||||
return defaults{}, err
|
||||
}
|
||||
out.Body = body
|
||||
} else {
|
||||
out.Title = headRef // TODO format or something?
|
||||
|
||||
body := fmt.Sprintf("---\n%d commits:\n\n", len(commits))
|
||||
for _, c := range commits {
|
||||
body += fmt.Sprintf("- %s %s\n", c.Sha[0:5], c.Title)
|
||||
}
|
||||
out.Body = body
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func prCreate(cmd *cobra.Command, _ []string) error {
|
||||
ctx := contextForCommand(cmd)
|
||||
remotes, err := ctx.Remotes()
|
||||
|
|
@ -68,6 +101,11 @@ func prCreate(cmd *cobra.Command, _ []string) error {
|
|||
return fmt.Errorf("could not parse body: %w", err)
|
||||
}
|
||||
|
||||
defs, err := computeDefaults(baseBranch, headBranch)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not compute title or body defaults: %w", err)
|
||||
}
|
||||
|
||||
isWeb, err := cmd.Flags().GetBool("web")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse web: %q", err)
|
||||
|
|
@ -91,7 +129,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
|
|||
templateFiles = githubtemplate.Find(rootDir, "PULL_REQUEST_TEMPLATE")
|
||||
}
|
||||
|
||||
tb, err := titleBodySurvey(cmd, title, body, templateFiles)
|
||||
tb, err := titleBodySurvey(cmd, title, body, defs, templateFiles)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not collect title and/or body: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ func TestPrCreateHelperProcess(*testing.T) {
|
|||
|
||||
args := test.GetTestHelperProcessArgs()
|
||||
switch args[1] {
|
||||
case "log":
|
||||
fmt.Println("123,cool,\"\"")
|
||||
case "status":
|
||||
switch args[0] {
|
||||
case "clean":
|
||||
|
|
@ -100,9 +102,9 @@ func TestPRCreate_web(t *testing.T) {
|
|||
eq(t, output.String(), "")
|
||||
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n")
|
||||
|
||||
eq(t, len(ranCommands), 3)
|
||||
eq(t, len(ranCommands), 4)
|
||||
eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature")
|
||||
eq(t, ranCommands[2][len(ranCommands[2])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
|
||||
eq(t, ranCommands[3][len(ranCommands[3])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
|
||||
}
|
||||
|
||||
func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
|
||||
|
|
@ -214,3 +216,31 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
|
|||
|
||||
// goal: only care that gql is formatted properly
|
||||
}
|
||||
|
||||
/*
|
||||
We aren't testing the survey code paths /at all/.
|
||||
|
||||
so if we want to test those code paths, some cases:
|
||||
|
||||
- user supplies no -t/-b and wants to preview in browser
|
||||
- user supplies no -t/-b and wants to submit directly
|
||||
- user supplies no -t/-b and wants to edit the title
|
||||
- user supplies no -t/-b and wants to edit the body
|
||||
|
||||
for defaults:
|
||||
|
||||
- one commit
|
||||
- multiple commits
|
||||
|
||||
checking that defaults are generated appropriately each time.
|
||||
|
||||
it seems that each survey prompt needs to be an injectable hook.
|
||||
*/
|
||||
|
||||
func PRCreate_survey_preview_defaults(t *testing.T) {
|
||||
// there are going to be calls to:
|
||||
// - git status
|
||||
// - git push
|
||||
// - git rev-parse
|
||||
// - git log
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ const (
|
|||
CancelAction
|
||||
)
|
||||
|
||||
func confirm() (Action, error) {
|
||||
var ConfirmSubmission = func() (Action, error) {
|
||||
confirmAnswers := struct {
|
||||
Confirmation int
|
||||
}{}
|
||||
|
|
@ -49,7 +49,7 @@ func confirm() (Action, error) {
|
|||
return Action(confirmAnswers.Confirmation), nil
|
||||
}
|
||||
|
||||
func selectTemplate(templatePaths []string) (string, error) {
|
||||
var SelectTemplate = func(templatePaths []string) (string, error) {
|
||||
templateResponse := struct {
|
||||
Index int
|
||||
}{}
|
||||
|
|
@ -77,17 +77,26 @@ func selectTemplate(templatePaths []string) (string, error) {
|
|||
return string(templateContents), nil
|
||||
}
|
||||
|
||||
func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody string, templatePaths []string) (*titleBody, error) {
|
||||
var SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
|
||||
return survey.Ask(qs, response, opts...)
|
||||
}
|
||||
|
||||
func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) {
|
||||
var inProgress titleBody
|
||||
inProgress.Title = defs.Title
|
||||
templateContents := ""
|
||||
|
||||
if providedBody == "" && len(templatePaths) > 0 {
|
||||
var err error
|
||||
templateContents, err = selectTemplate(templatePaths)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if providedBody == "" {
|
||||
if len(templatePaths) > 0 {
|
||||
var err error
|
||||
templateContents, err = SelectTemplate(templatePaths)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inProgress.Body = templateContents
|
||||
} else {
|
||||
inProgress.Body = defs.Body
|
||||
}
|
||||
inProgress.Body = templateContents
|
||||
}
|
||||
|
||||
titleQuestion := &survey.Question{
|
||||
|
|
@ -127,7 +136,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody stri
|
|||
inProgress.Body = templateContents
|
||||
}
|
||||
|
||||
confirmA, err := confirm()
|
||||
confirmA, err := ConfirmSubmission()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to confirm: %w", err)
|
||||
}
|
||||
|
|
|
|||
44
git/git.go
44
git/git.go
|
|
@ -71,6 +71,50 @@ func UncommittedChangeCount() (int, error) {
|
|||
return count, nil
|
||||
}
|
||||
|
||||
type Commit struct {
|
||||
Sha string
|
||||
Title string
|
||||
}
|
||||
|
||||
func Commits(baseRef, headRef string) ([]*Commit, error) {
|
||||
logCmd := GitCommand(
|
||||
"log", "--pretty=format:%H,%s",
|
||||
fmt.Sprintf("%s..%s", baseRef, headRef))
|
||||
output, err := utils.PrepareCmd(logCmd).Output()
|
||||
if err != nil {
|
||||
return []*Commit{}, err
|
||||
}
|
||||
|
||||
commits := []*Commit{}
|
||||
sha := 0
|
||||
title := 1
|
||||
for _, line := range outputLines(output) {
|
||||
split := strings.SplitN(line, ",", 2)
|
||||
if len(split) != 2 {
|
||||
continue
|
||||
}
|
||||
commits = append(commits, &Commit{
|
||||
Sha: split[sha],
|
||||
Title: split[title],
|
||||
})
|
||||
}
|
||||
|
||||
if len(commits) == 0 {
|
||||
return commits, fmt.Errorf("could not find any commits between %s and %s", baseRef, headRef)
|
||||
}
|
||||
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
func CommitBody(sha string) (string, error) {
|
||||
showCmd := GitCommand("show", "-s", "--pretty=format:%b", sha)
|
||||
output, err := utils.PrepareCmd(showCmd).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
// Push publishes a git ref to a remote and sets up upstream configuration
|
||||
func Push(remote string, ref string) error {
|
||||
pushCmd := GitCommand("push", "--set-upstream", remote, ref)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue