From a7426780ae1c01a72d0c4b884ef08cf0f0f934c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 14 Jun 2019 16:32:58 +0200 Subject: [PATCH] [pr show] Add ability to open a pull request based on number --- commands/pr.go | 42 ++++++++++++++++++++++++++++------------ features/pr-show.feature | 12 ++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/commands/pr.go b/commands/pr.go index acc29235..e39a5ff0 100644 --- a/commands/pr.go +++ b/commands/pr.go @@ -17,6 +17,7 @@ var ( pr list [-s ] [-h ] [-b ] [-o [-^]] [-f ] [-L ] pr checkout [] pr show [-uc] [-h ] +pr show [-uc] `, Long: `Manage GitHub Pull Requests for the current repository. @@ -29,7 +30,7 @@ pr show [-uc] [-h ] Check out the head of a pull request in a new branch. * _show_: - Open the GitHub pull request page in a web browser. + Open a pull request page in a web browser. ## Options: @@ -283,6 +284,27 @@ func showPr(command *Command, args *Args) { baseProject, err := localRepo.MainProject() utils.Check(err) + words := args.Words() + openUrl := "" + if len(words) > 0 { + if prNumber, err := strconv.Atoi(words[0]); err == nil { + openUrl = baseProject.WebURL("", "", fmt.Sprintf("pull/%d", prNumber)) + } else { + utils.Check(fmt.Errorf("invalid pull request number: '%s'", words[0])) + } + } else { + pr, err := findCurrentPullRequest(localRepo, baseProject, args.Flag.Value("--head")) + utils.Check(err) + openUrl = pr.HtmlUrl + } + + args.NoForward() + printUrl := args.Flag.Bool("--url") + copyUrl := args.Flag.Bool("--copy") + printBrowseOrCopy(args, openUrl, !printUrl && !copyUrl, copyUrl) +} + +func findCurrentPullRequest(localRepo *github.GitHubRepo, baseProject *github.Project, headArg string) (*github.PullRequest, error) { host, err := github.CurrentConfig().PromptForHost(baseProject.Host) utils.Check(err) gh := github.NewClientWithHost(host) @@ -292,8 +314,8 @@ func showPr(command *Command, args *Args) { } headWithOwner := "" - if args.Flag.HasReceived("--head") { - headWithOwner = args.Flag.Value("--head") + if headArg != "" { + headWithOwner = headArg if !strings.Contains(headWithOwner, ":") { headWithOwner = fmt.Sprintf("%s:%s", baseProject.Owner, headWithOwner) } @@ -311,16 +333,12 @@ func showPr(command *Command, args *Args) { filterParams["head"] = headWithOwner pulls, err := gh.FetchPullRequests(baseProject, filterParams, 1, nil) - utils.Check(err) - - if len(pulls) == 1 { - pr := pulls[0] - args.NoForward() - flagBrowseURLPrint := args.Flag.Bool("--url") - flagBrowseURLCopy := args.Flag.Bool("--copy") - printBrowseOrCopy(args, pr.HtmlUrl, !flagBrowseURLPrint && !flagBrowseURLCopy, flagBrowseURLCopy) + if err != nil { + return nil, err + } else if len(pulls) == 1 { + return &pulls[0], nil } else { - utils.Check(fmt.Errorf("no open pull requests found for branch '%s'", headWithOwner)) + return nil, fmt.Errorf("no open pull requests found for branch '%s'", headWithOwner) } } diff --git a/features/pr-show.feature b/features/pr-show.feature index bf2d217b..682e2bc6 100644 --- a/features/pr-show.feature +++ b/features/pr-show.feature @@ -94,3 +94,15 @@ Feature: hub pr show """ no open pull requests found for branch 'ashemesh:topic'\n """ + + Scenario: Show pull request by number + When I successfully run `hub pr show 102` + Then "open https://github.com/ashemesh/hub/pull/102" should be run + + Scenario: Show pull request by invalid number + When I run `hub pr show XYZ` + Then the exit status should be 1 + And the stderr should contain exactly: + """ + invalid pull request number: 'XYZ'\n + """ -- GitLab