Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #86 from blablacar/newrelic
Browse files Browse the repository at this point in the history
Fix newrelic hook to create deployments
  • Loading branch information
obiesmans authored May 15, 2018
2 parents 4a16efd + a75d128 commit 68780c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
48 changes: 27 additions & 21 deletions hooks/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type NewRelicClient struct {
}

type NewRelicDeployment struct {
revision string
changelog string
description string
user string
Revision string `json:"revision"`
Changelog string `json:"changelog"`
Description string `json:"description"`
User string `json:"user"`
}

type NewRelicApplicationList struct {
Expand Down Expand Up @@ -60,9 +60,9 @@ func (c NewRelicClient) PreDeployment(userName string, env string, service strin

description := fmt.Sprintf("Deploying %s %s on %s", service, podVersion, env)
d := &NewRelicDeployment{
description: description,
revision: podVersion,
user: userName,
Description: description,
Revision: podVersion,
User: userName,
}
return c.CreateDeployment(d)
}
Expand Down Expand Up @@ -104,32 +104,34 @@ func (c NewRelicClient) findApplicationId(nameFilter string) (int, error) {

filter := strings.NewReader(fmt.Sprintf("filter[name]=%s", nameFilter))
request, err := c.NewRequest("GET", "v2/applications.json", filter)

if err != nil {
return 0, err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

response, err := c.HttpClient.Do(request)
if err != nil {
return 0, err
}
if response.StatusCode != http.StatusOK {
return 0, errors.New("HTTP error from NewRelic")
}

defer response.Body.Close()
bodyJson, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, err
}
log.WithFields(log.Fields{
"statusCode": response.StatusCode,
"body": string(bodyJson),
}).Debug("NewRelic response")

if response.StatusCode != http.StatusOK {
return 0, errors.New("HTTP error from NewRelic")
}

err = json.Unmarshal(bodyJson, &applications)
if err != nil {
return 0, err
}
log.WithFields(log.Fields{
"statusCode": response.StatusCode,
}).Debug("NewRelic response")

if len(applications.Applications) == 0 {
return 0, fmt.Errorf("application %s not found", nameFilter)
Expand All @@ -147,8 +149,6 @@ func (c NewRelicClient) NewRequest(method string, route string, body io.Reader)
}

request.Header.Add("X-Api-Key", c.ApiKey)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")

log.WithFields(log.Fields{
"url": request.URL,
Expand All @@ -157,26 +157,32 @@ func (c NewRelicClient) NewRequest(method string, route string, body io.Reader)
return request, nil
}

type NewRelicDeploymentBody struct {
Deployment *NewRelicDeployment `json:"deployment"`
}

// https://rpm.newrelic.com/api/explore/application_deployments/create
func (c NewRelicClient) CreateDeployment(d *NewRelicDeployment) error {
body := &bytes.Buffer{}
if err := json.NewEncoder(body).Encode(d); err != nil {
if err := json.NewEncoder(body).Encode(
NewRelicDeploymentBody{Deployment: d},
); err != nil {
return err
}

request, err := c.NewRequest("POST", fmt.Sprintf("v2/applications/%d/deployments.json", c.ApplicationId), body)
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")

response, err := c.HttpClient.Do(request)
if err != nil {
return err
}

log.WithFields(log.Fields{
"statusCode": response.StatusCode,
}).Debug("NewRelic response")
if err != nil {
return err
}

if response.StatusCode != http.StatusCreated {
return errors.New(fmt.Sprintf("NewRelic status code: %d", response.StatusCode))
Expand Down
6 changes: 4 additions & 2 deletions hooks/newrelic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r
switch r.RequestURI {
case "/v2/applications.json":
w.WriteHeader(http.StatusOK)
if string(body) == "filter[name]=webhook" {
if string(body) == "filter[name]=webhook" && r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
w.Write(GetApplications)
} else {
w.Write(GetApplicationsEmpty)
}

case "/v2/applications/456/deployments.json":
w.WriteHeader(http.StatusCreated)
if r.Header.Get("Content-Type") == "application/json" {
w.WriteHeader(http.StatusCreated)
}

default:
w.WriteHeader(http.StatusBadRequest)
Expand Down

0 comments on commit 68780c4

Please sign in to comment.