Skip to content
Merged
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
6 changes: 6 additions & 0 deletions airflow/providers/slack/notifications/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(
proxy: str | None = None,
timeout: int | None = None,
retry_handlers: list[RetryHandler] | None = None,
unfurl_links: bool = True,
unfurl_media: bool = True,
):
super().__init__()
self.slack_conn_id = slack_conn_id
Expand All @@ -77,6 +79,8 @@ def __init__(
self.timeout = timeout
self.proxy = proxy
self.retry_handlers = retry_handlers
self.unfurl_links = unfurl_links
self.unfurl_media = unfurl_media

@cached_property
def hook(self) -> SlackHook:
Expand All @@ -98,6 +102,8 @@ def notify(self, context):
"icon_url": self.icon_url,
"attachments": json.dumps(self.attachments),
"blocks": json.dumps(self.blocks),
"unfurl_links": self.unfurl_links,
"unfurl_media": self.unfurl_media,
}
self.hook.call("chat.postMessage", json=api_call_params)

Expand Down
32 changes: 32 additions & 0 deletions tests/providers/slack/notifications/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def test_slack_notifier(self, mock_slack_hook, dag_maker, extra_kwargs, hook_ext
"/pin_100.png",
"attachments": "[]",
"blocks": "[]",
"unfurl_links": True,
"unfurl_media": True,
},
)
mock_slack_hook.assert_called_once_with(slack_conn_id="test_conn_id", **hook_extra_kwargs)
Expand All @@ -89,6 +91,8 @@ def test_slack_notifier_with_notifier_class(self, mock_slack_hook, dag_maker):
"/pin_100.png",
"attachments": "[]",
"blocks": "[]",
"unfurl_links": True,
"unfurl_media": True,
},
)

Expand All @@ -114,5 +118,33 @@ def test_slack_notifier_templated(self, mock_slack_hook, dag_maker):
"/pin_100.png",
"attachments": '[{"image_url": "test_slack_notifier.png"}]',
"blocks": "[]",
"unfurl_links": True,
"unfurl_media": True,
},
)

@mock.patch("airflow.providers.slack.notifications.slack.SlackHook")
def test_slack_notifier_unfurl_options(self, mock_slack_hook, dag_maker):
with dag_maker("test_slack_notifier_unfurl_options") as dag:
EmptyOperator(task_id="task1")

notifier = send_slack_notification(
text="test",
unfurl_links=False,
unfurl_media=False,
)
notifier({"dag": dag})
mock_slack_hook.return_value.call.assert_called_once_with(
"chat.postMessage",
json={
"channel": "#general",
"username": "Airflow",
"text": "test",
"icon_url": "https://raw.githubusercontent.com/apache/airflow/2.5.0/airflow/www/static"
"/pin_100.png",
"attachments": "[]",
"blocks": "[]",
"unfurl_links": False,
"unfurl_media": False,
},
)