From 86c4206db30cdf7d2754abd553a503aa8ff4e8b0 Mon Sep 17 00:00:00 2001 From: Daniel Moreno Date: Fri, 15 Oct 2021 15:40:28 +0200 Subject: [PATCH] feat(core): add slack integration --- giges/settings.py | 6 +++++- giges/slack.py | 25 +++++++++++++++++++++++++ requirements.in | 1 + requirements.txt | 41 +++++++++++++++++++++++------------------ 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 giges/slack.py diff --git a/giges/settings.py b/giges/settings.py index 5bf814d..8915636 100644 --- a/giges/settings.py +++ b/giges/settings.py @@ -18,6 +18,8 @@ class BaseSettings: SENTRY_URI = "" ASANA_TOKEN = os.environ.get("ASANA_TOKEN", "") ASANA_WORKSPACE = "1199978051314275" + SLACK_TOKEN = os.environ.get("SLACK_TOKEN", "") + SLACK_BLOCKS_CHANNEL = "" class ProductionSettings(BaseSettings): @@ -25,13 +27,14 @@ class ProductionSettings(BaseSettings): SERVER_BASE_URI = "https://integrations.tesselo.com" SENTRY_URI = "https://ec9a91e1ce0e40f59388c665c092dc2a@o640190.ingest.sentry.io/5911249" # noqa: E501 + SLACK_BLOCKS_CHANNEL = "C02GXT5UY22" class StagingSettings(BaseSettings): ENVIRONMENT = "staging" - ASANA_WORKSPACE = "1199978051314275" SERVER_BASE_URI = "https://integrations-staging.tesselo.com" + SLACK_BLOCKS_CHANNEL = "C021VUWGA3E" # dev-null class DevelopmentSettings(BaseSettings): @@ -41,6 +44,7 @@ class DevelopmentSettings(BaseSettings): SQLALCHEMY_DATABASE_URI = os.getenv("GIGES_DATABASE_URI") or ( "postgresql://postgres@localhost:5432/giges_dev" ) + SLACK_BLOCKS_CHANNEL = "C021VUWGA3E" # dev-null class TestingSettings(BaseSettings): diff --git a/giges/slack.py b/giges/slack.py new file mode 100644 index 0000000..59f1962 --- /dev/null +++ b/giges/slack.py @@ -0,0 +1,25 @@ +from typing import Any + +import structlog +from flask import current_app +from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError +from slack_sdk.web import SlackResponse + +logger = structlog.get_logger(__file__) + + +class SlackClient(WebClient): + def __init__(self, **kwargs: Any): + super().__init__(token=current_app.config["SLACK_TOKEN"], **kwargs) + + def send_message(self, channel: str, message: str) -> SlackResponse: + try: + return self.chat_postMessage(channel=channel, text=message) + except SlackApiError as e: + logger.error("Failed to send a message to slack", error=e) + + def send_to_road_blocks(self, message: str) -> SlackResponse: + return self.send_message( + current_app.config["SLACK_BLOCKS_CHANNEL"], message + ) diff --git a/requirements.in b/requirements.in index 2799659..ac93cb6 100644 --- a/requirements.in +++ b/requirements.in @@ -5,6 +5,7 @@ flask-sqlalchemy psycopg2-binary zappa sentry-sdk[flask] +slack-sdk structlog # All the package restrictions are due to zappa :( diff --git a/requirements.txt b/requirements.txt index d2630c7..f30c1d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ # # pip-compile --output-file=./requirements.txt requirements.in # -alembic==1.7.1 +alembic==1.7.4 # via flask-migrate argcomplete==1.12.3 # via zappa @@ -14,21 +14,21 @@ attrs==21.2.0 # via jsonschema blinker==1.4 # via sentry-sdk -boto3==1.18.32 +boto3==1.18.59 # via # kappa # zappa -botocore==1.21.32 +botocore==1.21.59 # via # boto3 # s3transfer -certifi==2021.5.30 +certifi==2021.10.8 # via # requests # sentry-sdk -cfn-flip==1.2.3 +cfn-flip==1.3.0 # via troposphere -charset-normalizer==2.0.4 +charset-normalizer==2.0.7 # via requests click==7.1.2 # via @@ -60,12 +60,14 @@ flask-sqlalchemy==2.5.1 # flask-migrate future==0.18.2 # via zappa -greenlet==1.1.1 +greenlet==1.1.2 # via sqlalchemy hjson==3.0.2 # via zappa idna==3.2 # via requests +importlib-metadata==4.8.1 + # via alembic importlib-resources==5.2.2 # via alembic inflection==0.5.1 @@ -83,7 +85,7 @@ jmespath==0.10.0 # boto3 # botocore # zappa -jsonschema==3.2.0 +jsonschema==4.1.0 # via # connexion # openapi-schema-validator @@ -104,9 +106,9 @@ openapi-spec-validator==0.3.1 # via connexion pep517==0.11.0 # via pip-tools -pip-tools==6.2.0 +pip-tools==6.3.1 # via zappa -placebo==0.9.0 +placebo==0.10.0 # via kappa psycopg2-binary==2.9.1 # via -r requirements.in @@ -136,25 +138,26 @@ requests-oauthlib==1.3.0 # via asana s3transfer==0.5.0 # via boto3 -sentry-sdk[flask]==1.3.1 +sentry-sdk[flask]==1.4.3 # via -r requirements.in six==1.16.0 # via # asana # cfn-flip - # jsonschema # openapi-schema-validator # openapi-spec-validator # python-dateutil # zappa -sqlalchemy==1.4.23 +slack-sdk==3.11.2 + # via -r requirements.in +sqlalchemy==1.4.25 # via # alembic # dictalchemy # flask-sqlalchemy structlog==21.1.0 # via -r requirements.in -swagger-ui-bundle==0.0.8 +swagger-ui-bundle==0.0.9 # via connexion text-unidecode==1.3 # via python-slugify @@ -162,13 +165,13 @@ toml==0.10.2 # via zappa tomli==1.2.1 # via pep517 -tqdm==4.62.2 +tqdm==4.62.3 # via zappa troposphere==2.7.1 # via # -r requirements.in # zappa -urllib3==1.26.6 +urllib3==1.26.7 # via # botocore # requests @@ -185,8 +188,10 @@ wsgi-request-logger==0.4.6 # via zappa zappa==0.53.0 # via -r requirements.in -zipp==3.5.0 - # via importlib-resources +zipp==3.6.0 + # via + # importlib-metadata + # importlib-resources # The following packages are considered to be unsafe in a requirements file: # pip