-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from lucasgomide/add-hangout-chat
Add hook: HangoutsChat
Showing
6 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters