Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

u #48

Merged
merged 1 commit into from
Oct 22, 2024
Merged

u #48

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion frontend/amundsen_application/api/file_upload/v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def complete_multipart_upload():
}
)

message = f"*{app.config['HOST_ID']} - Metadata File Upload*\n{file_name}"
_send_notification(message=message)

return make_response(jsonify(response), HTTPStatus.OK)

except Exception as e:
Expand All @@ -112,4 +115,11 @@ def _get_s3_client():
return s3_client

def _get_s3_file_key(file_name: str) -> str:
return f'upload/{file_name}'
return f'upload/{file_name}'

def _send_notification(message: str):
if app.config.get('SLACK_CLIENT'):
try:
app.config['SLACK_CLIENT'].post(channel=app.config['SLACK_SUPPORT_CHANNEL'], message=message)
except Exception as e:
LOGGER.exception(f'Failed to send Slack Notification')
46 changes: 46 additions & 0 deletions frontend/amundsen_application/client/slack/slack_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# https://slack.dev/python-slack-sdk/web/index.html#messaging
# https://pandeynandancse.medium.com/slack-send-attachments-using-python-webclient-baa39974ceea
from pathlib import Path
import logging

from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError


LOGGER = logging.getLogger(__name__)


class SlackClient():
def __init__(self,
token: str) -> None:

self.client = WebClient(token=token)


def post(self,
channel: str,
message: str):
try:
response = self.client.chat_postMessage(
channel=channel,
text=message
)
except SlackApiError as e:
LOGGER.exception(e)
raise e

def upload(self,
channel: str, # Must use the channel ID not the name
file_path: str,
title: str = None,
comment: str = None):
try:
response = self.client.files_upload_v2(
channel=channel,
file=file_path,
title=title,
initial_comment=comment
)
except SlackApiError as e:
LOGGER.exception(e)
raise e
8 changes: 8 additions & 0 deletions frontend/amundsen_application/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from amundsen_application.tests.test_utils import get_test_user

from amundsen_application.client.slack.slack_client import SlackClient


class MatchRuleObject:
def __init__(self,
Expand Down Expand Up @@ -157,6 +159,12 @@ class Config:
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')

SLACK_TOKEN = os.getenv('SLACK_TOKEN')
SLACK_SUPPORT_CHANNEL = os.getenv('SLACK_SUPPORT_CHANNEL')

if SLACK_TOKEN:
SLACK_CLIENT = SlackClient(token=SLACK_TOKEN)


class LocalConfig(Config):
DEBUG = False
Expand Down
Loading