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

Remove automatically adding # symbol to channel names (#5629)

* Add regex to match all channel ids

* Add changelog fragment

* Allow matching of channel ids with 9-11 characters

* Fix file name

* Update changelogs/fragments/5629-add-channel-prefix-regex.yml

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

* Remove channel auto prepend #

* Update changelog fragment

* Add prepend_hash option

* Add version_added to prepend_hash doc string

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

* Add description of possible values for the prepend_hash option

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

* Remove old channel assign statement

* Update changelogs/fragments/5629-add-prepend-hash-option-for-channel-id.yml

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

* Update changelog fragment tag

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

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
William McBroom 2022-12-01 15:17:09 -06:00 committed by GitHub
parent 428e181440
commit 03039a56c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View file

@ -0,0 +1,2 @@
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)."

View file

@ -129,6 +129,21 @@ options:
type: list
elements: dict
version_added: 1.0.0
prepend_hash:
type: str
description:
- 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'
- 'auto'
default: 'auto'
version_added: 6.1.0
"""
EXAMPLES = """
@ -289,7 +304,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,10 +312,15 @@ 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')):
payload['channel'] = channel
else:
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
if thread_id is not None:
payload['thread_ts'] = thread_id
if username is not None:
@ -428,6 +448,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,
)
@ -446,6 +467,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):
@ -470,7 +492,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: