Skip to content

Commit

Permalink
Merge pull request #226 from nsano-rururu/fix_dingtalk
Browse files Browse the repository at this point in the history
Changed required items of dingtalk alerter
  • Loading branch information
jertel committed Jun 4, 2021
2 parents 84a1b5f + b5c0387 commit 1b77f8d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ Required:

``dingtalk_access_token``: Dingtalk access token.

``dingtalk_msgtype``: Dingtalk msgtype. ``text``, ``markdown``, ``single_action_card``, ``action_card``.
``dingtalk_msgtype``: Dingtalk msgtype, default to ``text``. ``markdown``, ``single_action_card``, ``action_card``.

dingtalk_msgtype single_action_card Required:

Expand Down
6 changes: 3 additions & 3 deletions elastalert/alerters/dingtalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

class DingTalkAlerter(Alerter):
""" Creates a DingTalk room message for each alert """
required_options = frozenset(['dingtalk_access_token', 'dingtalk_msgtype'])
required_options = frozenset(['dingtalk_access_token'])

def __init__(self, rule):
super(DingTalkAlerter, self).__init__(rule)
self.dingtalk_access_token = self.rule.get('dingtalk_access_token')
self.dingtalk_access_token = self.rule['dingtalk_access_token']
self.dingtalk_webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=%s' % (self.dingtalk_access_token)
self.dingtalk_msgtype = self.rule.get('dingtalk_msgtype')
self.dingtalk_msgtype = self.rule.get('dingtalk_msgtype', 'text')
self.dingtalk_single_title = self.rule.get('dingtalk_single_title', 'elastalert')
self.dingtalk_single_url = self.rule.get('dingtalk_single_url', '')
self.dingtalk_btn_orientation = self.rule.get('dingtalk_btn_orientation', '')
Expand Down
50 changes: 50 additions & 0 deletions tests/alerters/dingtalk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,53 @@ def test_dingtalk_ea_exception():
alert.alert([match])
except EAException:
assert True


def test_dingtalk_getinfo():
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'dingtalk_access_token': 'xxxxxxx',
'alert': [],
'alert_subject': 'Test DingTalk'
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)

expected_data = {
'type': 'dingtalk',
"dingtalk_webhook_url": 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'
}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('dingtalk_access_token,, expected_data', [
('', True),
('xxxxxxx',
{
'type': 'dingtalk',
"dingtalk_webhook_url": 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'
}),
])
def test_dingtalk_key_error(dingtalk_access_token, expected_data):
try:
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'alert': [],
'alert_subject': 'Test DingTalk'
}

if dingtalk_access_token != '':
rule['dingtalk_access_token'] = dingtalk_access_token

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)

actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data

0 comments on commit 1b77f8d

Please sign in to comment.