Skip to content

Commit

Permalink
Add hook: HangoutsChat
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgomide committed Jul 23, 2018
1 parent f3aff32 commit e1db337
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
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
Expand Up @@ -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":
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Up @@ -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`))

Expand All @@ -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)
}
}

Expand Down
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:
Expand All @@ -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 e1db337

Please sign in to comment.