From c30b482bf0d4e275488b91bc00aca2551988caee Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 17 Mar 2020 17:03:10 -0500 Subject: [PATCH 1/3] switch defaults for pr view --- command/pr.go | 14 +++++++------- command/pr_test.go | 34 +++++++++++++++++----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/command/pr.go b/command/pr.go index e1c8e796b..104b90ffd 100644 --- a/command/pr.go +++ b/command/pr.go @@ -31,7 +31,7 @@ func init() { prListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") - prViewCmd.Flags().BoolP("preview", "p", false, "Display preview of pull request content") + prViewCmd.Flags().BoolP("web", "w", false, "Open pull request in browser") } var prCmd = &cobra.Command{ @@ -262,7 +262,7 @@ func prView(cmd *cobra.Command, args []string) error { return err } - preview, err := cmd.Flags().GetBool("preview") + web, err := cmd.Flags().GetBool("web") if err != nil { return err } @@ -283,7 +283,7 @@ func prView(cmd *cobra.Command, args []string) error { if prNumber > 0 { openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(baseRepo), prNumber) - if preview { + if !web { pr, err = api.PullRequestByNumber(apiClient, baseRepo, prNumber) if err != nil { return err @@ -299,12 +299,12 @@ func prView(cmd *cobra.Command, args []string) error { } } - if preview { - out := colorableOut(cmd) - return printPrPreview(out, pr) - } else { + if web { fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL) return utils.OpenInBrowser(openURL) + } else { + out := colorableOut(cmd) + return printPrPreview(out, pr) } } diff --git a/command/pr_test.go b/command/pr_test.go index 3fd4d9e9b..96dd14da0 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -391,7 +391,7 @@ func TestPRView_preview(t *testing.T) { defer jsonFile.Close() http.StubResponse(200, jsonFile) - output, err := RunCommand(prViewCmd, "pr view -p 12") + output, err := RunCommand(prViewCmd, "pr view 12") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -426,7 +426,7 @@ func TestPRView_previewCurrentBranch(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view -p") + output, err := RunCommand(prViewCmd, "pr view") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -461,7 +461,7 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view -p") + output, err := RunCommand(prViewCmd, "pr view") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -481,7 +481,7 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { } } -func TestPRView_currentBranch(t *testing.T) { +func TestPRView_web_currentBranch(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -502,7 +502,7 @@ func TestPRView_currentBranch(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view") + output, err := RunCommand(prViewCmd, "pr view -w") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -519,7 +519,7 @@ func TestPRView_currentBranch(t *testing.T) { } } -func TestPRView_noResultsForBranch(t *testing.T) { +func TestPRView_web_noResultsForBranch(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -540,7 +540,7 @@ func TestPRView_noResultsForBranch(t *testing.T) { }) defer restoreCmd() - _, err := RunCommand(prViewCmd, "pr view") + _, err := RunCommand(prViewCmd, "pr view -w") if err == nil || err.Error() != `no open pull requests found for branch "blueberries"` { t.Errorf("error running command `pr view`: %v", err) } @@ -550,7 +550,7 @@ func TestPRView_noResultsForBranch(t *testing.T) { } } -func TestPRView_numberArg(t *testing.T) { +func TestPRView_web_numberArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -568,7 +568,7 @@ func TestPRView_numberArg(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view 23") + output, err := RunCommand(prViewCmd, "pr view -w 23") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -582,7 +582,7 @@ func TestPRView_numberArg(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/pull/23") } -func TestPRView_numberArgWithHash(t *testing.T) { +func TestPRView_web_numberArgWithHash(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -600,7 +600,7 @@ func TestPRView_numberArgWithHash(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view \"#23\"") + output, err := RunCommand(prViewCmd, "pr view -w \"#23\"") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -614,7 +614,7 @@ func TestPRView_numberArgWithHash(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/pull/23") } -func TestPRView_urlArg(t *testing.T) { +func TestPRView_web_urlArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -632,7 +632,7 @@ func TestPRView_urlArg(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view https://github.com/OWNER/REPO/pull/23/files") + output, err := RunCommand(prViewCmd, "pr view -w https://github.com/OWNER/REPO/pull/23/files") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -646,7 +646,7 @@ func TestPRView_urlArg(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/pull/23") } -func TestPRView_branchArg(t *testing.T) { +func TestPRView_web_branchArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -666,7 +666,7 @@ func TestPRView_branchArg(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view blueberries") + output, err := RunCommand(prViewCmd, "pr view -w blueberries") if err != nil { t.Errorf("error running command `pr view`: %v", err) } @@ -680,7 +680,7 @@ func TestPRView_branchArg(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/pull/23") } -func TestPRView_branchWithOwnerArg(t *testing.T) { +func TestPRView_web_branchWithOwnerArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -701,7 +701,7 @@ func TestPRView_branchWithOwnerArg(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(prViewCmd, "pr view hubot:blueberries") + output, err := RunCommand(prViewCmd, "pr view -w hubot:blueberries") if err != nil { t.Errorf("error running command `pr view`: %v", err) } From 00de6b9c09a0cc52a30321fe07be7a294dc02477 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 17 Mar 2020 17:06:14 -0500 Subject: [PATCH 2/3] switch defaults for issue view --- command/issue.go | 12 ++-- command/issue_test.go | 150 +++++++++++++++++++++--------------------- 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/command/issue.go b/command/issue.go index 32858d145..92656d4f1 100644 --- a/command/issue.go +++ b/command/issue.go @@ -39,7 +39,7 @@ func init() { issueListCmd.Flags().StringP("author", "A", "", "Filter by author") issueCmd.AddCommand(issueViewCmd) - issueViewCmd.Flags().BoolP("preview", "p", false, "Display preview of issue content") + issueViewCmd.Flags().BoolP("web", "w", false, "Open issue in browser") } var issueCmd = &cobra.Command{ @@ -233,17 +233,17 @@ func issueView(cmd *cobra.Command, args []string) error { } openURL := issue.URL - preview, err := cmd.Flags().GetBool("preview") + web, err := cmd.Flags().GetBool("web") if err != nil { return err } - if preview { - out := colorableOut(cmd) - return printIssuePreview(out, issue) - } else { + if web { fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL) return utils.OpenInBrowser(openURL) + } else { + out := colorableOut(cmd) + return printIssuePreview(out, issue) } } diff --git a/command/issue_test.go b/command/issue_test.go index 3fb359693..10b4abdd9 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -212,79 +212,79 @@ func TestIssueList_disabledIssues(t *testing.T) { } } +func TestIssueView_web(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 &test.OutputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(issueViewCmd, "issue view -w 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_web_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 &test.OutputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(issueViewCmd, "issue view -w \"#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(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 &test.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_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 &test.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() - http.StubRepoResponse("OWNER", "REPO") - http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "hasIssuesEnabled": true, "issue": { "number": 123, @@ -305,7 +305,7 @@ func TestIssueView_preview(t *testing.T) { } } } } `)) - output, err := RunCommand(issueViewCmd, "issue view -p 123") + output, err := RunCommand(issueViewCmd, "issue view 123") if err != nil { t.Errorf("error running command `issue view`: %v", err) } @@ -326,7 +326,7 @@ func TestIssueView_preview(t *testing.T) { } } -func TestIssueView_previewWithEmptyBody(t *testing.T) { +func TestIssueView_WithEmptyBody(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -351,7 +351,7 @@ func TestIssueView_previewWithEmptyBody(t *testing.T) { } } } } `)) - output, err := RunCommand(issueViewCmd, "issue view -p 123") + output, err := RunCommand(issueViewCmd, "issue view 123") if err != nil { t.Errorf("error running command `issue view`: %v", err) } @@ -371,7 +371,7 @@ func TestIssueView_previewWithEmptyBody(t *testing.T) { } } -func TestIssueView_notFound(t *testing.T) { +func TestIssueView_web_notFound(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -388,7 +388,7 @@ func TestIssueView_notFound(t *testing.T) { }) defer restoreCmd() - _, err := RunCommand(issueViewCmd, "issue view 9999") + _, err := RunCommand(issueViewCmd, "issue view -w 9999") if err == nil || err.Error() != "graphql error: 'Could not resolve to an Issue with the number of 9999.'" { t.Errorf("error running command `issue view`: %v", err) } @@ -416,7 +416,7 @@ func TestIssueView_disabledIssues(t *testing.T) { } } -func TestIssueView_urlArg(t *testing.T) { +func TestIssueView_web_urlArg(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -435,7 +435,7 @@ func TestIssueView_urlArg(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(issueViewCmd, "issue view https://github.com/OWNER/REPO/issues/123") + output, err := RunCommand(issueViewCmd, "issue view -w https://github.com/OWNER/REPO/issues/123") if err != nil { t.Errorf("error running command `issue view`: %v", err) } From e4b43b08b987cd291d662e7f9979e1c70cead962 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 17 Mar 2020 17:13:48 -0500 Subject: [PATCH 3/3] update usage text --- command/issue.go | 2 +- command/pr.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/command/issue.go b/command/issue.go index 92656d4f1..4db194278 100644 --- a/command/issue.go +++ b/command/issue.go @@ -74,7 +74,7 @@ var issueViewCmd = &cobra.Command{ } return nil }, - Short: "View an issue in the browser", + Short: "View an issue", RunE: issueView, } diff --git a/command/pr.go b/command/pr.go index 104b90ffd..e9f78c819 100644 --- a/command/pr.go +++ b/command/pr.go @@ -57,10 +57,10 @@ var prStatusCmd = &cobra.Command{ var prViewCmd = &cobra.Command{ Use: "view [{ | | }]", Short: "View a pull request in the browser", - Long: `View a pull request specified by the argument in the browser. + Long: `View a pull request specified by the argument. Without an argument, the pull request that belongs to the current -branch is opened.`, +branch is displayed.`, RunE: prView, }