Repository: WordPress/openverse-api

Issue:
Distinguish between staging & prod when sending data refresh slack updates
## Problem
<!-- Describe a problem solved by this feature; or delete the section entirely. -->
It's not easy to tell based on the slack messages produced by the ingestion server whether a given ingestion is being run in production or staging.

## Description
<!-- Describe the feature and how it solves the problem. -->
We should pre-pend the messages (or change the bot's name) based on which environment the data refresh is running in. 

## Alternatives
<!-- Describe any alternative solutions or features you have considered. How is this feature better? -->
We _could_ turn off messages altogether for staging (how it is currently) but that would make it harder for us to know when a staging data refresh is complete.

## Additional context
<!-- Add any other context about the feature here; or delete the section entirely. -->

## Implementation
<!-- Replace the [ ] with [x] to check the box. -->
- [x] 🙋 I would be interested in implementing this feature.



Discussion:

Resolving patch:
diff --git a/ingestion_server/ingestion_server/slack.py b/ingestion_server/ingestion_server/slack.py
--- a/ingestion_server/ingestion_server/slack.py
+++ b/ingestion_server/ingestion_server/slack.py
@@ -2,6 +2,7 @@
 import os
 
 import requests
+from decouple import config
 
 
 log = logging.getLogger(__name__)
@@ -25,10 +26,12 @@ def message(text: str, summary: str = None) -> None:
         else:
             summary = text
 
+    environment = config("ENVIRONMENT", default="local")
+
     data = {
         "blocks": [{"text": {"text": text, "type": "mrkdwn"}, "type": "section"}],
         "text": summary,
-        "username": "Data Refresh Notification",
+        "username": f"Data Refresh Notification | {environment.upper()}",
         "icon_emoji": "arrows_counterclockwise",
     }
     try:


Test patch:
diff --git a/ingestion_server/test/unit_tests/test_slack.py b/ingestion_server/test/unit_tests/test_slack.py
new file mode 100644
--- /dev/null
+++ b/ingestion_server/test/unit_tests/test_slack.py
@@ -0,0 +1,59 @@
+from unittest import mock
+
+import pytest
+
+from ingestion_server import slack
+
+
+@pytest.mark.parametrize(
+    "text, summary, expected_summary",
+    [
+        # Short text with default summary
+        ("sample text", None, "sample text"),
+        # Short text with explicit summary
+        ("sample text", "different summary", "different summary"),
+        # Multi-line text with default summary
+        ("sample text\nLook a new line!", None, "Ingestion server message"),
+        # Multi-line text with explicit summary
+        ("sample text\nLook a new line!", "different summary", "different summary"),
+    ],
+)
+@pytest.mark.parametrize(
+    "webhook, should_alert",
+    [
+        # Actual webhook supplied
+        ("http://fake", True),
+        # No webhook supplied
+        ("", False),
+    ],
+)
+@pytest.mark.parametrize(
+    "environment",
+    [
+        # Default environment
+        None,
+        # Different, explicit environment
+        "staging",
+    ],
+)
+def test_message(
+    text,
+    summary,
+    webhook,
+    should_alert,
+    expected_summary,
+    environment,
+    monkeypatch,
+):
+    monkeypatch.setenv("ENVIRONMENT", environment)
+    monkeypatch.setenv(slack.SLACK_WEBHOOK, webhook)
+    with mock.patch("requests.post") as mock_post:
+        slack.message(text, summary)
+        assert mock_post.called == should_alert
+        if not should_alert:
+            return
+        data = mock_post.call_args.kwargs["json"]
+        assert data["blocks"][0]["text"]["text"] == text
+        assert data["text"] == expected_summary
+        if environment:
+            assert data["username"].endswith(environment.upper())