From 8f9574be35219917eacce8e266d0fbab529337eb Mon Sep 17 00:00:00 2001 From: Anowar Islam Date: Thu, 13 Feb 2020 01:43:51 -0800 Subject: [PATCH 1/2] added fix for empty body in pr preview --- command/pr.go | 8 +++-- command/pr_test.go | 34 +++++++++++++++++++++ test/fixtures/prView_EmptyBody.json | 46 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/prView_EmptyBody.json diff --git a/command/pr.go b/command/pr.go index 039b8c37a..d1cb7e5be 100644 --- a/command/pr.go +++ b/command/pr.go @@ -308,9 +308,11 @@ func printPrPreview(out io.Writer, pr *api.PullRequest) { pr.BaseRefName, pr.HeadRefName, ))) - fmt.Fprintln(out) - fmt.Fprintln(out, utils.RenderMarkdown(pr.Body)) - fmt.Fprintln(out) + if pr.Body != "" { + fmt.Fprintln(out) + fmt.Fprintln(out, utils.RenderMarkdown(pr.Body)) + fmt.Fprintln(out) + } fmt.Fprintf(out, utils.Gray("View this pull request on GitHub: %s\n"), pr.URL) } diff --git a/command/pr_test.go b/command/pr_test.go index f0069371f..d621d447b 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -313,6 +313,40 @@ func TestPRView_previewCurrentBranch(t *testing.T) { } } +func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { + initBlankContext("OWNER/REPO", "blueberries") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prView_EmptyBody.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(prViewCmd, "pr view -p") + if err != nil { + t.Errorf("error running command `pr view`: %v", err) + } + + eq(t, output.Stderr(), "") + + expectedLines := []*regexp.Regexp{ + regexp.MustCompile(`Blueberries are a good fruit`), + regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`), + regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`), + } + for _, r := range expectedLines { + if !r.MatchString(output.String()) { + t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) + return + } + } +} + func TestPRView_currentBranch(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() diff --git a/test/fixtures/prView_EmptyBody.json b/test/fixtures/prView_EmptyBody.json new file mode 100644 index 000000000..651f49ce0 --- /dev/null +++ b/test/fixtures/prView_EmptyBody.json @@ -0,0 +1,46 @@ +{ + "data": { + "repository": { + "pullRequests": { + "nodes": [ + { + "number": 12, + "title": "Blueberries are from a fork", + "body": "yeah", + "url": "https://github.com/OWNER/REPO/pull/12", + "headRefName": "blueberries", + "baseRefName": "master", + "headRepositoryOwner": { + "login": "hubot" + }, + "commits": { + "totalCount": 12 + }, + "author": { + "login": "nobody" + }, + "isCrossRepository": true + }, + { + "number": 10, + "title": "Blueberries are a good fruit", + "body": "", + "url": "https://github.com/OWNER/REPO/pull/10", + "baseRefName": "master", + "headRefName": "blueberries", + "author": { + "login": "nobody" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "commits": { + "totalCount": 8 + }, + "isCrossRepository": false + } + ] + } + } + } +} From 2c2c8a999148a402ce4c69ee4313b25ce885b2ad Mon Sep 17 00:00:00 2001 From: Anowar Islam Date: Thu, 13 Feb 2020 01:44:03 -0800 Subject: [PATCH 2/2] added fix for empty body in issue preview --- command/issue.go | 8 +++++--- command/issue_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/command/issue.go b/command/issue.go index 4641d91c8..6caf81c12 100644 --- a/command/issue.go +++ b/command/issue.go @@ -255,9 +255,11 @@ func printIssuePreview(out io.Writer, issue *api.Issue) { utils.Pluralize(issue.Comments.TotalCount, "comment"), coloredLabels, ))) - fmt.Fprintln(out) - fmt.Fprintln(out, utils.RenderMarkdown(issue.Body)) - fmt.Fprintln(out) + if issue.Body != "" { + fmt.Fprintln(out) + fmt.Fprintln(out, utils.RenderMarkdown(issue.Body)) + fmt.Fprintln(out) + } fmt.Fprintf(out, utils.Gray("View this issue on GitHub: %s\n"), issue.URL) } diff --git a/command/issue_test.go b/command/issue_test.go index edce87fb6..c09de2c62 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -288,6 +288,51 @@ func TestIssueView_preview(t *testing.T) { } } +func TestIssueView_previewWithEmptyBody(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, + "body": "", + "title": "ix of coins", + "author": { + "login": "marseilles" + }, + "labels": { + "nodes": [ + {"name": "tarot"} + ] + }, + "comments": { + "totalCount": 9 + }, + "url": "https://github.com/OWNER/REPO/issues/123" + } } } } + `)) + + output, err := RunCommand(issueViewCmd, "issue view -p 123") + if err != nil { + t.Errorf("error running command `issue view`: %v", err) + } + + eq(t, output.Stderr(), "") + + expectedLines := []*regexp.Regexp{ + regexp.MustCompile(`ix of coins`), + regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`), + regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`), + } + for _, r := range expectedLines { + if !r.MatchString(output.String()) { + t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) + return + } + } +} + func TestIssueView_notFound(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP()