-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from joaodaher/feature/new-relic-hook
NewRelic deployment hook
- Loading branch information
Showing
7 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
language: go | ||
install: true | ||
go: | ||
- 1.9.x | ||
- 1.8.x | ||
- 1.7.x | ||
- 1.6.x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package hook | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/lucasgomide/snitch/types" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type NewRelic struct { | ||
Host string | ||
ApplicationId string | ||
ApiKey string | ||
Revision string | ||
} | ||
|
||
func (s NewRelic) CallHook(deploys []types.Deploy) error { | ||
httpClient := &http.Client{ | ||
Timeout: time.Second * 10, | ||
} | ||
if err := s.createDeploy(httpClient, deploys[0]); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *NewRelic) createDeploy(httpClient *http.Client, deploy types.Deploy) error { | ||
data := []byte(` | ||
{ | ||
"deployment": { | ||
"revision": "` + s.Revision + `", | ||
"changelog": "", | ||
"description": "", | ||
"user": "` + deploy.User + `" | ||
} | ||
}`) | ||
|
||
url := s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json" | ||
req, err := http.NewRequest("POST", url, bytes.NewReader(data)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Add("X-Api-Key", s.ApiKey) | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
resp, err := httpClient.Do(req) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != 201 { | ||
return errors.New("NewRelic::CreateDeploy - response status code isn't 201") | ||
} | ||
return nil | ||
} | ||
|
||
|
||
func (s NewRelic) ValidatesFields() error { | ||
if s.Host == "" { | ||
return errors.New("Field host into NewRelic hook is required") | ||
} | ||
if s.ApplicationId == "" { | ||
return errors.New("Field application_id into NewRelic hook is required") | ||
} | ||
if s.ApiKey == "" { | ||
return errors.New("Field api_key into NewRelic hook is required") | ||
} | ||
if s.Revision == "" { | ||
return errors.New("Field revision into NewRelic hook is required") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package hook | ||
|
||
import ( | ||
"github.com/lucasgomide/snitch/types" | ||
"gopkg.in/jarcoal/httpmock.v1" | ||
"testing" | ||
) | ||
|
||
var newrelic_host = "https://api.newrelic.com" | ||
|
||
|
||
func TestNewRelicDeploySuccessful(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
var deploys []types.Deploy | ||
deploys = append(deploys, types.Deploy{"app-sample", "12345678909", "sha1", "[email protected]", "v15"}) | ||
|
||
s := NewRelic{newrelic_host, "app-id-here", "api-key-here", "revision-here"} | ||
|
||
httpmock.RegisterResponder("POST", s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json", | ||
httpmock.NewStringResponder(201, `ok`)) | ||
|
||
|
||
if err := s.CallHook(deploys); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
|
||
func TestNewRelicReturnsErrorWhenCreateDeployFails(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
var deploys []types.Deploy | ||
deploys = append(deploys, types.Deploy{"app-sample", "12345678909", "sha1", "[email protected]", "v15"}) | ||
|
||
s := NewRelic{newrelic_host, "app-id-here", "api-key-here", "revision-here"} | ||
|
||
httpmock.RegisterResponder("POST", s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json", | ||
httpmock.NewStringResponder(502, `error`)) | ||
|
||
if err := s.CallHook(deploys); err == nil { | ||
t.Error("Expected returns error, got no error") | ||
} else if err.Error() != "NewRelic::CreateDeploy - response status code isn't 201" { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
|
||
func TestNewRelicValidateFields(t *testing.T) { | ||
s := NewRelic{} | ||
|
||
if err = s.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field host into NewRelic hook is required" { | ||
t.Error("Expected error Field host into NewRelic hook is required, got", err.Error()) | ||
} | ||
s.Host = "http://abc" | ||
|
||
if err = s.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field application_id into NewRelic hook is required" { | ||
t.Error("Expected error Field application_id into NewRelic hook is required, got", err.Error()) | ||
} | ||
s.ApplicationId = "app-id-here" | ||
|
||
if err = s.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field api_key into NewRelic hook is required" { | ||
t.Error("Expected error Field api_key into NewRelic hook is required, got", err.Error()) | ||
} | ||
s.ApiKey = "api-key-here" | ||
|
||
if err = s.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field revision into NewRelic hook is required" { | ||
t.Error("Expected error Field revision into NewRelic hook is required, got", err.Error()) | ||
} | ||
s.Revision = "revision-here" | ||
|
||
if err = s.ValidatesFields(); err != nil { | ||
t.Error("Expected returns no error, got", err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters