move conversion of data to json in slack API handling (#1101) (#1120)

* move conversion of data to json in slack API handling

at one point in do_notify_slack, we do operations on the payload
variable assuming it's a dict, but it's not: it's a json encoded string.

it's useful to operate on the payload as a dict rather than a string, so
solve this problem by moving the jsonify call to right before sending
the payload to the slack API.

fixes #1097

* add changelog fragment

* Update changelogs/fragments/1101-slack-ts-fix.yaml

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

* return payload as a json encoded string for backwards compatibility

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit b5b5410575)

Co-authored-by: Andreas Lutro <anlutro@gmail.com>
This commit is contained in:
patchback[bot] 2020-10-19 11:33:03 +03:00 committed by GitHub
parent 3c6e84b21c
commit e3e3682eb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View file

@ -328,7 +328,6 @@ def build_payload_for_slack(module, text, channel, thread_id, username, icon_url
]
payload['blocks'] = recursive_escape_quotes(blocks, block_keys_to_escape)
payload = module.jsonify(payload)
return payload
@ -377,14 +376,15 @@ def do_notify_slack(module, domain, token, payload):
if use_webapi:
headers['Authorization'] = 'Bearer ' + token
response, info = fetch_url(module=module, url=slack_uri, headers=headers, method='POST', data=payload)
data = module.jsonify(payload)
response, info = fetch_url(module=module, url=slack_uri, headers=headers, method='POST', data=data)
if info['status'] != 200:
if use_webapi:
obscured_incoming_webhook = slack_uri
else:
obscured_incoming_webhook = SLACK_INCOMING_WEBHOOK % ('[obscured]')
module.fail_json(msg=" failed to send %s to %s: %s" % (payload, 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
if use_webapi:
@ -459,8 +459,10 @@ def main():
if 'ok' in slack_response:
# Evaluate WebAPI response
if slack_response['ok']:
# return payload as a string for backwards compatibility
payload_json = module.jsonify(payload)
module.exit_json(changed=changed, ts=slack_response['ts'], channel=slack_response['channel'],
api=slack_response, payload=payload)
api=slack_response, payload=payload_json)
else:
module.fail_json(msg="Slack API error", error=slack_response['error'])
else: