Skip to content

Commit 3e26616

Browse files
authored
Merge pull request #11 from lucasgomide/add-hangout-chat
Add hook: HangoutsChat
2 parents f3aff32 + d851600 commit 3e26616

File tree

6 files changed

+112
-2
lines changed

6 files changed

+112
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Here is all avaliables hook's configurations and your descriptions. Remember tha
6969
- **api_key** The API Key to use the NewRelic REST API. You can find more [here](https://docs.newrelic.com/docs/apis/rest-api-v2/getting-started/api-keys)
7070
- **revision** The application's current revision (e.g 0.0.1r42)
7171

72+
- HangoutsChat
73+
- **webhook_url** Indicates the Webhook URL to dispatch messages to HangoutsChat Room.
74+
7275
## Example
7376

7477
[Snitch App Sample](https://github.com/lucasgomide/snitch-app-example)

hook/hangouts_chat.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package hook
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"net/http"
7+
8+
"github.com/lucasgomide/snitch/types"
9+
)
10+
11+
type HangoutsChat struct {
12+
WebhookUrl string
13+
}
14+
15+
func (s HangoutsChat) CallHook(deploy []types.Deploy) error {
16+
message := `"The application *` + deploy[0].App + `* has been deployed just now by ` + deploy[0].User + ` at _` + deploy[0].ConvertTimestampToRFC822() + `_"`
17+
18+
data := []byte(`{"text":` + message + `}`)
19+
resp, err := http.Post(s.WebhookUrl, "application/json", bytes.NewReader(data))
20+
if err != nil {
21+
return err
22+
}
23+
24+
if resp.StatusCode != 200 {
25+
return errors.New(`HangoutsChat - response status code is ` + resp.Status)
26+
}
27+
return nil
28+
}
29+
30+
func (s HangoutsChat) ValidatesFields() error {
31+
return nil
32+
}

hook/hangouts_chat_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package hook
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lucasgomide/snitch/types"
7+
"gopkg.in/jarcoal/httpmock.v1"
8+
)
9+
10+
var hangoutWebhookUrl = "https://hangouts.chat/123"
11+
12+
func TestHangoutWhenNotificatedSuccessful(t *testing.T) {
13+
httpmock.Activate()
14+
defer httpmock.DeactivateAndReset()
15+
16+
httpmock.RegisterResponder("POST", hangoutWebhookUrl,
17+
httpmock.NewStringResponder(200, `ok`))
18+
19+
hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
20+
var deploy []types.Deploy
21+
deploy = append(deploy, types.Deploy{App: "app-sample"})
22+
23+
err := hangout.CallHook(deploy)
24+
if err != nil {
25+
t.Error(err)
26+
}
27+
}
28+
29+
func TestHangoutWhenResponseStatusCodeIsnt200(t *testing.T) {
30+
httpmock.Activate()
31+
defer httpmock.DeactivateAndReset()
32+
33+
httpmock.RegisterResponder("POST", hangoutWebhookUrl,
34+
httpmock.NewStringResponder(400, `error`))
35+
36+
hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
37+
var deploy []types.Deploy
38+
deploy = append(deploy, types.Deploy{App: "app-sample"})
39+
40+
err := hangout.CallHook(deploy)
41+
expected := "HangoutsChat - response status code is 400"
42+
if err == nil || err.Error() != expected {
43+
t.Error("Expected: "+expected+", but got", err.Error())
44+
}
45+
}
46+
47+
func TestHangoutReturnsErrorWhenRequestFail(t *testing.T) {
48+
httpmock.Activate()
49+
defer httpmock.DeactivateAndReset()
50+
51+
httpmock.RegisterNoResponder(nil)
52+
53+
hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
54+
var deploy []types.Deploy
55+
deploy = append(deploy, types.Deploy{App: "app-sample"})
56+
57+
err := hangout.CallHook(deploy)
58+
if err == nil {
59+
t.Error("The request has been failed but no error was raised")
60+
}
61+
}

hook/hook.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Execute(h types.Hook, t types.Tsuru) {
2222
utils.LogError(err.Error())
2323
} else {
2424
for hookName, conf := range config.Data() {
25-
switch strings.Title(hookName.(string)) {
25+
switch defineHookName(hookName.(string)) {
2626
case "Slack":
2727
h = &Slack{}
2828
case "Sentry":
@@ -31,6 +31,8 @@ func Execute(h types.Hook, t types.Tsuru) {
3131
h = &Rollbar{}
3232
case "Newrelic":
3333
h = &NewRelic{}
34+
case "Hangouts Chat":
35+
h = &HangoutsChat{}
3436
default:
3537
continue
3638
}
@@ -74,3 +76,9 @@ func executeHook(h types.Hook, deploy []types.Deploy, conf interface{}) error {
7476

7577
return nil
7678
}
79+
80+
func defineHookName(name string) string {
81+
return strings.Title(
82+
strings.Join(strings.Split(name, "_"), " "),
83+
)
84+
}

hook/hook_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func TestShouldExecuteHooksFromConfig(t *testing.T) {
9191
httpmock.RegisterResponder("POST", "http://dummy.sample",
9292
httpmock.NewStringResponder(200, `ok`))
9393

94+
httpmock.RegisterResponder("POST", "http://hangouts.chat.sample",
95+
httpmock.NewStringResponder(200, `ok`))
96+
9497
httpmock.RegisterResponder("POST", "https://api.rollbar.com/api/1/deploy/",
9598
httpmock.NewStringResponder(200, `ok`))
9699

@@ -116,6 +119,7 @@ func TestShouldExecuteHooksFromConfig(t *testing.T) {
116119

117120
if msg != "" {
118121
t.Error("Expected that msg is not empty, got empty msg")
122+
t.Error(msg)
119123
}
120124
}
121125

testdata/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
slack:
22
webhook_url: http://dummy.sample
3+
hangouts_chat:
4+
webhook_url: http://hangouts.chat.sample
35
missing_hook:
46
field: value
57
rollbar:
@@ -16,4 +18,4 @@ newrelic:
1618
host: https://api.newrelic.com
1719
application_id: "01234"
1820
api_key: 0a0b11223344
19-
revision: 0.0.1
21+
revision: 0.0.1

0 commit comments

Comments
 (0)