Skip to content

Commit

Permalink
Merge pull request #547 from Hugefarsen/feature/AddJiraUrlToSlack
Browse files Browse the repository at this point in the history
Feature/add jira url to slack
  • Loading branch information
jertel authored Nov 7, 2021
2 parents b075b47 + 364ee81 commit 27deb8a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Add summary_table_max_rows optional configuration to limit rows in summary tables - [#508](https://github.com/jertel/elastalert2/pull/508) - @mdavyt92
- Added support for shortening Kibana Discover URLs using Kibana Shorten URL API - [#512](https://github.com/jertel/elastalert2/pull/512) - @JeffAshton
- Added new alerter `HTTP Post 2` which allow more flexibility to build the body/headers of the request. - [#530](https://github.com/jertel/elastalert2/pull/530) - @lepouletsuisse
- [Slack] Added new option to include url to jira ticket if it is created in the same pipeline. - [#547](https://github.com/jertel/elastalert2/pull/547) - @hugefarsen

## Other changes
- [Docs] Add exposed metrics documentation - [#498](https://github.com/jertel/elastalert2/pull/498) - @thisisxgp
Expand Down
8 changes: 7 additions & 1 deletion docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ Required:

Optional:

``discord_emoji_title``: By default ElastAlert 2 will use the ``:warning:`` emoji when posting to the channel. You can use a different emoji per ElastAlert 2 rule. Any Apple emoji can be used, see http://emojipedia.org/apple/ . If slack_icon_url_override parameter is provided, emoji is ignored.
``discord_emoji_title``: By default ElastAlert 2 will use the ``:warning:`` emoji when posting to the channel. You can use a different emoji per ElastAlert 2 rule. Any Apple emoji can be used, see http://emojipedia.org/apple/ . If discord_embed_icon_url parameter is provided, emoji is ignored.

``discord_proxy``: By default ElastAlert 2 will not use a network proxy to send notifications to Discord. Set this option using ``hostname:port`` if you need to use a proxy. only supports https.

Expand Down Expand Up @@ -2883,6 +2883,12 @@ Example slack_attach_kibana_discover_url, slack_kibana_discover_color, slack_kib

``slack_msg_pretext``: You can set the message attachment pretext using this option. Defaults to "".

``slack_attach_jira_ticket_url``: Add url to the jira ticket created. Only works if the Jira alert runs before Slack alert. Set the field to ``True`` in order to generate the url. Defaults to ``False``.

``slack_jira_ticket_color``: The color of the Jira Ticket url attachment. Defaults to ``#ec4b98``.

``slack_jira_ticket_title``: The title of the Jira Ticket url attachment. Defaults to ``Jira Ticket``.

Splunk On-Call (Formerly VictorOps)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
12 changes: 12 additions & 0 deletions elastalert/alerters/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def __init__(self, rule):
self.slack_author_link = self.rule.get('slack_author_link', '')
self.slack_author_icon = self.rule.get('slack_author_icon', '')
self.slack_msg_pretext = self.rule.get('slack_msg_pretext', '')
self.slack_attach_jira_ticket_url = self.rule.get('slack_attach_jira_ticket_url', False)
self.slack_jira_ticket_color = self.rule.get('slack_jira_ticket_color', '#ec4b98')
self.slack_jira_ticket_title = self.rule.get('slack_jira_ticket_title', 'Jira Ticket')

def format_body(self, body):
# https://api.slack.com/docs/formatting
Expand Down Expand Up @@ -139,6 +142,15 @@ def alert(self, matches):
'title_link': kibana_discover_url
})

if self.slack_attach_jira_ticket_url and self.pipeline is not None and 'jira_ticket' in self.pipeline:
jira_url = '%s/browse/%s' % (self.pipeline['jira_server'], self.pipeline['jira_ticket'])

payload['attachments'].append({
'color': self.slack_jira_ticket_color,
'title': self.slack_jira_ticket_title,
'title_link': jira_url
})

for url in self.slack_webhook_url:
for channel_override in self.slack_channel_override:
try:
Expand Down
3 changes: 3 additions & 0 deletions elastalert/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ properties:
slack_attach_kibana_discover_url: {type: boolean}
slack_kibana_discover_color: {type: string}
slack_kibana_discover_title: {type: string}
slack_attach_jira_ticket_url: {type: boolean}
slack_jira_ticket_color: {type: string}
slack_jira_ticket_title: {type: string}
slack_ca_certs: {type: boolean}
slack_footer: {type: string}
slack_footer_icon: {type: string}
Expand Down
53 changes: 53 additions & 0 deletions tests/alerters/slack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,3 +1398,56 @@ def test_slack_required_error(slack_webhook_url, expected_data):
assert expected_data == actual_data
except Exception as ea:
assert expected_data in str(ea)


def test_slack_attach_jira_url_when_generated():
rule = {
'name': 'Test Rule',
'type': 'any',
'slack_attach_jira_ticket_url': True,
'slack_jira_ticket_title': 'My Title',
'slack_jira_ticket_color': '#aabbcc',
'slack_webhook_url': 'http://please.dontgohere.slack',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = SlackAlerter(rule)
alert.pipeline = {'jira_ticket': 'foo_ticket', 'jira_server': 'https://myjiraserver'}
match = {
'@timestamp': '2016-01-01T00:00:00',
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'username': 'elastalert',
'parse': 'none',
'text': '',
'attachments': [
{
'color': 'danger',
'title': 'Test Rule',
'text': BasicMatchString(rule, match).__str__(),
'mrkdwn_in': ['text', 'pretext'],
'fields': []
},
{
'color': '#aabbcc',
'title': 'My Title',
'title_link': 'https://myjiraserver/browse/foo_ticket'
}
],
'icon_emoji': ':ghost:',
'channel': ''
}
mock_post_request.assert_called_once_with(
rule['slack_webhook_url'],
data=mock.ANY,
headers={'content-type': 'application/json'},
proxies=None,
verify=True,
timeout=10
)
actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data

0 comments on commit 27deb8a

Please sign in to comment.