From ac94ae587202f72578291b3e19a027a4e4efb9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 24 Feb 2020 13:36:34 +0100 Subject: [PATCH 1/3] Return interface from `determineBaseRepo()`, not pointer to interface It's sufficient to return a value of type `ghrepo.Interface` instead of a pointer to an interface. This avoids having to use `*` whenever we are passing the result of `determineBaseRepo()` into another function that accepts a `ghrepo.Interface`. --- command/issue.go | 20 ++++++++++---------- command/pr.go | 18 +++++++++--------- command/root.go | 7 +++---- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/command/issue.go b/command/issue.go index 3ba00ad4c..0fb1dc3b1 100644 --- a/command/issue.go +++ b/command/issue.go @@ -109,9 +109,9 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(baseRepo)) - issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit) + issues, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit) if err != nil { return err } @@ -169,7 +169,7 @@ func issueStatus(cmd *cobra.Command, args []string) error { return err } - issuePayload, err := api.IssueStatus(apiClient, *baseRepo, currentUser) + issuePayload, err := api.IssueStatus(apiClient, baseRepo, currentUser) if err != nil { return err } @@ -177,7 +177,7 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) fmt.Fprintln(out, "") - fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Issues assigned to you") @@ -221,7 +221,7 @@ func issueView(cmd *cobra.Command, args []string) error { return err } - issue, err := issueFromArg(apiClient, *baseRepo, args[0]) + issue, err := issueFromArg(apiClient, baseRepo, args[0]) if err != nil { return err } @@ -294,7 +294,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo)) baseOverride, err := cmd.Flags().GetString("repo") if err != nil { @@ -311,7 +311,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { 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)) + openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo)) if len(templateFiles) > 1 { openURL += "/choose" } @@ -324,12 +324,12 @@ func issueCreate(cmd *cobra.Command, args []string) error { return err } - repo, err := api.GitHubRepo(apiClient, *baseRepo) + repo, err := api.GitHubRepo(apiClient, baseRepo) if err != nil { return err } if !repo.HasIssuesEnabled { - return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(*baseRepo)) + return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(baseRepo)) } action := SubmitAction @@ -370,7 +370,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { if action == PreviewAction { openURL := fmt.Sprintf( "https://github.com/%s/issues/new/?title=%s&body=%s", - ghrepo.FullName(*baseRepo), + ghrepo.FullName(baseRepo), url.QueryEscape(title), url.QueryEscape(body), ) diff --git a/command/pr.go b/command/pr.go index 9d3e2487e..cd767f13d 100644 --- a/command/pr.go +++ b/command/pr.go @@ -85,7 +85,7 @@ func prStatus(cmd *cobra.Command, args []string) error { return err } - prPayload, err := api.PullRequests(apiClient, *baseRepo, currentPRNumber, currentPRHeadRef, currentUser) + prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser) if err != nil { return err } @@ -93,7 +93,7 @@ func prStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) fmt.Fprintln(out, "") - fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Current branch") @@ -136,7 +136,7 @@ func prList(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nPull requests for %s\n\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(colorableErr(cmd), "\nPull requests for %s\n\n", ghrepo.FullName(baseRepo)) limit, err := cmd.Flags().GetInt("limit") if err != nil { @@ -174,8 +174,8 @@ func prList(cmd *cobra.Command, args []string) error { } params := map[string]interface{}{ - "owner": (*baseRepo).RepoOwner(), - "repo": (*baseRepo).RepoName(), + "owner": baseRepo.RepoOwner(), + "repo": baseRepo.RepoName(), "state": graphqlState, } if len(labels) > 0 { @@ -261,7 +261,7 @@ func prView(cmd *cobra.Command, args []string) error { var openURL string var pr *api.PullRequest if len(args) > 0 { - pr, err = prFromArg(apiClient, *baseRepo, args[0]) + pr, err = prFromArg(apiClient, baseRepo, args[0]) if err != nil { return err } @@ -273,15 +273,15 @@ func prView(cmd *cobra.Command, args []string) error { } if prNumber > 0 { - openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(*baseRepo), prNumber) + openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(baseRepo), prNumber) if preview { - pr, err = api.PullRequestByNumber(apiClient, *baseRepo, prNumber) + pr, err = api.PullRequestByNumber(apiClient, baseRepo, prNumber) if err != nil { return err } } } else { - pr, err = api.PullRequestForBranch(apiClient, *baseRepo, branchWithOwner) + pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner) if err != nil { return err } diff --git a/command/root.go b/command/root.go index 0257b6a48..5130f74c9 100644 --- a/command/root.go +++ b/command/root.go @@ -171,7 +171,7 @@ func changelogURL(version string) string { return url } -func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (*ghrepo.Interface, error) { +func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) { apiClient, err := apiClientForContext(ctx) if err != nil { return nil, err @@ -192,11 +192,10 @@ func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (*ghrepo.Interfa return nil, err } - var baseRepo ghrepo.Interface - baseRepo, err = repoContext.BaseRepo() + baseRepo, err := repoContext.BaseRepo() if err != nil { return nil, err } - return &baseRepo, nil + return baseRepo, nil } From 38b58a491420e240c621ba8da68874c0e8f46e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 24 Feb 2020 13:35:36 +0100 Subject: [PATCH 2/3] Respect explicit title & body with `issue create --web` --- command/issue.go | 32 +++++++++++++++++++------------- command/issue_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/command/issue.go b/command/issue.go index 0fb1dc3b1..686baaa99 100644 --- a/command/issue.go +++ b/command/issue.go @@ -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 @@ -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 @@ -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 { diff --git a/command/issue_test.go b/command/issue_test.go index c09de2c62..41aedb178 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -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") } From f6eb710462e9a920b5e4e0c543626686da6e4646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 24 Feb 2020 13:53:37 +0100 Subject: [PATCH 3/3] Fix test expectation on Windows On Windows, `&` characters in URLs need to be escaped with `^`, but that messes up the test expectation for other platforms, so this normalizes it. --- command/issue_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command/issue_test.go b/command/issue_test.go index 41aedb178..f8a5d6ad9 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "regexp" + "strings" "testing" "github.com/cli/cli/utils" @@ -516,7 +517,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) { if seenCmd == nil { t.Fatal("expected a command to run") } - url := seenCmd.Args[len(seenCmd.Args)-1] + url := strings.ReplaceAll(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") }