Skip to content

Commit

Permalink
feat(slack): add unfurl options to slack notifier (#40694)
Browse files Browse the repository at this point in the history
  • Loading branch information
armand-sauzay authored Jul 10, 2024
1 parent b17a9fd commit c25858c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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,
},
)

0 comments on commit c25858c

Please sign in to comment.