-
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.
Showing
4 changed files
with
143 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
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,47 @@ | ||
package hook | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/lucasgomide/snitch/types" | ||
"net/http" | ||
) | ||
|
||
type Rollbar struct { | ||
AccessToken string | ||
Env string | ||
} | ||
|
||
func (r Rollbar) CallHook(deploys []types.Deploy) error { | ||
data := []byte( | ||
`{ | ||
"access_token": "` + r.AccessToken + `", | ||
"environment": "` + r.Env + `", | ||
"revision": "` + deploys[0].Commit + `", | ||
"local_username": "` + deploys[0].User + `" | ||
} | ||
`) | ||
|
||
resp, err := http.Post("https://api.rollbar.com/api/1/deploy/", "application/json", bytes.NewReader(data)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return errors.New("Rollbar - response status code isn't 200") | ||
} | ||
return nil | ||
} | ||
|
||
func (r Rollbar) ValidatesFields() error { | ||
if r.AccessToken == "" { | ||
return errors.New("Field access_token into Rollbar hook is required") | ||
} | ||
|
||
if r.Env == "" { | ||
return errors.New("Field env into Rollbar 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,89 @@ | ||
package hook | ||
|
||
import ( | ||
"github.com/lucasgomide/snitch/types" | ||
"gopkg.in/jarcoal/httpmock.v1" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCreateDeploySuccessfulOnRollbar(t *testing.T) { | ||
r := Rollbar{"abc", "development"} | ||
|
||
var ( | ||
deploys []types.Deploy | ||
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/" | ||
) | ||
|
||
deploys = append(deploys, types.Deploy{"", "", "sha1", "[email protected]", ""}) | ||
|
||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("POST", urlRollbarApi, | ||
httpmock.NewStringResponder(200, `ok`)) | ||
|
||
if err := r.CallHook(deploys); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestReturnsErrorWhenCreateDeployFailsOnRollbar(t *testing.T) { | ||
r := Rollbar{"abc", "development"} | ||
var deploys []types.Deploy | ||
deploys = append(deploys, types.Deploy{"", "", "sha1", "[email protected]", ""}) | ||
|
||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterNoResponder(nil) | ||
|
||
if err := r.CallHook(deploys); err == nil { | ||
t.Error("Expected returns error, got no error") | ||
} else if !strings.Contains(err.Error(), "no responder") { | ||
t.Error("Expected that the returns contain: no responder, got", err.Error()) | ||
} | ||
} | ||
|
||
func TestReturnsErrorWhenRequestToCreateDeployIsnt200OnRollbar(t *testing.T) { | ||
r := Rollbar{"abc123", "development"} | ||
var ( | ||
deploys []types.Deploy | ||
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/" | ||
) | ||
deploys = append(deploys, types.Deploy{"", "", "sha1", "[email protected]", ""}) | ||
|
||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("POST", urlRollbarApi, | ||
httpmock.NewStringResponder(501, `error`)) | ||
expected := "Rollbar - response status code isn't 200" | ||
if err := r.CallHook(deploys); err == nil { | ||
t.Error("Expected returns error, got no error") | ||
} else if !strings.Contains(err.Error(), expected) { | ||
t.Errorf("Expected that the returns contain: %s, got %s", expected, err.Error()) | ||
} | ||
} | ||
|
||
func TestValidateFieldsOnRollbar(t *testing.T) { | ||
r := Rollbar{} | ||
var err error | ||
if err = r.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field access_token into Rollbar hook is required" { | ||
t.Error("Expected error Field access_token into Rollbar hook is required, got", err.Error()) | ||
} | ||
|
||
r.AccessToken = "abc123" | ||
if err = r.ValidatesFields(); err == nil { | ||
t.Error("Expected returns error, got nil error") | ||
} else if err.Error() != "Field env into Rollbar hook is required" { | ||
t.Error("Expected error Field env into Rollbar hook is required, got", err.Error()) | ||
} | ||
|
||
r.Env = "dev" | ||
if err = r.ValidatesFields(); err != nil { | ||
t.Error("Expected returns no error, got", err.Error()) | ||
} | ||
} |