Skip to content

Commit

Permalink
Add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd committed Dec 31, 2024
1 parent 7e9ac42 commit 39dd4dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
1 change: 1 addition & 0 deletions ci/output-results-to-slack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
33 changes: 11 additions & 22 deletions scripts/slack_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -78,21 +64,24 @@ 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,
)

else:
response = client.files_upload_v2(
channel=slack_channel_id,
file=attachment_filename,
title=title,
initial_comment=initial_comment,
)
Expand All @@ -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)

0 comments on commit 39dd4dc

Please sign in to comment.