slack_incoming: Handle null blocks and attachments.

This is not accepted according to Slack's block-builder, but is
attested in the wild.
This commit is contained in:
Alex Vandiver 2022-06-16 14:58:41 -07:00 committed by Tim Abbott
parent 74cf9a1e9f
commit 0511400d73
2 changed files with 3 additions and 2 deletions

View File

@ -1,6 +1,7 @@
{ {
"channel": "#prometheus-alerts", "channel": "#prometheus-alerts",
"username": "Alertmanager", "username": "Alertmanager",
"blocks": null,
"attachments": [ "attachments": [
{ {
"title": "[FIRING:2] InstanceDown for api-server (env=\"prod\", severity=\"critical\")", "title": "[FIRING:2] InstanceDown for api-server (env=\"prod\", severity=\"critical\")",

View File

@ -50,11 +50,11 @@ def api_slack_incoming_webhook(
body = "" body = ""
if "blocks" in payload: if "blocks" in payload and payload["blocks"]:
for block in payload["blocks"]: for block in payload["blocks"]:
body = add_block(block, body) body = add_block(block, body)
if "attachments" in payload: if "attachments" in payload and payload["attachments"]:
for attachment in payload["attachments"]: for attachment in payload["attachments"]:
body = add_attachment(attachment, body) body = add_attachment(attachment, body)