Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Update Slack messages to include environment (#382)

Staci Cooperf3808d85b7fa
diff --git a/openverse_catalog/dags/common/slack.py b/openverse_catalog/dags/common/slack.py
index 9ea92d472..c919d954e 100644
--- a/openverse_catalog/dags/common/slack.py
+++ b/openverse_catalog/dags/common/slack.py
@@ -224,7 +224,11 @@ def send_message(
"""Send a simple slack message, convenience message for short/simple messages."""
if not should_send_message(http_conn_id):
return
- s = SlackMessage(username, icon_emoji, http_conn_id=http_conn_id)
+
+ environment = Variable.get("environment", default_var="dev")
+ s = SlackMessage(
+ f"{username} | {environment}", icon_emoji, http_conn_id=http_conn_id
+ )
s.add_text(text, plain_text=not markdown)
s.send(text)
diff --git a/tests/dags/common/test_slack.py b/tests/dags/common/test_slack.py
index 7583a6ab9..c11311a24 100644
--- a/tests/dags/common/test_slack.py
+++ b/tests/dags/common/test_slack.py
@@ -305,12 +305,16 @@ def test_should_send_message_is_false_without_hook(http_hook_mock):
assert not should_send_message()
-def test_send_message(http_hook_mock):
- with mock.patch("common.slack.should_send_message", return_value=True):
[email protected]("environment", ["dev", "prod"])
+def test_send_message(environment, http_hook_mock):
+ with mock.patch("common.slack.should_send_message", return_value=True), mock.patch(
+ "common.slack.Variable"
+ ) as MockVariable:
+ MockVariable.get.side_effect = [environment]
send_message("Sample text", username="DifferentUser")
http_hook_mock.run.assert_called_with(
endpoint=None,
- data='{"username": "DifferentUser", "unfurl_links": true, "unfurl_media": true,'
+ data=f'{{"username": "DifferentUser | {environment}", "unfurl_links": true, "unfurl_media": true,'
' "icon_emoji": ":airflow:", "blocks": [{"type": "section", "text": '
'{"type": "mrkdwn", "text": "Sample text"}}], "text": "Sample text"}',
headers={"Content-type": "application/json"},
@@ -352,10 +356,18 @@ def test_on_failure_callback(
"execution_date": datetime.now(),
"exception": exception,
}
+ env_vars = {
+ "environment": environment,
+ "slack_message_override": slack_message_override,
+ }
+
+ # Mock env variables
+ def environment_vars_mock(value, **kwargs):
+ return env_vars[value]
+
with mock.patch("common.slack.Variable") as MockVariable:
run_mock = http_hook_mock.run
- # Mock the calls to Variable.get, in order
- MockVariable.get.side_effect = [environment, slack_message_override]
+ MockVariable.get.side_effect = environment_vars_mock
on_failure_callback(context)
assert run_mock.called == call_expected
if call_expected: