Skip to content

Commit

Permalink
Merge pull request #11 from lucasgomide/add-hangout-chat
Browse files Browse the repository at this point in the history
Add hook: HangoutsChat
lucasgomide authored Jul 23, 2018
2 parents f3aff32 + d851600 commit 3e26616
Showing 6 changed files with 112 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -69,6 +69,9 @@ Here is all avaliables hook's configurations and your descriptions. Remember tha
- **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)
- **revision** The application's current revision (e.g 0.0.1r42)

- HangoutsChat
- **webhook_url** Indicates the Webhook URL to dispatch messages to HangoutsChat Room.

## Example

[Snitch App Sample](https://github.com/lucasgomide/snitch-app-example)
32 changes: 32 additions & 0 deletions hook/hangouts_chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hook

import (
"bytes"
"errors"
"net/http"

"github.com/lucasgomide/snitch/types"
)

type HangoutsChat struct {
WebhookUrl string
}

func (s HangoutsChat) CallHook(deploy []types.Deploy) error {
message := `"The application *` + deploy[0].App + `* has been deployed just now by ` + deploy[0].User + ` at _` + deploy[0].ConvertTimestampToRFC822() + `_"`

data := []byte(`{"text":` + message + `}`)
resp, err := http.Post(s.WebhookUrl, "application/json", bytes.NewReader(data))
if err != nil {
return err
}

if resp.StatusCode != 200 {
return errors.New(`HangoutsChat - response status code is ` + resp.Status)
}
return nil
}

func (s HangoutsChat) ValidatesFields() error {
return nil
}
61 changes: 61 additions & 0 deletions hook/hangouts_chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package hook

import (
"testing"

"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
)

var hangoutWebhookUrl = "https://hangouts.chat/123"

func TestHangoutWhenNotificatedSuccessful(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", hangoutWebhookUrl,
httpmock.NewStringResponder(200, `ok`))

hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
var deploy []types.Deploy
deploy = append(deploy, types.Deploy{App: "app-sample"})

err := hangout.CallHook(deploy)
if err != nil {
t.Error(err)
}
}

func TestHangoutWhenResponseStatusCodeIsnt200(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", hangoutWebhookUrl,
httpmock.NewStringResponder(400, `error`))

hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
var deploy []types.Deploy
deploy = append(deploy, types.Deploy{App: "app-sample"})

err := hangout.CallHook(deploy)
expected := "HangoutsChat - response status code is 400"
if err == nil || err.Error() != expected {
t.Error("Expected: "+expected+", but got", err.Error())
}
}

func TestHangoutReturnsErrorWhenRequestFail(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterNoResponder(nil)

hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl}
var deploy []types.Deploy
deploy = append(deploy, types.Deploy{App: "app-sample"})

err := hangout.CallHook(deploy)
if err == nil {
t.Error("The request has been failed but no error was raised")
}
}
10 changes: 9 additions & 1 deletion hook/hook.go
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ func Execute(h types.Hook, t types.Tsuru) {
utils.LogError(err.Error())
} else {
for hookName, conf := range config.Data() {
switch strings.Title(hookName.(string)) {
switch defineHookName(hookName.(string)) {
case "Slack":
h = &Slack{}
case "Sentry":
@@ -31,6 +31,8 @@ func Execute(h types.Hook, t types.Tsuru) {
h = &Rollbar{}
case "Newrelic":
h = &NewRelic{}
case "Hangouts Chat":
h = &HangoutsChat{}
default:
continue
}
@@ -74,3 +76,9 @@ func executeHook(h types.Hook, deploy []types.Deploy, conf interface{}) error {

return nil
}

func defineHookName(name string) string {
return strings.Title(
strings.Join(strings.Split(name, "_"), " "),
)
}
4 changes: 4 additions & 0 deletions hook/hook_test.go
Original file line number Diff line number Diff line change
@@ -91,6 +91,9 @@ func TestShouldExecuteHooksFromConfig(t *testing.T) {
httpmock.RegisterResponder("POST", "http://dummy.sample",
httpmock.NewStringResponder(200, `ok`))

httpmock.RegisterResponder("POST", "http://hangouts.chat.sample",
httpmock.NewStringResponder(200, `ok`))

httpmock.RegisterResponder("POST", "https://api.rollbar.com/api/1/deploy/",
httpmock.NewStringResponder(200, `ok`))

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

if msg != "" {
t.Error("Expected that msg is not empty, got empty msg")
t.Error(msg)
}
}

4 changes: 3 additions & 1 deletion testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
slack:
webhook_url: http://dummy.sample
hangouts_chat:
webhook_url: http://hangouts.chat.sample
missing_hook:
field: value
rollbar:
@@ -16,4 +18,4 @@ newrelic:
host: https://api.newrelic.com
application_id: "01234"
api_key: 0a0b11223344
revision: 0.0.1
revision: 0.0.1

0 comments on commit 3e26616

Please sign in to comment.