-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add teams notification feature - Enabling push notifications to teams…
… channel (#57) * Added the user_config variables for the Teams notification * Added teams.py, made changes for get and set methods for teams variables and also added exception for teams * changed features.png - added Teams in notification part added teams_plugin.md, made changes in se_flow_and_feature.pptx * Added test documents in the subsequent folder and ran successfully * Added pytest for the changes done --------- Co-authored-by: BharatSahitya <[email protected]>
- Loading branch information
1 parent
ca1ceb5
commit 6d546bb
Showing
13 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,11 @@ | ||
--- | ||
search: | ||
exclude: true | ||
--- | ||
|
||
::: spark_expectations.notifications.plugins.teams | ||
handler: python | ||
options: | ||
filters: | ||
- "!^_[^_]" | ||
- "!^__[^__]" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains 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 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 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 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 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,64 @@ | ||
from typing import Dict, Union | ||
import requests | ||
from spark_expectations import _log | ||
from spark_expectations.notifications.plugins.base_notification import ( | ||
SparkExpectationsNotification, | ||
spark_expectations_notification_impl, | ||
) | ||
from spark_expectations.core.exceptions import ( | ||
SparkExpectationsTeamsNotificationException, | ||
) | ||
from spark_expectations.core.context import SparkExpectationsContext | ||
|
||
|
||
class SparkExpectationsTeamsPluginImpl(SparkExpectationsNotification): | ||
""" | ||
This class implements/supports functionality to send teams notification | ||
""" | ||
|
||
@spark_expectations_notification_impl | ||
def send_notification( | ||
self, | ||
_context: SparkExpectationsContext, | ||
_config_args: Dict[Union[str], Union[str, bool]], | ||
) -> None: | ||
""" | ||
function to send the teams notification for assigned channel | ||
Args: | ||
_context: SparkExpectationsContext class object | ||
_config_args: dict | ||
Returns: None | ||
""" | ||
try: | ||
if _context.get_enable_teams is True: | ||
# payload = {"token": "{token}", "channel": kwargs['channel'], "text": kwargs['message']} | ||
|
||
message = _config_args.get("message") | ||
|
||
# Format Message for Teams | ||
if isinstance(message, str): | ||
message = message.replace("\n", "\n\n").replace(" ", "") | ||
|
||
payload = { | ||
"title": "SE Notification", | ||
"themeColor": "008000", | ||
"text": message, | ||
} | ||
|
||
response = requests.post( | ||
_context.get_teams_webhook_url, json=payload, timeout=10 | ||
) | ||
|
||
# Check the response for success or failure | ||
if response.status_code == 200: | ||
_log.info("Message posted successfully!") | ||
else: | ||
_log.info("Failed to post message") | ||
raise SparkExpectationsTeamsNotificationException( | ||
"error occurred while sending teams notification from spark expectations project" | ||
) | ||
|
||
except Exception as e: | ||
raise SparkExpectationsTeamsNotificationException(e) |
This file contains 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 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 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,65 @@ | ||
from unittest.mock import patch, Mock | ||
import pytest | ||
import requests | ||
from spark_expectations.core.exceptions import SparkExpectationsTeamsNotificationException | ||
from spark_expectations.notifications.plugins.teams import SparkExpectationsTeamsPluginImpl | ||
|
||
|
||
@patch('spark_expectations.notifications.plugins.teams.SparkExpectationsContext', autospec=True, spec_set=True) | ||
def test_send_notification_success(_mock_context): | ||
# Arrange | ||
teams_handler = SparkExpectationsTeamsPluginImpl() | ||
_mock_context.get_enable_teams = True | ||
_mock_context.get_teams_webhook_url = "http://test_webhook_url" | ||
|
||
_config_args = { | ||
"title": "SE Notification", | ||
"themeColor": "008000", "message": "test message"} | ||
|
||
# Mock requests.post to return a response with status code 200 | ||
with patch.object(requests, "post") as mock_post: | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_post.return_value = mock_response | ||
|
||
# Act | ||
teams_handler.send_notification(_context=_mock_context, _config_args=_config_args) | ||
|
||
# Assert | ||
mock_post.assert_called_once_with(_mock_context.get_teams_webhook_url, json={ | ||
"title": "SE Notification", | ||
"themeColor": "008000", "text": "test message"}, timeout=10) | ||
|
||
|
||
@patch('spark_expectations.notifications.plugins.teams.SparkExpectationsContext', autospec=True, spec_set=True) | ||
def test_send_notification_exception(_mock_context): | ||
# Arrange | ||
teams_handler = SparkExpectationsTeamsPluginImpl() | ||
_mock_context.get_enable_teams = True | ||
_mock_context.get_teams_webhook_url = "http://test_webhook_url" | ||
_config_args = {"message": "test message"} | ||
|
||
# Mock requests.post to return a response with status code 404 | ||
with patch.object(requests, "post") as mock_post: | ||
mock_response = Mock() | ||
mock_response.status_code = 404 | ||
mock_post.return_value = mock_response | ||
|
||
# Act and Assert | ||
with pytest.raises(SparkExpectationsTeamsNotificationException): | ||
teams_handler.send_notification(_context=_mock_context, _config_args=_config_args) | ||
|
||
|
||
@patch('spark_expectations.notifications.plugins.teams.SparkExpectationsContext', autospec=True, spec_set=True) | ||
def test_send_notification_teams_disabled(_mock_context): | ||
# Arrange | ||
teams_handler = SparkExpectationsTeamsPluginImpl() | ||
_mock_context.get_enable_teams = False | ||
_mock_context.get_teams_webhook_url = "http://test_webhook_url" | ||
_config_args = {"message": "test message"} | ||
|
||
with patch.object(requests, "post") as mock_post: | ||
# Act | ||
teams_handler.send_notification(_context=_mock_context, _config_args=_config_args) | ||
|
||
mock_post.post.assert_not_called() |
This file contains 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 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