Skip to content

Commit

Permalink
Respect explicit title & body with issue create --web
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Feb 24, 2020
1 parent ac94ae5 commit 38b58a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
32 changes: 19 additions & 13 deletions command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {
return err
}

fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo))

baseOverride, err := cmd.Flags().GetString("repo")
if err != nil {
return err
Expand All @@ -309,16 +307,33 @@ func issueCreate(cmd *cobra.Command, args []string) error {
}
}

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)
}

if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb {
// TODO: move URL generation into GitHubRepository
openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo))
if len(templateFiles) > 1 {
if title != "" || body != "" {
openURL += fmt.Sprintf(
"?title=%s&body=%s",
url.QueryEscape(title),
url.QueryEscape(body),
)
} else if len(templateFiles) > 1 {
openURL += "/choose"
}
cmd.Printf("Opening %s in your browser.\n", openURL)
cmd.Printf("Opening %s in your browser.\n", displayURL(openURL))
return utils.OpenInBrowser(openURL)
}

fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo))

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand All @@ -334,15 +349,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {

action := SubmitAction

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)
}

interactive := title == "" || body == ""

if interactive {
Expand Down
28 changes: 27 additions & 1 deletion command/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,31 @@ func TestIssueCreate_web(t *testing.T) {
}
url := seenCmd.Args[len(seenCmd.Args)-1]
eq(t, url, "https://github.com/OWNER/REPO/issues/new")
eq(t, output.String(), "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n")
eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
eq(t, output.Stderr(), "")
}

func TestIssueCreate_webTitleBody(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
seenCmd = cmd
return &outputStub{}
})
defer restoreCmd()

output, err := RunCommand(issueCreateCmd, `issue create -w -t mytitle -b mybody`)
if err != nil {
t.Errorf("error running command `issue create`: %v", err)
}

if seenCmd == nil {
t.Fatal("expected a command to run")
}
url := seenCmd.Args[len(seenCmd.Args)-1]
eq(t, url, "https://github.com/OWNER/REPO/issues/new?title=mytitle&body=mybody")
eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
}

0 comments on commit 38b58a4

Please sign in to comment.