Skip to content
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

SendEmailOnSLABreach - Updated the script to support python3 #21032

Closed
wants to merge 8 commits into from
4 changes: 4 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_7_41.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#### Scripts
##### SendEmailOnSLABreach
- Updated the Docker image to: *demisto/python3:3.10.6.33415*.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401

"""
This script is used to send an email about a breached SLA. The script, by default, sends the email to the assignee of the
incident. The email is sent using the send-email command, using all enabled integrations that support the command.
"""


def get_owner_email():
owner_username = demisto.incidents()[0].get("owner")
if owner_username:
try:
owner_info = demisto.executeCommand('getUserByUsername', {"username": owner_username})[0]
owner_email = owner_info.get("EntryContext").get("UserByUsername").get("email")
return owner_email
except Exception as ex:
demisto.results({
"Type": entryTypes["error"],
"ContentsFormat": formats["text"],
"Contents": "Could not retrieve user email. Maybe the user has no associated email to it.\
Error: {}".format(ex)

})
return
else:
demisto.results({
"Type": entryTypes["error"],
"ContentsFormat": formats["text"],
"Contents": "An email can't be sent to the owner of the incident, because no owner was assigned."
})


def get_subject():
incident_name = demisto.incidents()[0].get("name")
incident_id = demisto.incidents()[0].get("id")
subject = "SLA Breached in incident \"{}\" #{}".format(incident_name, incident_id)
return subject


def send_email(to, subject, body):
demisto.results(demisto.executeCommand('send-mail', {
"to": to,
"subject": subject,
"body": body}))


def get_body():
field_name = demisto.args().get("field").get("cliName")
sla = demisto.args().get("fieldValue").get("sla")
start_date = demisto.args().get("fieldValue").get("startDate")
body = "We have detected a breach in your SLA \"{}\".\nThe SLA was set to {} minute and was started on {}.".format(
field_name, sla, start_date.split(".")[0])
return body


def main():
email_to = get_owner_email()
email_subject = get_subject()
email_body = get_body()

if email_to:
send_email(email_to, email_subject, email_body)


if __name__ == "__builtin__" or __name__ == "builtins":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
commonfields:
id: SendEmailOnSLABreach
version: -1
name: SendEmailOnSLABreach
script: ''
type: python
subtype: python3
tags:
- sla
- example
comment: |-
Sends an email informing the user of an SLA breach. The email is sent to the user who is assigned to the incident. It includes the incident name, ID, name of the SLA field that was breached, duration of that SLA field, and the date and time when that SLA was started.
In order to run successfully, the script should be configured to trigger on SLA breach, through field edit mode.
enabled: true
scripttarget: 0
runonce: false
runas: DBotWeakRole
tests:
- No test - Can't test script that triggers on SLA breach. Need a field to trigger it and need a configured mail sender. Also errors aren't accounted.
fromversion: 6.5.0
dockerimage: demisto/python3:3.10.6.33415
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import demistomock as demisto

incident_with_owner = {
'id': 1,
'name': 'incident1',
'owner': 'admin'
}
incident_without_owner = {
'id': 2,
'name': 'incident2',
'owner': ""
}


def test_get_owner_email(mocker):
"""
Given:
- Incident with owner.
When:
- Running get_owner_email function.
Then:
- Validating the return value as expected.
"""
from SendEmailOnSLABreach import get_owner_email

mocker.patch.object(demisto, 'incidents', return_value=[incident_with_owner])
mocker.patch.object(demisto, "executeCommand", return_value=[{'EntryContext':
{'UserByUsername': {'email': "test@gmail.com"}},
'Type': ''}])
results_mock = mocker.patch.object(demisto, 'results')
assert get_owner_email() == "test@gmail.com"
results_mock.assert_not_called()


def test_get_no_email_owner(mocker):
"""
Given:
- Incident without owner.
When:
- Running get_owner_email function.
Then:
- Validating calling to 'demisto.results' once with the right arguments.
"""
from SendEmailOnSLABreach import get_owner_email

mocker.patch.object(demisto, 'incidents', return_value=[incident_without_owner])
demisto_results_mocker = mocker.patch.object(demisto, 'results')
get_owner_email()
demisto_results_mocker.assert_called_once()
results = demisto_results_mocker.call_args[0][0]
assert results == {"Type": 4,
"ContentsFormat": "text",
"Contents": "An email can't be sent to the owner of the incident,"
" because no owner was assigned."}


def test_get_subject(mocker):
"""
Given:
- Incident
When:
- Running get_subject function.
Then:
- Validating the return value as expected.
"""
from SendEmailOnSLABreach import get_subject

mocker.patch.object(demisto, 'incidents', return_value=[incident_with_owner])
excepted_subject = "SLA Breached in incident \"{}\" #{}".format(incident_with_owner['name'],
incident_with_owner['id'])
assert get_subject() == excepted_subject


def test_send_email(mocker):
"""
Given:
- The function's arguments
When:
- Running send_email function.
Then:
- Validating calling to 'demisto.results' once with the right arguments.
"""
from SendEmailOnSLABreach import send_email
mocker.patch.object(demisto, "executeCommand", return_value="send-mail")
results_mock = mocker.patch.object(demisto, 'results')
send_email(to="to_mail_test", subject="subject_test", body="body_test")
results_mock.assert_called_once()
assert results_mock.call_args[0][0] == "send-mail"


def test_get_body(mocker):
"""
Given:
- The function's arguments
When:
- Running get_body function.
Then:
- Validating the return value as expected.
"""
from SendEmailOnSLABreach import get_body
field_name, sla, start_date = "cliNameTest", "slaTest", "2022-09-07T15:10:04.000Z"
args = {'field': {'cliName': "cliNameTest"}, 'fieldValue': {"sla": "slaTest",
"startDate": "2022-09-07T15:10:04.000Z"}}
mocker.patch.object(demisto, 'args', return_value=args)
excepted_body = "We have detected a breach in your SLA \"{}\".\nThe SLA was set to {} minute and was started on {}." \
.format(field_name, sla, start_date.split(".")[0])
assert get_body() == excepted_body
85 changes: 0 additions & 85 deletions Packs/CommonScripts/Scripts/script-SendEmailOnSLABreach.yml

This file was deleted.