Repository: PrefectHQ/prefect

Issue:
Make the secret name for slack_notifier customizable
We recently made the slack web hook url secret name customizable, we should do the same for slack_notifier


Discussion:
We should extend this to all tasks in the task library
I'm a big dummy -> this is already customizable via the `webhook_url` keyword argument to the `slack_notifier`.  However, I think for consistency we should update this kwarg to specify a secret name instead of the URL directly.

Resolving patch:
diff --git a/src/prefect/utilities/notifications.py b/src/prefect/utilities/notifications.py
--- a/src/prefect/utilities/notifications.py
+++ b/src/prefect/utilities/notifications.py
@@ -227,7 +227,7 @@ def slack_notifier(
     new_state: "prefect.engine.state.State",
     ignore_states: list = None,
     only_states: list = None,
-    webhook_url: str = None,
+    webhook_secret: str = None,
 ) -> "prefect.engine.state.State":
     """
     Slack state change handler; requires having the Prefect slack app installed.
@@ -243,8 +243,8 @@ def slack_notifier(
             e.g., `[Running, Scheduled]`. If `new_state` is an instance of one of the passed states, no notification will occur.
         - only_states ([State], optional): similar to `ignore_states`, but
             instead _only_ notifies you if the Task / Flow is in a state from the provided list of `State` classes
-        - webhook_url (str, optional): the Prefect slack app webhook URL; if not
-            provided, will attempt to use your `"SLACK_WEBHOOK_URL"` Prefect Secret
+        - webhook_secret (str, optional): the name of the Prefect Secret which stores your slack webhook URL;
+            defaults to `"SLACK_WEBHOOK_URL"`
 
     Returns:
         - State: the `new_state` object which was provided
@@ -263,7 +263,7 @@ def add(x, y):
         ```
     """
     webhook_url = cast(
-        str, webhook_url or prefect.client.Secret("SLACK_WEBHOOK_URL").get()
+        str, prefect.client.Secret(webhook_secret or "SLACK_WEBHOOK_URL").get()
     )
     ignore_states = ignore_states or []
     only_states = only_states or []


Test patch:
diff --git a/tests/utilities/test_notifications.py b/tests/utilities/test_notifications.py
--- a/tests/utilities/test_notifications.py
+++ b/tests/utilities/test_notifications.py
@@ -162,6 +162,27 @@ def test_slack_notifier_returns_new_state_and_old_state_is_ignored(monkeypatch):
             assert slack_notifier(Task(), "", new_state) is new_state
 
 
+def test_slack_notifier_pulls_url_from_secret(monkeypatch):
+    post = MagicMock(ok=True)
+    monkeypatch.setattr("prefect.utilities.notifications.requests.post", post)
+    state = Failed(message="1", result=0)
+    with set_temporary_config({"cloud.use_local_secrets": True}):
+        with pytest.raises(ValueError) as exc:
+            slack_notifier(Task(), "", state)
+
+        assert "SLACK_WEBHOOK_URL" in str(exc.value)
+
+        with prefect.context(secrets=dict(SLACK_WEBHOOK_URL="https://foo/bar")):
+            slack_notifier(Task(), "", state)
+
+        assert post.call_args[0][0] == "https://foo/bar"
+
+        with prefect.context(secrets=dict(TOP_SECRET='"42"')):
+            slack_notifier(Task(), "", state, webhook_secret="TOP_SECRET")
+
+        assert post.call_args[0][0] == "42"
+
+
 def test_slack_notifier_ignores_ignore_states(monkeypatch):
     all_states = [
         Running,