From 5ec0865327f4a7c958482bd2b93c792bbc3b526b Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 20 Jul 2022 11:37:30 -0700 Subject: [PATCH] Merging the step marketplace fix into argo-hub Signed-off-by: Laurent --- .../0.0.2/images/post-to-channel/lib/slack.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/workflows/slack/versions/0.0.2/images/post-to-channel/lib/slack.py b/workflows/slack/versions/0.0.2/images/post-to-channel/lib/slack.py index 89685f3c..ca503369 100644 --- a/workflows/slack/versions/0.0.2/images/post-to-channel/lib/slack.py +++ b/workflows/slack/versions/0.0.2/images/post-to-channel/lib/slack.py @@ -6,6 +6,7 @@ import os import sys import logging +import json from slack_sdk import WebClient from slack_sdk.errors import SlackApiError @@ -13,9 +14,10 @@ def main(): log_format = "%(asctime)s:%(levelname)s:%(name)s.%(funcName)s: %(message)s" logging.basicConfig(format = log_format, level = os.environ['LOG_LEVEL'].upper()) - channel=os.getenv('SLACK_CHANNEL') - message=os.getenv('SLACK_MESSAGE', "") - token =os.getenv('SLACK_TOKEN') + channel =os.getenv('SLACK_CHANNEL') + message =os.getenv('SLACK_MESSAGE', "") + token =os.getenv('SLACK_TOKEN') + attachments =os.getenv('SLACK_ATTACHMENTS', "") if ( channel == None ): logging.error("SLACK_CHANNEL is not defined") @@ -25,6 +27,13 @@ def main(): logging.error("SLACK_TOKEN is not defined") sys.exit(1) + if ( attachments != "" ): + try: + attachments = json.loads(attachments) + except ValueError as e: + logging.error(f"Error decoding attachments: {e}") + sys.exit(3) + logging.info("Connecting to Slack") client = WebClient(token=token) @@ -41,8 +50,7 @@ def main(): sys.exit(3) try: - response = client.chat_postMessage(channel=channel, text=message) - assert response["message"]["text"] == message + response = client.chat_postMessage(channel=channel, text=message, attachments=attachments) except SlackApiError as e: # You will get a SlackApiError if "ok" is False assert e.response["ok"] is False