Skip to content

Commit

Permalink
Merge pull request cli#523 from cli/title-body-web
Browse files Browse the repository at this point in the history
Respect title & body from arguments to `pr create -w`
  • Loading branch information
mislav authored Feb 20, 2020
2 parents 69304ce + 1a82e39 commit b5d0b7c
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions command/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,21 @@ func prCreate(cmd *cobra.Command, _ []string) error {
headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch)
}

compareURL := fmt.Sprintf(
"https://github.com/%s/compare/%s...%s?expand=1",
ghrepo.FullName(baseRepo),
baseBranch,
headBranchLabel,
)
title, err := cmd.Flags().GetString("title")
if err != nil {
return fmt.Errorf("could not parse title: %w", err)
}
body, err := cmd.Flags().GetString("body")
if err != nil {
return fmt.Errorf("could not parse body: %w", err)
}

isWeb, err := cmd.Flags().GetBool("web")
if err != nil {
return fmt.Errorf("could not parse web: %q", err)
}
if isWeb {
compareURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body)
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(compareURL))
return utils.OpenInBrowser(compareURL)
}
Expand All @@ -133,15 +136,6 @@ func prCreate(cmd *cobra.Command, _ []string) error {
utils.Cyan(baseBranch),
ghrepo.FullName(baseRepo))

title, err := cmd.Flags().GetString("title")
if err != nil {
return fmt.Errorf("could not parse title: %w", err)
}
body, err := cmd.Flags().GetString("body")
if err != nil {
return fmt.Errorf("could not parse body: %w", err)
}

action := SubmitAction

interactive := title == "" || body == ""
Expand Down Expand Up @@ -203,12 +197,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {

fmt.Fprintln(cmd.OutOrStdout(), pr.URL)
} else if action == PreviewAction {
openURL := fmt.Sprintf(
"%s&title=%s&body=%s",
compareURL,
url.QueryEscape(title),
url.QueryEscape(body),
)
openURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body)
// TODO could exceed max url length for explorer
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
return utils.OpenInBrowser(openURL)
Expand All @@ -220,6 +209,22 @@ func prCreate(cmd *cobra.Command, _ []string) error {

}

func generateCompareURL(r ghrepo.Interface, base, head, title, body string) string {
u := fmt.Sprintf(
"https://github.com/%s/compare/%s...%s?expand=1",
ghrepo.FullName(r),
base,
head,
)
if title != "" {
u += "&title=" + url.QueryEscape(title)
}
if body != "" {
u += "&body=" + url.QueryEscape(body)
}
return u
}

var prCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a pull request",
Expand Down

0 comments on commit b5d0b7c

Please sign in to comment.