Skip to content

Commit b03eb53

Browse files
committed
Adding Rollbar hook
readme: describing rollbar hook options
1 parent cd23ca5 commit b03eb53

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ Here is all avaliables hook's configurations and your descriptions. Remember tha
5454
- **host** Tell to snith your sentry host (e.g http://sentry.io or http://sentry.self.hosted)
5555
- **organization_slug** The organization slug is a unique ID used to identify your organization. (You'll find it at your sentry's configuration, probably)
5656
- **project_slug** The Project Slug is a unique ID used to identify your project (You'll find it at your project config)
57-
- **auth_token** The Auth Token to use the Sentry Web API. You can find [here](https://docs.sentry.io/api/auth/#auth-tokens)
57+
- **auth_token** The Auth Token to use the Sentry Web API. You can find more [here](https://docs.sentry.io/api/auth/#auth-tokens)
58+
- **env** The application's environment variable (e.g development, production)
59+
60+
- Rollbar
61+
- **access_token** The access token with `post_server_item` scope. You can find more [here](https://rollbar.com/docs/api/#authentication)
5862
- **env** The application's environment variable (e.g development, production)
5963

6064
## Example

hook/hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func Execute(h types.Hook, t types.Tsuru) {
2424
h = &Slack{}
2525
case "Sentry":
2626
h = &Sentry{}
27+
case "Rollbar":
28+
h = &Rollbar{}
2729
default:
2830
continue
2931
}

hook/rollbar.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package hook
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"github.com/lucasgomide/snitch/types"
7+
"net/http"
8+
)
9+
10+
type Rollbar struct {
11+
AccessToken string
12+
Env string
13+
}
14+
15+
func (r Rollbar) CallHook(deploys []types.Deploy) error {
16+
data := []byte(
17+
`{
18+
"access_token": "` + r.AccessToken + `",
19+
"environment": "` + r.Env + `",
20+
"revision": "` + deploys[0].Commit + `",
21+
"local_username": "` + deploys[0].User + `"
22+
}
23+
`)
24+
25+
resp, err := http.Post("https://api.rollbar.com/api/1/deploy/", "application/json", bytes.NewReader(data))
26+
27+
if err != nil {
28+
return err
29+
}
30+
31+
if resp.StatusCode != 200 {
32+
return errors.New("Rollbar - response status code isn't 200")
33+
}
34+
return nil
35+
}
36+
37+
func (r Rollbar) ValidatesFields() error {
38+
if r.AccessToken == "" {
39+
return errors.New("Field access_token into Rollbar hook is required")
40+
}
41+
42+
if r.Env == "" {
43+
return errors.New("Field env into Rollbar hook is required")
44+
}
45+
46+
return nil
47+
}

hook/rollbar_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package hook
2+
3+
import (
4+
"github.com/lucasgomide/snitch/types"
5+
"gopkg.in/jarcoal/httpmock.v1"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestCreateDeploySuccessfulOnRollbar(t *testing.T) {
11+
r := Rollbar{"abc", "development"}
12+
13+
var (
14+
deploys []types.Deploy
15+
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/"
16+
)
17+
18+
deploys = append(deploys, types.Deploy{"", "", "sha1", "user@g.com", ""})
19+
20+
httpmock.Activate()
21+
defer httpmock.DeactivateAndReset()
22+
23+
httpmock.RegisterResponder("POST", urlRollbarApi,
24+
httpmock.NewStringResponder(200, `ok`))
25+
26+
if err := r.CallHook(deploys); err != nil {
27+
t.Error(err)
28+
}
29+
}
30+
31+
func TestReturnsErrorWhenCreateDeployFailsOnRollbar(t *testing.T) {
32+
r := Rollbar{"abc", "development"}
33+
var deploys []types.Deploy
34+
deploys = append(deploys, types.Deploy{"", "", "sha1", "user@g.com", ""})
35+
36+
httpmock.Activate()
37+
defer httpmock.DeactivateAndReset()
38+
39+
httpmock.RegisterNoResponder(nil)
40+
41+
if err := r.CallHook(deploys); err == nil {
42+
t.Error("Expected returns error, got no error")
43+
} else if !strings.Contains(err.Error(), "no responder") {
44+
t.Error("Expected that the returns contain: no responder, got", err.Error())
45+
}
46+
}
47+
48+
func TestReturnsErrorWhenRequestToCreateDeployIsnt200OnRollbar(t *testing.T) {
49+
r := Rollbar{"abc123", "development"}
50+
var (
51+
deploys []types.Deploy
52+
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/"
53+
)
54+
deploys = append(deploys, types.Deploy{"", "", "sha1", "user@g.com", ""})
55+
56+
httpmock.Activate()
57+
defer httpmock.DeactivateAndReset()
58+
59+
httpmock.RegisterResponder("POST", urlRollbarApi,
60+
httpmock.NewStringResponder(501, `error`))
61+
expected := "Rollbar - response status code isn't 200"
62+
if err := r.CallHook(deploys); err == nil {
63+
t.Error("Expected returns error, got no error")
64+
} else if !strings.Contains(err.Error(), expected) {
65+
t.Errorf("Expected that the returns contain: %s, got %s", expected, err.Error())
66+
}
67+
}
68+
69+
func TestValidateFieldsOnRollbar(t *testing.T) {
70+
r := Rollbar{}
71+
var err error
72+
if err = r.ValidatesFields(); err == nil {
73+
t.Error("Expected returns error, got nil error")
74+
} else if err.Error() != "Field access_token into Rollbar hook is required" {
75+
t.Error("Expected error Field access_token into Rollbar hook is required, got", err.Error())
76+
}
77+
78+
r.AccessToken = "abc123"
79+
if err = r.ValidatesFields(); err == nil {
80+
t.Error("Expected returns error, got nil error")
81+
} else if err.Error() != "Field env into Rollbar hook is required" {
82+
t.Error("Expected error Field env into Rollbar hook is required, got", err.Error())
83+
}
84+
85+
r.Env = "dev"
86+
if err = r.ValidatesFields(); err != nil {
87+
t.Error("Expected returns no error, got", err.Error())
88+
}
89+
}

0 commit comments

Comments
 (0)