From 2ec2fc37a5fdcf1c4b872f294073b17a9368e70d Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Tue, 23 Mar 2021 11:19:29 +0100 Subject: [PATCH] Fix defer execution (#1065) --- modules/circleci/client.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/circleci/client.go b/modules/circleci/client.go index 48e20a3a..be691f33 100644 --- a/modules/circleci/client.go +++ b/modules/circleci/client.go @@ -1,7 +1,9 @@ package circleci import ( + "bytes" "fmt" + "io/ioutil" "net/http" "net/url" @@ -28,7 +30,7 @@ func (client *Client) BuildsFor() ([]*Build, error) { return builds, err } - err = utils.ParseJSON(&builds, resp.Body) + err = utils.ParseJSON(&builds, bytes.NewReader(resp)) if err != nil { return builds, err } @@ -42,7 +44,7 @@ var ( circleAPIURL = &url.URL{Scheme: "https", Host: "circleci.com", Path: "/api/v1/"} ) -func (client *Client) circleRequest(path string) (*http.Response, error) { +func (client *Client) circleRequest(path string) ([]byte, error) { params := url.Values{} params.Add("circle-token", client.apiKey) @@ -60,11 +62,16 @@ func (client *Client) circleRequest(path string) (*http.Response, error) { if err != nil { return nil, err } + defer func() { _ = resp.Body.Close() }() if resp.StatusCode < 200 || resp.StatusCode > 299 { return nil, fmt.Errorf(resp.Status) } - return resp, nil + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return body, nil }