diff --git a/command/issue.go b/command/issue.go index 1565b776c..146e2bb0e 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..f5f8854f2 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -243,6 +243,40 @@ func TestIssueView(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/issues/123") } +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": { + "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 221ef0bc4..a318154be 100644 --- a/command/pr.go +++ b/command/pr.go @@ -333,7 +333,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..efa894802 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -472,6 +472,38 @@ 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.StubRepoResponse("OWNER", "REPO") + + 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()