From 5ac4b1e6d69cf1013e35a89169e62ecce9302fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 4 Dec 2019 16:16:48 +0100 Subject: [PATCH] Implement `pr create --web` --- command/pr_create.go | 22 +++++++++++++++++----- command/pr_create_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/command/pr_create.go b/command/pr_create.go index ec816658e..bd4beaab4 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -8,6 +8,7 @@ import ( "github.com/github/gh-cli/api" "github.com/github/gh-cli/context" "github.com/github/gh-cli/git" + "github.com/github/gh-cli/utils" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -29,6 +30,11 @@ func prCreate(cmd *cobra.Command, _ []string) error { cmd.Printf("Warning: %d uncommitted %s\n", ucc, noun) } + repo, err := ctx.BaseRepo() + if err != nil { + return errors.Wrap(err, "could not determine GitHub repo") + } + head, err := ctx.Branch() if err != nil { return errors.Wrap(err, "could not determine current branch") @@ -43,6 +49,16 @@ func prCreate(cmd *cobra.Command, _ []string) error { return err } + isWeb, err := cmd.Flags().GetBool("web") + if err != nil { + return errors.Wrap(err, "could not parse web") + } + if isWeb { + openURL := fmt.Sprintf(`https://github.com/%s/%s/pull/%s`, repo.RepoOwner(), repo.RepoName(), head) + cmd.Printf("Opening %s in your browser.\n", openURL) + return utils.OpenInBrowser(openURL) + } + title, err := cmd.Flags().GetString("title") if err != nil { return errors.Wrap(err, "could not parse title") @@ -87,11 +103,6 @@ func prCreate(cmd *cobra.Command, _ []string) error { return errors.Wrap(err, "could not initialize api client") } - repo, err := ctx.BaseRepo() - if err != nil { - return errors.Wrap(err, "could not determine GitHub repo") - } - isDraft, err := cmd.Flags().GetBool("draft") if err != nil { return errors.Wrap(err, "could not parse draft") @@ -158,4 +169,5 @@ func init() { "Supply a body. Will prompt for one otherwise.") prCreateCmd.Flags().StringP("base", "T", "", "The branch into which you want your code merged") + prCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create a pull request") } diff --git a/command/pr_create_test.go b/command/pr_create_test.go index af609f5a4..50e280d69 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -6,11 +6,14 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" + "strings" "testing" "github.com/github/gh-cli/context" "github.com/github/gh-cli/git" "github.com/github/gh-cli/test" + "github.com/github/gh-cli/utils" ) func TestPrCreateHelperProcess(*testing.T) { @@ -91,6 +94,36 @@ func TestPRCreate(t *testing.T) { eq(t, output, "https://github.com/OWNER/REPO/pull/12\n") } +func TestPRCreate_web(t *testing.T) { + ctx := context.NewBlank() + ctx.SetBranch("feature") + ctx.SetRemotes(map[string]string{ + "origin": "OWNER/REPO", + }) + initContext = func() context.Context { + return ctx + } + initFakeHTTP() + + ranCommands := [][]string{} + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + ranCommands = append(ranCommands, cmd.Args) + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(prCreateCmd, `pr create --web`) + eq(t, err, nil) + + if output == "" { + t.Fatal("expected output") + } + + eq(t, len(ranCommands), 3) + eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature") + eq(t, ranCommands[2][len(ranCommands[2])-1], "https://github.com/OWNER/REPO/pull/feature") +} + func TestPRCreate_ReportsUncommittedChanges(t *testing.T) { ctx := context.NewBlank() ctx.SetBranch("feature")