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