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 67a3c66
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
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")
}
}
2 changes: 2 additions & 0 deletions hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func Execute(h types.Hook, t types.Tsuru) {
h = &Rollbar{}
case "Newrelic":
h = &NewRelic{}
case "Hangout":
h = &HangoutsChat{}
default:
continue
}
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 67a3c66

Please sign in to comment.