diff --git a/command/pr.go b/command/pr.go index a5b1f0079af..81dd0bb8018 100644 --- a/command/pr.go +++ b/command/pr.go @@ -3,6 +3,7 @@ package command import ( "fmt" "io" + "os" "regexp" "strconv" "strings" @@ -206,8 +207,13 @@ func prList(cmd *cobra.Command, args []string) error { }) title := listHeader(ghrepo.FullName(baseRepo), "pull request", len(listResult.PullRequests), listResult.TotalCount, hasFilters) - // TODO: avoid printing header if piped to a script - fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title) + + out := cmd.OutOrStdout() + if outFile, isFile := out.(*os.File); isFile { + if utils.IsTerminal(outFile) { + fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title) + } + } table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range listResult.PullRequests { diff --git a/command/pr_test.go b/command/pr_test.go index 074420debae..9d10a89eda0 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -264,7 +264,7 @@ Requesting a code review from you } } -func TestPRList(t *testing.T) { +func TestPRList_stdout_is_not_a_tty(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -278,17 +278,14 @@ func TestPRList(t *testing.T) { t.Fatal(err) } - eq(t, output.Stderr(), ` -Showing 3 of 3 pull requests in OWNER/REPO - -`) + eq(t, output.Stderr(), ``) eq(t, output.String(), `32 New feature feature 29 Fixed bad bug hubot:bug-fix 28 Improve documentation docs `) } -func TestPRList_filtering(t *testing.T) { +func TestPRList_filtering_stdout_is_not_a_tty(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") @@ -302,10 +299,7 @@ func TestPRList_filtering(t *testing.T) { } eq(t, output.String(), "") - eq(t, output.Stderr(), ` -No pull requests match your search in OWNER/REPO - -`) + eq(t, output.Stderr(), ``) bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) reqBody := struct {