From 1ab5644d6dca2942333274f4d555b47ed85141d4 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 14 Oct 2019 19:58:34 -0700 Subject: [PATCH 1/6] Add pr show command --- command/pr.go | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/command/pr.go b/command/pr.go index b411356c7..e3d4e4da1 100644 --- a/command/pr.go +++ b/command/pr.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "strconv" "strings" "github.com/github/gh-cli/api" @@ -12,7 +13,18 @@ import ( func init() { RootCmd.AddCommand(prCmd) - prCmd.AddCommand(prListCmd) + prCmd.AddCommand( + &cobra.Command{ + Use: "list", + Short: "List pull requests", + RunE: prList, + }, + &cobra.Command{ + Use: "view", + Short: "Open the pull request in the browser", + RunE: prShow, + }, + ) } var prCmd = &cobra.Command{ @@ -26,15 +38,7 @@ work with pull requests.`, }, } -var prListCmd = &cobra.Command{ - Use: "list", - Short: "List pull requests", - RunE: func(cmd *cobra.Command, args []string) error { - return ExecutePr() - }, -} - -func ExecutePr() error { +func prList(cmd *cobra.Command, args []string) error { prPayload, err := api.PullRequests() if err != nil { return err @@ -68,6 +72,27 @@ func ExecutePr() error { return nil } +func show(cmd *cobra.Command, args []string) error { + project := project() + + var openURL string + if len(number) > 0 { + if prNumber, err := strconv.Atoi(number[0]); err == nil { + openURL = project.WebURL("", "", fmt.Sprintf("pull/%d", prNumber)) + } else { + return fmt.Errorf("invalid pull request number: '%s'", number[0]) + } + } else { + pr, err := pullRequestForCurrentBranch() + if err != nil { + return err + } + openURL = pr.HtmlUrl + } + + return openInBrowser(openURL) +} + func printPrs(prs ...api.PullRequest) { for _, pr := range prs { fmt.Printf(" #%d %s [%s]\n", pr.Number, truncateTitle(pr.Title), aurora.Cyan(pr.HeadRefName)) From 2df7c2d86f488f817e266434019f950a5e13a04d Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 14 Oct 2019 20:08:57 -0700 Subject: [PATCH 2/6] Make show work --- command/pr.go | 63 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/command/pr.go b/command/pr.go index e3d4e4da1..c7e5449ef 100644 --- a/command/pr.go +++ b/command/pr.go @@ -2,11 +2,16 @@ package command import ( "fmt" + "net/url" + "os" + "os/exec" "strconv" "strings" "github.com/github/gh-cli/api" "github.com/github/gh-cli/git" + "github.com/github/gh-cli/github" + "github.com/github/gh-cli/utils" "github.com/logrusorgru/aurora" "github.com/spf13/cobra" ) @@ -20,9 +25,9 @@ func init() { RunE: prList, }, &cobra.Command{ - Use: "view", + Use: "view [pr-number]", Short: "Open the pull request in the browser", - RunE: prShow, + RunE: prView, }, ) } @@ -33,8 +38,8 @@ var prCmd = &cobra.Command{ Long: `This command allows you to work with pull requests.`, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("pr") + RunE: func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("%+v is not a valid PR command", args) }, } @@ -72,22 +77,22 @@ func prList(cmd *cobra.Command, args []string) error { return nil } -func show(cmd *cobra.Command, args []string) error { +func prView(cmd *cobra.Command, args []string) error { project := project() var openURL string - if len(number) > 0 { - if prNumber, err := strconv.Atoi(number[0]); err == nil { + if len(args) > 0 { + if prNumber, err := strconv.Atoi(args[0]); err == nil { openURL = project.WebURL("", "", fmt.Sprintf("pull/%d", prNumber)) } else { - return fmt.Errorf("invalid pull request number: '%s'", number[0]) + return fmt.Errorf("invalid pull request number: '%s'", args[0]) } } else { - pr, err := pullRequestForCurrentBranch() + prPayload, err := api.PullRequests() if err != nil { return err } - openURL = pr.HtmlUrl + openURL = prPayload.CurrentPR.URL } return openInBrowser(openURL) @@ -116,6 +121,17 @@ func truncateTitle(title string) string { return title } +func openInBrowser(url string) error { + launcher, err := utils.BrowserLauncher() + if err != nil { + return err + } + endingArgs := append(launcher[1:], url) + return exec.Command(launcher[0], endingArgs...).Run() +} + +// The functions below should be replaced at some point by the context package +// 🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨 func currentBranch() string { currentBranch, err := git.Head() if err != nil { @@ -124,3 +140,30 @@ func currentBranch() string { return strings.Replace(currentBranch, "refs/heads/", "", 1) } + +func project() github.Project { + if repoFromEnv := os.Getenv("GH_REPO"); repoFromEnv != "" { + repoURL, err := url.Parse(fmt.Sprintf("https://github.com/%s.git", repoFromEnv)) + if err != nil { + panic(err) + } + project, err := github.NewProjectFromURL(repoURL) + if err != nil { + panic(err) + } + return *project + } + + remotes, err := github.Remotes() + if err != nil { + panic(err) + } + + for _, remote := range remotes { + if project, err := remote.Project(); err == nil { + return *project + } + } + + panic("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?") +} From ef61b495750629739b1150d10efdef65b07eb353 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 15 Oct 2019 15:01:01 -0700 Subject: [PATCH 3/6] A nice error --- command/pr.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command/pr.go b/command/pr.go index c7e5449ef..b6b6586ec 100644 --- a/command/pr.go +++ b/command/pr.go @@ -90,7 +90,8 @@ func prView(cmd *cobra.Command, args []string) error { } else { prPayload, err := api.PullRequests() if err != nil { - return err + branch := currentBranch() + return fmt.Errorf("The [%s] branch has no open PRs", branch) } openURL = prPayload.CurrentPR.URL } From f16d95e8cd57f6238fa12b897330308ebcb59f3d Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 15 Oct 2019 15:31:32 -0700 Subject: [PATCH 4/6] small tweaks --- command/pr.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/command/pr.go b/command/pr.go index b6b6586ec..b637d1e95 100644 --- a/command/pr.go +++ b/command/pr.go @@ -20,13 +20,13 @@ func init() { RootCmd.AddCommand(prCmd) prCmd.AddCommand( &cobra.Command{ - Use: "list", + Use: "list k", Short: "List pull requests", RunE: prList, }, &cobra.Command{ Use: "view [pr-number]", - Short: "Open the pull request in the browser", + Short: "Open a pull request in the browser", RunE: prView, }, ) @@ -89,7 +89,7 @@ func prView(cmd *cobra.Command, args []string) error { } } else { prPayload, err := api.PullRequests() - if err != nil { + if err != nil || prPayload.CurrentPR == nil { branch := currentBranch() return fmt.Errorf("The [%s] branch has no open PRs", branch) } From 8ad1afe2243c049c707e3a060209596ae07d1136 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 16 Oct 2019 09:38:33 -0700 Subject: [PATCH 5/6] Update command/pr.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Mislav Marohnić --- command/pr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/pr.go b/command/pr.go index b637d1e95..e1f979ca9 100644 --- a/command/pr.go +++ b/command/pr.go @@ -20,7 +20,7 @@ func init() { RootCmd.AddCommand(prCmd) prCmd.AddCommand( &cobra.Command{ - Use: "list k", + Use: "list", Short: "List pull requests", RunE: prList, }, From 354529b2df3cb249dafb059b915f505456d976cf Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 16 Oct 2019 12:39:51 -0700 Subject: [PATCH 6/6] Add some output --- command/pr.go | 1 + 1 file changed, 1 insertion(+) diff --git a/command/pr.go b/command/pr.go index e1f979ca9..035eb3646 100644 --- a/command/pr.go +++ b/command/pr.go @@ -96,6 +96,7 @@ func prView(cmd *cobra.Command, args []string) error { openURL = prPayload.CurrentPR.URL } + fmt.Printf("Opening %s in your browser.\n", openURL) return openInBrowser(openURL) }