diff --git a/ci/output-results-to-slack.yaml b/ci/output-results-to-slack.yaml index 034c802..f5bb377 100644 --- a/ci/output-results-to-slack.yaml +++ b/ci/output-results-to-slack.yaml @@ -10,6 +10,7 @@ params: OUTPUT_BUCKET: OUTPUT_DIR: SLACK_CHANNEL_NAME: + SLACK_CHANNEL_ID: NUMBER_OF_DAYS: SERVICE_ACCOUNT_JSON: ((gcp.service_account_json)) SLACK_AUTH_TOKEN: ((eq-ons-slack-app.bot_user_oauth_token)) diff --git a/scripts/slack_notification.py b/scripts/slack_notification.py index 3301a8f..4956372 100644 --- a/scripts/slack_notification.py +++ b/scripts/slack_notification.py @@ -17,6 +17,11 @@ def parse_environment_variables(): print("'SLACK_CHANNEL_NAME' environment variable must be provided") sys.exit(1) + slack_channel_id = os.getenv("SLACK_CHANNEL_ID") + if not slack_channel_id: + print("'SLACK_CHANNEL_ID' environment variable must be provided") + sys.exit(1) + content = os.getenv("CONTENT") attachment_filename = os.getenv("ATTACHMENT_FILENAME") if content and attachment_filename: @@ -42,6 +47,7 @@ def parse_environment_variables(): return { "slack_auth_token": slack_auth_token, "slack_channel": slack_channel, + "slack_channel_id": slack_channel_id, "content": content, "attachment_filename": attachment_filename, "file_type": file_type, @@ -50,26 +56,6 @@ def parse_environment_variables(): } -def get_channel_id(client, channel_name): - try: - conversation_data = client.conversations_list() - if not conversation_data.get("ok", False): - print("Failed to fetch channels") - sys.exit(2) - - channels = conversation_data.get("channels", []) - for channel in channels: - if channel.get("name") == channel_name: - return channel.get("id") - - print(f"Channel: '{channel_name}' not found") - sys.exit(1) - - except SlackApiError as e: - print(f'Error fetching channel list \nError: {e.response["error"]}') - sys.exit(2) - - def post_slack_notification( slack_auth_token, slack_channel, @@ -78,14 +64,16 @@ def post_slack_notification( file_type, initial_comment, title, + slack_channel_id, ): client = slack.WebClient(token=slack_auth_token) - slack_channel_id = get_channel_id(client, slack_channel) + try: if content: response = client.files_upload_v2( channel=slack_channel_id, content=content, + filetype=file_type, title=title, initial_comment=initial_comment, ) @@ -93,6 +81,7 @@ def post_slack_notification( else: response = client.files_upload_v2( channel=slack_channel_id, + file=attachment_filename, title=title, initial_comment=initial_comment, ) @@ -113,4 +102,4 @@ def post_slack_notification( warnings.simplefilter("ignore", category=RuntimeWarning) parsed_variables = parse_environment_variables() - post_slack_notification(**parsed_variables) + post_slack_notification(**parsed_variables) \ No newline at end of file