From 941fa2b4dff1018de64e7c8bf5645c0e4c587fc7 Mon Sep 17 00:00:00 2001 From: Kash law Date: Mon, 16 May 2022 12:23:53 -0400 Subject: [PATCH 1/3] remove message assert as it fails when using interpolation --- incubating/slack-post-channel/lib/slack.py | 1 - 1 file changed, 1 deletion(-) diff --git a/incubating/slack-post-channel/lib/slack.py b/incubating/slack-post-channel/lib/slack.py index 89685f3c7..16a6fe566 100644 --- a/incubating/slack-post-channel/lib/slack.py +++ b/incubating/slack-post-channel/lib/slack.py @@ -42,7 +42,6 @@ def main(): try: response = client.chat_postMessage(channel=channel, text=message) - assert response["message"]["text"] == message except SlackApiError as e: # You will get a SlackApiError if "ok" is False assert e.response["ok"] is False From a104b11dec33364d31e09c0b7e5844ffbaf200e3 Mon Sep 17 00:00:00 2001 From: Kash law Date: Mon, 16 May 2022 13:39:36 -0400 Subject: [PATCH 2/3] add optional arg SLACK_ATTACHMENTS --- incubating/slack-post-channel/lib/slack.py | 17 +++++++++++++---- incubating/slack-post-channel/step.yaml | 7 ++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/incubating/slack-post-channel/lib/slack.py b/incubating/slack-post-channel/lib/slack.py index 16a6fe566..ca5033697 100644 --- a/incubating/slack-post-channel/lib/slack.py +++ b/incubating/slack-post-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,7 +50,7 @@ def main(): sys.exit(3) try: - response = client.chat_postMessage(channel=channel, 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 diff --git a/incubating/slack-post-channel/step.yaml b/incubating/slack-post-channel/step.yaml index d83aaa347..e4bbade97 100644 --- a/incubating/slack-post-channel/step.yaml +++ b/incubating/slack-post-channel/step.yaml @@ -2,7 +2,7 @@ kind: step-type version: '1.0' metadata: name: slack-post-channel - version: 0.0.6 + version: 0.0.7 title: Send a Slack message to a channel isPublic: true description: Send a message to a named Slack channel (instead of using a webhook URL) @@ -67,6 +67,11 @@ spec: "description": "The message to send to the channel. Use <@ID> to tag a user. Check https://api.slack.com/reference/surfaces/formatting for details.", "default": "Message sent from Codefresh was not defined explicitely." }, + "SLACK_ATTACHMENTS": { + "type": "string", + "description": "Attachments to send. Documentation https://api.slack.com/docs/message-attachments", + "default": "" + }, "LOG_LEVEL": { "type": "string", "description": "Set log level, default info", From e7479c3e0a255261d7a44ec70f4ec379bd1cbcfb Mon Sep 17 00:00:00 2001 From: Kash law Date: Mon, 16 May 2022 17:11:32 -0400 Subject: [PATCH 3/3] bump default image version to 0.0.7 --- incubating/slack-post-channel/step.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/incubating/slack-post-channel/step.yaml b/incubating/slack-post-channel/step.yaml index e4bbade97..ab360ce8e 100644 --- a/incubating/slack-post-channel/step.yaml +++ b/incubating/slack-post-channel/step.yaml @@ -51,7 +51,7 @@ spec: }, "SLACK_IMAGE_VERSION": { "type": "string", - "default": "0.0.6", + "default": "0.0.7", "description": "Version (tag) of the slack-post-channel image to use." }, "SLACK_TOKEN": {