-
Notifications
You must be signed in to change notification settings - Fork 16.5k
Add DiscordNotifier
#31273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eladkal
merged 19 commits into
apache:main
from
vargacypher:31238-add-discord-notifications
Jun 16, 2023
Merged
Add DiscordNotifier
#31273
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fb55b0b
Add discord notifications
765c42d
Merge branch 'main' into 31238-add-discord-notifications
vargacypher bed89b5
Merge branch 'main' into 31238-add-discord-notifications
vargacypher b14806e
Add notifications to providers/ Fix tests / Rename file
9c79b38
Merge branch 'apache:main' into 31238-add-discord-notifications
vargacypher 4089b9d
Merge branch 'main' into 31238-add-discord-notifications
vargacypher 834ca5a
Fix conn_type
72dcce7
Update airflow/providers/discord/notifications/discord.py
vargacypher 0a9a421
Update airflow/providers/discord/notifications/discord.py
vargacypher 2350b86
Update airflow/providers/discord/notifications/discord.py
vargacypher dec03d2
Merge branch 'main' into 31238-add-discord-notifications
eladkal f09952d
Merge branch 'main' into 31238-add-discord-notifications
vargacypher 6486eaf
Update functools import
vargacypher 3803dac
Update discord.py
vargacypher 8a48000
Merge branch 'main' into 31238-add-discord-notifications
vargacypher bde9836
Merge branch 'main' into 31238-add-discord-notifications
vargacypher f3a18bf
Update discord.py
vargacypher f5a171d
Update notifier png
vargacypher 27d2e0e
Merge branch 'main' into 31238-add-discord-notifications
vargacypher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| from functools import cached_property | ||
|
|
||
| from airflow.exceptions import AirflowOptionalProviderFeatureException | ||
|
|
||
| try: | ||
| from airflow.notifications.basenotifier import BaseNotifier | ||
| except ImportError: | ||
| raise AirflowOptionalProviderFeatureException( | ||
| "Failed to import BaseNotifier. This feature is only available in Airflow versions >= 2.6.0" | ||
| ) | ||
|
|
||
| from airflow.providers.discord.hooks.discord_webhook import DiscordWebhookHook | ||
|
|
||
| ICON_URL: str = "https://raw.githubusercontent.com/apache/airflow/main/airflow/www/static/pin_100.png" | ||
|
|
||
|
|
||
| class DiscordNotifier(BaseNotifier): | ||
| """ | ||
| Discord BaseNotifier. | ||
|
|
||
| :param discord_conn_id: Http connection ID with host as "https://discord.com/api/" and | ||
| default webhook endpoint in the extra field in the form of | ||
| {"webhook_endpoint": "webhooks/{webhook.id}/{webhook.token}"} | ||
| :param text: The content of the message | ||
| :param username: The username to send the message as. Optional | ||
| :param avatar_url: The URL of the avatar to use for the message. Optional | ||
| :param tts: Text to speech. | ||
| """ | ||
|
|
||
| # A property that specifies the attributes that can be templated. | ||
| template_fields = ("discord_conn_id", "text", "username", "avatar_url", "tts") | ||
|
|
||
| def __init__( | ||
| self, | ||
| discord_conn_id: str = "discord_webhook_default", | ||
| text: str = "This is a default message", | ||
| username: str = "Airflow", | ||
| avatar_url: str = ICON_URL, | ||
| tts: bool = False, | ||
| ): | ||
| super().__init__() | ||
| self.discord_conn_id = discord_conn_id | ||
| self.text = text | ||
| self.username = username | ||
| self.avatar_url = avatar_url | ||
|
|
||
| # If you're having problems with tts not being recognized in __init__(), | ||
| # you can define that after instantiating the class | ||
| self.tts = tts | ||
|
|
||
| @cached_property | ||
| def hook(self) -> DiscordWebhookHook: | ||
| """Discord Webhook Hook.""" | ||
| return DiscordWebhookHook(http_conn_id=self.discord_conn_id) | ||
|
|
||
| def notify(self, context): | ||
| """Send a message to a Discord channel.""" | ||
| self.hook.username = self.username | ||
| self.hook.message = self.text | ||
| self.hook.avatar_url = self.avatar_url | ||
| self.hook.tts = self.tts | ||
|
|
||
| self.hook.execute() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow.models import Connection | ||
| from airflow.providers.discord.notifications.discord import DiscordNotifier | ||
| from airflow.utils import db | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def setup(): | ||
| db.merge_conn( | ||
| Connection( | ||
| conn_id="my_discord_conn_id", | ||
| conn_type="discord", | ||
| host="https://discordapp.com/api/", | ||
| extra='{"webhook_endpoint": "webhooks/00000/some-discord-token_000"}', | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @patch("airflow.providers.discord.notifications.discord.DiscordWebhookHook.execute") | ||
| def test_discord_notifier_notify(mock_execute): | ||
| notifier = DiscordNotifier( | ||
| discord_conn_id="my_discord_conn_id", | ||
| text="This is a test message", | ||
| username="test_user", | ||
| avatar_url="https://example.com/avatar.png", | ||
| tts=False, | ||
| ) | ||
| context = MagicMock() | ||
|
|
||
| notifier.notify(context) | ||
|
|
||
| mock_execute.assert_called_once() | ||
| assert notifier.hook.username == "test_user" | ||
| assert notifier.hook.message == "This is a test message" | ||
| assert notifier.hook.avatar_url == "https://example.com/avatar.png" | ||
| assert notifier.hook.tts is False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.