From 038e5e534221e6d9d1ad7c9b76bc389b45bb4671 Mon Sep 17 00:00:00 2001 From: Myk Date: Thu, 6 Feb 2020 17:39:05 +1100 Subject: [PATCH 1/2] Accept `#` syntax as issue/PR number --- command/issue.go | 2 +- command/issue_test.go | 33 +++++++++++++++++++++++++++++++++ command/pr.go | 2 +- command/pr_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/command/issue.go b/command/issue.go index 686baaa99..40be99a13 100644 --- a/command/issue.go +++ b/command/issue.go @@ -273,7 +273,7 @@ func printIssuePreview(out io.Writer, issue *api.Issue) error { var issueURLRE = regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/issues/(\d+)`) func issueFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*api.Issue, error) { - if issueNumber, err := strconv.Atoi(arg); err == nil { + if issueNumber, err := strconv.Atoi(strings.TrimPrefix(arg, "#")); err == nil { return api.IssueByNumber(apiClient, baseRepo, issueNumber) } diff --git a/command/issue_test.go b/command/issue_test.go index f8a5d6ad9..414dc0cf7 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -243,6 +243,39 @@ func TestIssueView(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/issues/123") } +func TestIssueViewWithHash(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { "hasIssuesEnabled": true, "issue": { + "number": 123, + "url": "https://github.com/OWNER/REPO/issues/123" + } } } } + `)) + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(issueViewCmd, "issue view \"#123\"") + if err != nil { + t.Errorf("error running command `issue view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/OWNER/REPO/issues/123") +} + func TestIssueView_preview(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() diff --git a/command/pr.go b/command/pr.go index cd767f13d..e9db521f6 100644 --- a/command/pr.go +++ b/command/pr.go @@ -325,7 +325,7 @@ func printPrPreview(out io.Writer, pr *api.PullRequest) error { var prURLRE = regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`) func prFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*api.PullRequest, error) { - if prNumber, err := strconv.Atoi(arg); err == nil { + if prNumber, err := strconv.Atoi(strings.TrimPrefix(arg, "#")); err == nil { return api.PullRequestByNumber(apiClient, baseRepo, prNumber) } diff --git a/command/pr_test.go b/command/pr_test.go index 25fc8b34d..58a872406 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -472,6 +472,37 @@ func TestPRView_numberArg(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/pull/23") } +func TestPRView_numberArgWithHash(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { "pullRequest": { + "url": "https://github.com/OWNER/REPO/pull/23" + } } } } + `)) + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(prViewCmd, "pr view \"#23\"") + if err != nil { + t.Errorf("error running command `pr view`: %v", err) + } + + eq(t, output.String(), "") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/OWNER/REPO/pull/23") +} + func TestPRView_urlArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() From f5b0354ae3bc5bf62aa6a9308a0cea2f12653bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 27 Feb 2020 12:11:35 +0100 Subject: [PATCH 2/2] Fix tests --- command/issue_test.go | 3 ++- command/pr_test.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/command/issue_test.go b/command/issue_test.go index 414dc0cf7..f5f8854f2 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -243,9 +243,10 @@ func TestIssueView(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/issues/123") } -func TestIssueViewWithHash(t *testing.T) { +func TestIssueView_numberArgWithHash(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "hasIssuesEnabled": true, "issue": { diff --git a/command/pr_test.go b/command/pr_test.go index 58a872406..efa894802 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -475,6 +475,7 @@ func TestPRView_numberArg(t *testing.T) { func TestPRView_numberArgWithHash(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "pullRequest": {