Implement pr create --web

This commit is contained in:
Mislav Marohnić 2019-12-04 16:16:48 +01:00
parent 5d644d2468
commit 5ac4b1e6d6
2 changed files with 50 additions and 5 deletions

View file

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

View file

@ -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")