Skip to content

Commit

Permalink
Add slack notification
Browse files Browse the repository at this point in the history
  • Loading branch information
sukrit007 committed Jul 18, 2016
1 parent edf5aa7 commit 8a08b3b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
17 changes: 17 additions & 0 deletions deployer/tasks/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def notify_hipchat(obj, ctx, level, config, security_profile):
headers=headers).raise_for_status()


@app.task
def notify_slack(obj, ctx, level, config, security_profile):
config = decrypt_config(config, profile=security_profile)
url = config.get('url')
notification = util.as_dict(obj)
notification['channel'] = config.get('channel')
msg = templatefactory.render_template(
'slack.json.jinja', notification=notification, ctx=ctx,
level=level)
headers = {
'content-type': 'application/json',
}
if url:
requests.post(url, data=msg, headers=headers) \
.raise_for_status()


@app.task
def notify_github(obj, ctx, level, config, security_profile):
config = decrypt_config(config, profile=security_profile)
Expand Down
28 changes: 28 additions & 0 deletions deployer/templates/slack.json.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{%- set git = ctx.deployment['meta-info'].git or {} %}
{
"username": "Deployer ({{ctx.cluster}}-{{ctx.operation}})",
"channel": "{{ notification.channel or '#totem' }}",
"text":
{% if git.type == 'github' %}
"<https://github.com/{{git.owner}}|{{git.owner or 'NA'}}> / <https://github.com/{{git.owner}}/{{git.repo}}|{{git.repo or 'NA'}}> / <https://github.com/{{git.owner}}/{{git.repo}}/{{git.ref}}|{{git.ref or 'NA'}}> / <https://github.com/{{ctx.owner}}/{{ctx.repo}}/commits/{{ctx.commit or ctx.ref}}|{{ctx.commit or ctx.ref or 'NA'}}>"
{% else %}
"{{git.owner or 'NA'}} / {{git.repo or 'NA'}} / {{git.ref or 'NA'}} / {{git.commit or 'NA'}}"
{% endif %},

"attachments": [
{
"text": "{{ notification.code}}: {{ notification.message | truncate(1000) }}",
"color":
{% if level == 1 %}
"danger"
{% elif level == 2 %}
"warning"
{% elif level == 3 %}
"good"
{% else %}
"#439FE0"
{% endif %}
}
]

}
43 changes: 43 additions & 0 deletions tests/integration/templates/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json

from nose.tools import eq_, ok_

from deployer import templatefactory

"""
Tests for Slack Jinja templates
"""


def test_slack_template():
"""
should render json output for slack API.
"""

output = templatefactory.render_template(
'slack.json.jinja',
ctx={
"deployment": {
"meta-info": {
"git": {
"owner": "test-owner",
"repo": "test-repo",
"ref": "test-ref",
"type": "github",
}
}
},
"cluster": "local",
"operation": "test",

},
notification={
"message": "test message",
"code": "DEPLOYMENT_FAILED"
}, level=1)
slack_dict = json.loads(output)

eq_(slack_dict.get("username"), "Deployer (local-test)")
eq_(slack_dict.get("channel"), "#totem")
ok_(slack_dict.get("text"))
ok_(slack_dict.get("attachments"))
54 changes: 54 additions & 0 deletions tests/unit/tasks/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,60 @@ def test_notify_hipchat_for_level_success(m_json, m_templatefactory,
})


@patch('deployer.tasks.notification.requests')
@patch('deployer.tasks.notification.templatefactory')
@patch('deployer.tasks.notification.json')
def test_notify_slack(m_json, m_templatefactory, m_requests):
"""
Should send slack notification
:return:
"""
# Given: Template factory that renders html template for notification
m_templatefactory.render_template.return_value = '{}'

# And: Mock implementation for jsonify (for validating data)
m_json.dumps.side_effect = lambda data: data

# When: I send message using slack
notification.notify_slack(
{'message': 'mock'}, {}, LEVEL_FAILED,
{'url': 'http://mockslackurl'},
'default')

# Then: Notification gets send successfully
m_requests.post.assert_called_once_with(
'http://mockslackurl',
headers={
'content-type': 'application/json'
},
data='{}')


@patch('deployer.tasks.notification.requests')
@patch('deployer.tasks.notification.templatefactory')
@patch('deployer.tasks.notification.json')
def test_notify_slack_when_url_is_not_set(m_json, m_templatefactory,
m_requests):
"""
Should not send slack notification
:return:
"""
# Given: Template factory that renders html template for notification
m_templatefactory.render_template.return_value = '{}'

# And: Mock implementation for jsonify (for validating data)
m_json.dumps.side_effect = lambda data: data

# When: I send message using slack
notification.notify_slack(
{'message': 'mock'}, {}, LEVEL_FAILED,
{},
'default')

# Then: Notification is not sent
m_requests.post.assert_not_called()


@patch('deployer.tasks.notification.requests')
@patch('deployer.tasks.notification.json')
def test_github(m_json, m_requests):
Expand Down

0 comments on commit 8a08b3b

Please sign in to comment.