Skip to content

Commit

Permalink
DEVPROD-1877: Add ability to send emails (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
zamj authored Nov 14, 2023
1 parent 31839e4 commit c87879f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.6.16 - 2023-11-14

- Added support for sending emails.

## 3.6.15

- Added support for binary and nonbinary artifact streams.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "evergreen.py"
version = "3.6.15"
version = "3.6.16"
description = "Python client for the Evergreen API"
authors = [
"Dev Prod DAG <[email protected]>",
Expand Down
37 changes: 37 additions & 0 deletions src/evergreen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,43 @@ def send_slack_message(
url, data=json.dumps(data), method="POST",
)

def send_email(
self,
recipients: List[str],
sender: Optional[str] = None,
subject: Optional[str] = None,
body: Optional[str] = None,
is_plain_text: Optional[bool] = None,
headers: Optional[Dict[str, List[str]]] = None,
) -> None:
"""
Send an email to a user.
:param recipients: Who to send the email to.
:param sender: Who the email should be sent on behalf of.
:param subject: The subject of the email to send.
:param body: What should be in the body of the email.
:param is_plain_text: If the email is in plain text or not. If true, will be text/plain. text/html otherwise.
:param headers: What email headers to attach.
"""
url = self._create_url("/notifications/email")
data: Dict[str, Any] = {
"recipients": recipients,
}
if sender is not None:
data["sender"] = sender
if subject is not None:
data["subject"] = subject
if body is not None:
data["body"] = body
if is_plain_text is not None:
data["is_plain_text"] = is_plain_text
if headers is not None:
data["headers"] = headers
self._call_api(
url, data=json.dumps(data), method="POST",
)

def alias_for_version(
self, version_id: str, alias: str, include_deps: bool = False
) -> List[VariantAlias]:
Expand Down
11 changes: 11 additions & 0 deletions tests/evergreen/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ def test_single_test_by_task(self, mocked_api):
method="GET",
)

def test_email(self, mocked_api):
recipients = ["@fake-user"]
body = "I'm a fake message"
subject = "I'm a fake subject"
mocked_api.send_email(recipients=recipients, subject=subject, body=body)
expected_url = mocked_api._create_url("/notifications/email")
expected_data = json.dumps({"recipients": recipients, "subject": subject, "body": body})
mocked_api.session.request.assert_called_with(
url=expected_url, timeout=None, data=expected_data, method="POST", params=None
)

def test_send_slack_message(self, mocked_api):
target = "@fake-user"
msg = "I'm a fake message"
Expand Down

0 comments on commit c87879f

Please sign in to comment.