Add pr show command

This commit is contained in:
Corey Johnson 2019-10-14 19:58:34 -07:00
parent b4d28f11b0
commit 1ab5644d6d

View file

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