diff --git a/command/pr_create.go b/command/pr_create.go index cd1b2110133..64f43c8956b 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -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) } @@ -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 == "" @@ -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) @@ -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",