Dont execute jq filters or templates when api requests are not successful (#5088)

This commit is contained in:
Sam Coe 2022-01-29 09:32:01 +02:00 committed by GitHub
parent 22c5269606
commit e68aa12564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -366,13 +366,13 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream
responseBody = io.TeeReader(responseBody, bodyCopy)
}
if opts.FilterOutput != "" {
if opts.FilterOutput != "" && serverError == "" {
// TODO: reuse parsed query across pagination invocations
err = export.FilterJSON(opts.IO.Out, responseBody, opts.FilterOutput)
if err != nil {
return
}
} else if opts.Template != "" {
} else if opts.Template != "" && serverError == "" {
// TODO: reuse parsed template across pagination invocations
err = template.Execute(responseBody)
if err != nil {

View file

@ -490,6 +490,20 @@ func Test_apiRun(t *testing.T) {
stdout: "not a cat",
stderr: ``,
},
{
name: "output template when REST error",
options: ApiOptions{
Template: `{{.status}}`,
},
httpResponse: &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)),
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
},
err: cmdutil.SilentError,
stdout: `{"message": "THIS IS FINE"}`,
stderr: "gh: THIS IS FINE (HTTP 400)\n",
},
{
name: "jq filter",
options: ApiOptions{
@ -504,6 +518,20 @@ func Test_apiRun(t *testing.T) {
stdout: "Mona\nHubot\n",
stderr: ``,
},
{
name: "jq filter when REST error",
options: ApiOptions{
FilterOutput: `.[].name`,
},
httpResponse: &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)),
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
},
err: cmdutil.SilentError,
stdout: `{"message": "THIS IS FINE"}`,
stderr: "gh: THIS IS FINE (HTTP 400)\n",
},
}
for _, tt := range tests {