From 5ae0631307214ab1fff23fe2239fff4f2a42541d Mon Sep 17 00:00:00 2001 From: William McBroom Date: Tue, 29 Nov 2022 15:03:14 -0600 Subject: [PATCH 01/13] Add regex to match all channel ids --- plugins/modules/slack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index 46602a5d167..e61679372b6 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -297,7 +297,7 @@ def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_e # With a custom color we have to set the message as attachment, and explicitly turn markdown parsing on for it. payload = dict(attachments=[dict(text=escape_quotes(text), color=color, mrkdwn_in=["text"])]) if channel is not None: - if channel.startswith(('#', '@', 'C0', 'GF', 'G0', 'CP')): + if channel.startswith(('#', '@')) or re.match(r'^[C|D|G][A-Za-z0-9]{8}$', channel): payload['channel'] = channel else: payload['channel'] = '#' + channel From 43ceac65801a0b6ca5c339b52b88417ef64706bd Mon Sep 17 00:00:00 2001 From: William McBroom Date: Tue, 29 Nov 2022 15:48:00 -0600 Subject: [PATCH 02/13] Add changelog fragment --- changelogs/fragments/5629-add-channel-prefix-regex.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/5629-add-channel-prefix-regex.yml diff --git a/changelogs/fragments/5629-add-channel-prefix-regex.yml b/changelogs/fragments/5629-add-channel-prefix-regex.yml new file mode 100644 index 00000000000..7d979c945b3 --- /dev/null +++ b/changelogs/fragments/5629-add-channel-prefix-regex.yml @@ -0,0 +1,2 @@ +bugfixes: + - slack - When ``message-id`` was passed it failed for channels that have a 2 digit prefix not in the hardcoded list, for example ``G4``, because the ``#`` symbol was added before the ``channel_id``. This change adds a regex to catch those channels. (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file From 76a096154115fbab7b06bbdb748c7a5daa1230de Mon Sep 17 00:00:00 2001 From: William McBroom Date: Tue, 29 Nov 2022 16:06:52 -0600 Subject: [PATCH 03/13] Allow matching of channel ids with 9-11 characters --- plugins/modules/slack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index e61679372b6..acc3e2fcabc 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -297,7 +297,7 @@ def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_e # With a custom color we have to set the message as attachment, and explicitly turn markdown parsing on for it. payload = dict(attachments=[dict(text=escape_quotes(text), color=color, mrkdwn_in=["text"])]) if channel is not None: - if channel.startswith(('#', '@')) or re.match(r'^[C|D|G][A-Za-z0-9]{8}$', channel): + if channel.startswith(('#', '@')) or re.match(r'^[C|D|G][A-Za-z0-9]{8,10}$', channel): payload['channel'] = channel else: payload['channel'] = '#' + channel From f47fca431e9de9fbae615d6a9522090472da31e1 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Tue, 29 Nov 2022 16:08:14 -0600 Subject: [PATCH 04/13] Fix file name --- ...hannel-prefix-regex.yml => 5629-add-channel-prefix-regex.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelogs/fragments/{5629-add-channel-prefix-regex.yml => 5629-add-channel-prefix-regex.yml} (100%) diff --git a/changelogs/fragments/5629-add-channel-prefix-regex.yml b/changelogs/fragments/5629-add-channel-prefix-regex.yml similarity index 100% rename from changelogs/fragments/5629-add-channel-prefix-regex.yml rename to changelogs/fragments/5629-add-channel-prefix-regex.yml From 236ef9029a843d3926ec02a002bd6db223236608 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 14:16:34 -0600 Subject: [PATCH 05/13] Update changelogs/fragments/5629-add-channel-prefix-regex.yml Co-authored-by: Felix Fontein --- changelogs/fragments/5629-add-channel-prefix-regex.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/5629-add-channel-prefix-regex.yml b/changelogs/fragments/5629-add-channel-prefix-regex.yml index 7d979c945b3..00f87ca8fc8 100644 --- a/changelogs/fragments/5629-add-channel-prefix-regex.yml +++ b/changelogs/fragments/5629-add-channel-prefix-regex.yml @@ -1,2 +1,2 @@ bugfixes: - - slack - When ``message-id`` was passed it failed for channels that have a 2 digit prefix not in the hardcoded list, for example ``G4``, because the ``#`` symbol was added before the ``channel_id``. This change adds a regex to catch those channels. (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file + - slack - when ``message-id`` was passed it failed for channels that have a 2 digit prefix not in the hardcoded list, for example ``G4``, because the ``#`` symbol was added before the ``channel_id``. This change adds a regex to catch those channels (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file From e6cc9642934cec84e8155536b0840a9a1fff8ed9 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 14:37:01 -0600 Subject: [PATCH 06/13] Remove channel auto prepend # --- plugins/modules/slack.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index acc3e2fcabc..05f2dc27a4c 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -297,10 +297,7 @@ def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_e # With a custom color we have to set the message as attachment, and explicitly turn markdown parsing on for it. payload = dict(attachments=[dict(text=escape_quotes(text), color=color, mrkdwn_in=["text"])]) if channel is not None: - if channel.startswith(('#', '@')) or re.match(r'^[C|D|G][A-Za-z0-9]{8,10}$', channel): - payload['channel'] = channel - else: - payload['channel'] = '#' + channel + payload['channel'] = channel if thread_id is not None: payload['thread_ts'] = thread_id if username is not None: From f0f1b90be8c083b73e0cd1df1010d1d3a50cfe89 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 14:59:57 -0600 Subject: [PATCH 07/13] Update changelog fragment --- changelogs/fragments/5629-add-channel-prefix-regex.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/5629-add-channel-prefix-regex.yml b/changelogs/fragments/5629-add-channel-prefix-regex.yml index 00f87ca8fc8..ab8de656125 100644 --- a/changelogs/fragments/5629-add-channel-prefix-regex.yml +++ b/changelogs/fragments/5629-add-channel-prefix-regex.yml @@ -1,2 +1,2 @@ bugfixes: - - slack - when ``message-id`` was passed it failed for channels that have a 2 digit prefix not in the hardcoded list, for example ``G4``, because the ``#`` symbol was added before the ``channel_id``. This change adds a regex to catch those channels (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file + - slack - fix message update for channels which start with a prefix not in the hardcoded list, for example ``G4``. When ``message-id`` was passed it failed for channels which started with ``G4`` because the ``#`` symbol was added before the ``channel_id``. The fix is to remove the logic to prepend a ``#`` and instead leave the passed in channel value alone (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file From 454a16534b64860fed92c88b77ec4859624f3cc6 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 15:46:02 -0600 Subject: [PATCH 08/13] Add prepend_hash option --- .../5629-add-channel-prefix-regex.yml | 2 -- ...add-prepend-hash-option-for-channel-id.yml | 2 ++ plugins/modules/slack.py | 25 +++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) delete mode 100644 changelogs/fragments/5629-add-channel-prefix-regex.yml create mode 100644 changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml diff --git a/changelogs/fragments/5629-add-channel-prefix-regex.yml b/changelogs/fragments/5629-add-channel-prefix-regex.yml deleted file mode 100644 index ab8de656125..00000000000 --- a/changelogs/fragments/5629-add-channel-prefix-regex.yml +++ /dev/null @@ -1,2 +0,0 @@ -bugfixes: - - slack - fix message update for channels which start with a prefix not in the hardcoded list, for example ``G4``. When ``message-id`` was passed it failed for channels which started with ``G4`` because the ``#`` symbol was added before the ``channel_id``. The fix is to remove the logic to prepend a ``#`` and instead leave the passed in channel value alone (https://github.com/ansible-collections/community.general/pull/5629). \ No newline at end of file diff --git a/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml new file mode 100644 index 00000000000..bd6f7ff6b34 --- /dev/null +++ b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml @@ -0,0 +1,2 @@ +bugfixes: + - "slack - fix message update for channels which start with a prefix not in the hardcoded list, for example ``G4``. When ``message-id`` was passed it failed for channels which started with ``G4`` because the ``#`` symbol was added before the ``channel_id``. Adding a prepend_hash option to workaround this issue for channel id prefixes not in the list. There are three-value options:``prepend_hash: always | never | auto``, with current default auto to maintain current behavior (https://github.com/ansible-collections/community.general/pull/5629)." \ No newline at end of file diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index 05f2dc27a4c..05bce0f9d8e 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -129,6 +129,15 @@ type: list elements: dict version_added: 1.0.0 + prepend_hash: + type: str + description: + - Setting for automatically prepending a # symbol on the passed in channel_id + choices: + - 'always' + - 'never' + - 'auto' + default: 'auto' """ EXAMPLES = """ @@ -289,7 +298,7 @@ def recursive_escape_quotes(obj, keys): 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, prepend_hash): payload = {} if color == "normal" and text is not None: payload = dict(text=escape_quotes(text)) @@ -297,6 +306,16 @@ def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_e # With a custom color we have to set the message as attachment, and explicitly turn markdown parsing on for it. payload = dict(attachments=[dict(text=escape_quotes(text), color=color, mrkdwn_in=["text"])]) if channel is not None: + if prepend_hash == 'auto': + if channel.startswith(('#', '@', 'C0', 'GF', 'G0', 'CP')): + payload['channel'] = channel + else: + payload['channel'] = '#' + channel + elif prepend_hash == 'always': + payload['channel'] = '#' + channel + elif prepend_hash == 'never': + payload['channel'] = channel + payload['channel'] = channel if thread_id is not None: payload['thread_ts'] = thread_id @@ -425,6 +444,7 @@ def main(): attachments=dict(type='list', elements='dict'), blocks=dict(type='list', elements='dict'), message_id=dict(type='str'), + prepend_hash=dict(type='str', default='auto', choices=['always', 'never', 'auto']), ), supports_check_mode=True, ) @@ -443,6 +463,7 @@ def main(): attachments = module.params['attachments'] blocks = module.params['blocks'] message_id = module.params['message_id'] + prepend_hash = module.params['prepend_hash'] color_choices = ['normal', 'good', 'warning', 'danger'] if color not in color_choices and not is_valid_hex_color(color): @@ -467,7 +488,7 @@ def main(): module.exit_json(changed=changed) 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, prepend_hash) slack_response = do_notify_slack(module, domain, token, payload) if 'ok' in slack_response: From 471dfb0ec756ee7a84e871eb1ebd1531425ec3d7 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 16:06:13 -0600 Subject: [PATCH 09/13] Add version_added to prepend_hash doc string Co-authored-by: Felix Fontein --- plugins/modules/slack.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index 05bce0f9d8e..6ab7160b2f3 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -138,6 +138,7 @@ - 'never' - 'auto' default: 'auto' + version_added: 6.1.0 """ EXAMPLES = """ From df31dbe5b403a22f25a8ff15e42c46394e671e3b Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 16:08:27 -0600 Subject: [PATCH 10/13] Add description of possible values for the prepend_hash option Co-authored-by: Felix Fontein --- plugins/modules/slack.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index 6ab7160b2f3..b65af7d6ed9 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -132,7 +132,12 @@ prepend_hash: type: str description: - - Setting for automatically prepending a # symbol on the passed in channel_id + - Setting for automatically prepending a C(#) symbol on the passed in I(channel_id). + - The C(auto) method prepends a C(#) unless I(channel_id) starts with one of C(#), C(@), C(C0), C(GF), C(G0), C(CP). + These prefixes only cover a small set of the prefixes that should not have a C(#) prepended. + Since an exact condition which I(channel_id) values must not have the C(#) prefix is not known, + the value C(auto) for this option will be deprecated in the future. It is best to explicitly set + I(prepend_hash=always) or I(prepend_hash=never) to obtain the needed behavior. choices: - 'always' - 'never' From 7982496467a79fa45ef844eec5fc1dc75cfc4303 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 16:10:14 -0600 Subject: [PATCH 11/13] Remove old channel assign statement --- plugins/modules/slack.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/modules/slack.py b/plugins/modules/slack.py index b65af7d6ed9..2854277f60e 100644 --- a/plugins/modules/slack.py +++ b/plugins/modules/slack.py @@ -321,8 +321,6 @@ def build_payload_for_slack(text, channel, thread_id, username, icon_url, icon_e payload['channel'] = '#' + channel elif prepend_hash == 'never': payload['channel'] = channel - - payload['channel'] = channel if thread_id is not None: payload['thread_ts'] = thread_id if username is not None: From 96dbb9efc5f4eada02d5de8b3923c36f547f380e Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 16:12:07 -0600 Subject: [PATCH 12/13] Update changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml Co-authored-by: Felix Fontein --- .../fragments/5629-add-prepend-hash-option-for-channel-id.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml index bd6f7ff6b34..77e4c5fa8e7 100644 --- a/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml +++ b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml @@ -1,2 +1,2 @@ bugfixes: - - "slack - fix message update for channels which start with a prefix not in the hardcoded list, for example ``G4``. When ``message-id`` was passed it failed for channels which started with ``G4`` because the ``#`` symbol was added before the ``channel_id``. Adding a prepend_hash option to workaround this issue for channel id prefixes not in the list. There are three-value options:``prepend_hash: always | never | auto``, with current default auto to maintain current behavior (https://github.com/ansible-collections/community.general/pull/5629)." \ No newline at end of file + - "slack - add option ``prepend_hash`` which allows to control whether a ``#`` is prepended to ``channel_id``. The current behavior (value ``auto``) is to prepend ``#`` unless some specific prefixes are found. That list of prefixes is incomplete, and there does not seem to exist a documented condition on when exactly ``#`` must not be prepended. We recommend to explicitly set ``prepend_hash=always`` or ``prepend_hash=never`` to avoid any ambiguity (https://github.com/ansible-collections/community.general/pull/5629)." \ No newline at end of file From 15dcbebe5a359d4b3569f9bf2e044f1edf3c21b1 Mon Sep 17 00:00:00 2001 From: William McBroom Date: Wed, 30 Nov 2022 16:32:54 -0600 Subject: [PATCH 13/13] Update changelog fragment tag Co-authored-by: Felix Fontein --- .../fragments/5629-add-prepend-hash-option-for-channel-id.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml index 77e4c5fa8e7..f38a6b4e132 100644 --- a/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml +++ b/changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml @@ -1,2 +1,2 @@ -bugfixes: +minor_changes: - "slack - add option ``prepend_hash`` which allows to control whether a ``#`` is prepended to ``channel_id``. The current behavior (value ``auto``) is to prepend ``#`` unless some specific prefixes are found. That list of prefixes is incomplete, and there does not seem to exist a documented condition on when exactly ``#`` must not be prepended. We recommend to explicitly set ``prepend_hash=always`` or ``prepend_hash=never`` to avoid any ambiguity (https://github.com/ansible-collections/community.general/pull/5629)." \ No newline at end of file