1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

slack - minor refactoring and pythonifying (#3205)

* slack - minor refactoring and pythonifying

* added changelog fragment

* Update changelogs/fragments/3205-slack-minor-refactor.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2021-08-16 22:24:15 +12:00 committed by GitHub
parent 16945d3847
commit 8a4cdd2b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- slack - minor refactoring (https://github.com/ansible-collections/community.general/pull/3205).

View file

@ -264,12 +264,12 @@ def is_valid_hex_color(color_choice):
def escape_quotes(text): def escape_quotes(text):
'''Backslash any quotes within text.''' """Backslash any quotes within text."""
return "".join(escape_table.get(c, c) for c in text) return "".join(escape_table.get(c, c) for c in text)
def recursive_escape_quotes(obj, keys): def recursive_escape_quotes(obj, keys):
'''Recursively escape quotes inside supplied keys inside block kit objects''' """Recursively escape quotes inside supplied keys inside block kit objects"""
if isinstance(obj, dict): if isinstance(obj, dict):
escaped = {} escaped = {}
for k, v in obj.items(): for k, v in obj.items():
@ -284,7 +284,7 @@ def recursive_escape_quotes(obj, keys):
return escaped return escaped
def build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names, def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_emoji, link_names,
parse, color, attachments, blocks, message_id): parse, color, attachments, blocks, message_id):
payload = {} payload = {}
if color == "normal" and text is not None: if color == "normal" and text is not None:
@ -344,7 +344,7 @@ def build_payload_for_slack(module, text, channel, thread_id, username, icon_url
return payload return payload
def get_slack_message(module, domain, token, channel, ts): def get_slack_message(module, token, channel, ts):
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept': 'application/json', 'Accept': 'application/json',
@ -372,7 +372,7 @@ def do_notify_slack(module, domain, token, payload):
use_webapi = False use_webapi = False
if token.count('/') >= 2: if token.count('/') >= 2:
# New style webhook token # New style webhook token
slack_uri = SLACK_INCOMING_WEBHOOK % (token) slack_uri = SLACK_INCOMING_WEBHOOK % token
elif re.match(r'^xox[abp]-\S+$', token): elif re.match(r'^xox[abp]-\S+$', token):
slack_uri = SLACK_UPDATEMESSAGE_WEBAPI if 'ts' in payload else SLACK_POSTMESSAGE_WEBAPI slack_uri = SLACK_UPDATEMESSAGE_WEBAPI if 'ts' in payload else SLACK_POSTMESSAGE_WEBAPI
use_webapi = True use_webapi = True
@ -396,7 +396,7 @@ def do_notify_slack(module, domain, token, payload):
if use_webapi: if use_webapi:
obscured_incoming_webhook = slack_uri obscured_incoming_webhook = slack_uri
else: else:
obscured_incoming_webhook = SLACK_INCOMING_WEBHOOK % ('[obscured]') obscured_incoming_webhook = SLACK_INCOMING_WEBHOOK % '[obscured]'
module.fail_json(msg=" failed to send %s to %s: %s" % (data, obscured_incoming_webhook, info['msg'])) module.fail_json(msg=" failed to send %s to %s: %s" % (data, obscured_incoming_webhook, info['msg']))
# each API requires different handling # each API requires different handling
@ -409,21 +409,21 @@ def do_notify_slack(module, domain, token, payload):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
domain=dict(type='str', required=False, default=None), domain=dict(type='str'),
token=dict(type='str', required=True, no_log=True), token=dict(type='str', required=True, no_log=True),
msg=dict(type='str', required=False, default=None), msg=dict(type='str'),
channel=dict(type='str', default=None), channel=dict(type='str'),
thread_id=dict(type='str', default=None), thread_id=dict(type='str'),
username=dict(type='str', default='Ansible'), username=dict(type='str', default='Ansible'),
icon_url=dict(type='str', default='https://www.ansible.com/favicon.ico'), icon_url=dict(type='str', default='https://www.ansible.com/favicon.ico'),
icon_emoji=dict(type='str', default=None), icon_emoji=dict(type='str'),
link_names=dict(type='int', default=1, choices=[0, 1]), link_names=dict(type='int', default=1, choices=[0, 1]),
parse=dict(type='str', default=None, choices=['none', 'full']), parse=dict(type='str', choices=['none', 'full']),
validate_certs=dict(default=True, type='bool'), validate_certs=dict(default=True, type='bool'),
color=dict(type='str', default='normal'), color=dict(type='str', default='normal'),
attachments=dict(type='list', elements='dict', required=False, default=None), attachments=dict(type='list', elements='dict'),
blocks=dict(type='list', elements='dict'), blocks=dict(type='list', elements='dict'),
message_id=dict(type='str', default=None), message_id=dict(type='str'),
), ),
supports_check_mode=True, supports_check_mode=True,
) )
@ -453,7 +453,7 @@ def main():
# if updating an existing message, we can check if there's anything to update # if updating an existing message, we can check if there's anything to update
if message_id is not None: if message_id is not None:
changed = False changed = False
msg = get_slack_message(module, domain, token, channel, message_id) msg = get_slack_message(module, token, channel, message_id)
for key in ('icon_url', 'icon_emoji', 'link_names', 'color', 'attachments', 'blocks'): for key in ('icon_url', 'icon_emoji', 'link_names', 'color', 'attachments', 'blocks'):
if msg.get(key) != module.params.get(key): if msg.get(key) != module.params.get(key):
changed = True changed = True
@ -465,7 +465,7 @@ def main():
elif module.check_mode: elif module.check_mode:
module.exit_json(changed=changed) module.exit_json(changed=changed)
payload = build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names, payload = build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_emoji, link_names,
parse, color, attachments, blocks, message_id) parse, color, attachments, blocks, message_id)
slack_response = do_notify_slack(module, domain, token, payload) slack_response = do_notify_slack(module, domain, token, payload)