From 3468a4652144aced277772e02c24ddb6ad2d0ab4 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 15 Jan 2020 11:27:12 -0600 Subject: [PATCH] support preview in browser for issue create --- command/issue.go | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/command/issue.go b/command/issue.go index 6a5820309..6abd2714b 100644 --- a/command/issue.go +++ b/command/issue.go @@ -3,6 +3,7 @@ package command import ( "fmt" "io" + "net/url" "regexp" "strconv" "strings" @@ -308,6 +309,8 @@ func issueCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("the '%s/%s' repository has disabled issues", baseRepo.RepoOwner(), baseRepo.RepoName()) } + action := SubmitAction + title, err := cmd.Flags().GetString("title") if err != nil { return errors.Wrap(err, "could not parse title") @@ -320,13 +323,16 @@ func issueCreate(cmd *cobra.Command, args []string) error { interactive := title == "" || body == "" if interactive { + // TODO handle tb.Action tb, err := titleBodySurvey(cmd, title, body, templateFiles) if err != nil { return errors.Wrap(err, "could not collect title and/or body") } - if tb == nil { - // editing was canceled, we can just leave + action = tb.Action + + if tb.Action == CancelAction { + // TODO print something return nil } @@ -337,17 +343,34 @@ func issueCreate(cmd *cobra.Command, args []string) error { body = tb.Body } } - params := map[string]interface{}{ - "title": title, - "body": body, + + if action == PreviewAction { + openURL := fmt.Sprintf( + "https://github.com/%s/%s/issues/new/?title=%s&body=%s", + baseRepo.RepoOwner(), + baseRepo.RepoName(), + url.QueryEscape(title), + url.QueryEscape(body), + ) + // TODO could exceed max url length for explorer + fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL) + return utils.OpenInBrowser(openURL) + } else if action == SubmitAction { + params := map[string]interface{}{ + "title": title, + "body": body, + } + + newIssue, err := api.IssueCreate(apiClient, repo, params) + if err != nil { + return err + } + + fmt.Fprintln(cmd.OutOrStdout(), newIssue.URL) + } else { + panic("Unreachable state") } - newIssue, err := api.IssueCreate(apiClient, repo, params) - if err != nil { - return err - } - - fmt.Fprintln(cmd.OutOrStdout(), newIssue.URL) return nil }