Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove automatically adding # symbol to channel names #5629

Merged
Merged
Original file line number Diff line number Diff line change
@@ -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)."
32 changes: 27 additions & 5 deletions plugins/modules/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@
type: list
elements: dict
version_added: 1.0.0
prepend_hash:
wmcbroomd2d marked this conversation as resolved.
Show resolved Hide resolved
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'
wmcbroomd2d marked this conversation as resolved.
Show resolved Hide resolved
version_added: 6.1.0
"""

EXAMPLES = """
Expand Down Expand Up @@ -289,18 +304,23 @@ 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))
elif text is not None:
# 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:
Expand Down Expand Up @@ -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,
)
Expand All @@ -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):
Expand All @@ -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:
Expand Down