integrations: Add Freshstatus webhook integration.

This commit is contained in:
Adam Birds 2021-04-13 00:22:41 +00:00 committed by Tim Abbott
parent 7999d52400
commit ee375e2999
21 changed files with 765 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -375,6 +375,9 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [
WebhookIntegration("flock", ["customer-support"], display_name="Flock"),
WebhookIntegration("freshdesk", ["customer-support"]),
WebhookIntegration("freshping", ["monitoring"], display_name="Freshping"),
WebhookIntegration(
"freshstatus", ["monitoring", "customer-support"], display_name="Freshstatus"
),
WebhookIntegration("front", ["customer-support"], display_name="Front"),
WebhookIntegration("gitea", ["version-control"], stream_name="commits"),
WebhookIntegration(
@ -719,6 +722,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[BaseScreenshotConfig]] = {
ScreenshotConfig("ticket_created.json", image_name="004.png", use_basic_auth=True)
],
"freshping": [ScreenshotConfig("freshping_check_unreachable.json")],
"freshstatus": [ScreenshotConfig("freshstatus_incident_open.json")],
"front": [ScreenshotConfig("inbound_message.json")],
"gci": [ScreenshotConfig("task_abandoned_by_student.json")],
"gitea": [ScreenshotConfig("pull_request__merged.json")],

View File

View File

@ -0,0 +1,79 @@
Receive Freshstatus notifications in Zulip!
1. {!create-stream.md!}
1. {!create-bot-construct-url-indented.md!}
1. On your Freshstatus dashboard, click **Settings** and click **Integrations**.
Go to **Webhooks** and click **Manage**. Click **New Webhook**
to set up a new webhook.
1. Set **Webhook Name** to any name of your choice, such as `Zulip`.
Set **Description** to any suitable description.
1. Under **Condition**, select the events you would like to be notified for.
Under **Action**, set **Request Type** to `POST`. Set **Callback URL**
to the **URL** constructed above.
1. Leave **Requires Authentication** unchecked and set **Content** to
**Advanced**.
1. Set **Incident JSON** to the following:
```
{
"id": "{id}",
"title": "{title}",
"description": "{description}",
"start_time": "{start_time}",
"end_time": "{end_time}",
"is_private": "{is_private}",
"source": "{source}",
"affected_services": "{affected_services}",
"notification_options": "{notification_options}"
}
```
1. Set **Maintenance JSON** to the following:
```
{
"id": "{id}",
"title": "{title}",
"description": "{description}",
"start_time": "{start_time}",
"end_time": "{end_time}",
"is_private": "{is_private}",
"source": "{source}",
"affected_services": "{affected_services}",
"notification_options": "{notification_options}",
"scheduled_start_time": "{scheduled_start_time}",
"scheduled_end_time": "{scheduled_end_time}",
"is_auto_update_status_on_scheduled_start": "{is_auto_update_status_on_scheduled_start}",
"is_auto_update_status_on_scheduled_end": "{is_auto_update_status_on_scheduled_end}",
"is_auto_update_component_status_on_scheduled_start": "{is_auto_update_component_status_on_scheduled_start}",
"is_auto_update_component_status_on_scheduled_end": "{is_auto_update_component_status_on_scheduled_end}"
}
```
1. Set **Incident/Maintenance Note JSON** to the following:
```
{
"id": "{note_id}",
"title": "{title}",
"incident_id": "{note_incident_id}",
"incident_status": "{note_incident_status}",
"message": "{note_message}",
"status": "{note_status}",
"is_private": "{note_is_private}",
"notification_options": "{note_notification_options}"
}
```
1. Finally, click **Save**. Optionally, you may click **Test** to check if
the webhook was configured correctly.
{!congrats.md!}
![](/static/images/integrations/freshstatus/001.png)

View File

@ -0,0 +1,19 @@
{
"id": "1",
"title": "Sample title",
"source": "",
"end_time": "None",
"is_private": "False",
"start_time": "2021-04-12 16:53:00.080999+00:00",
"description": "Sample description",
"affected_services": "'service 1','service 2','service 3'",
"notification_options": "{notification_options}",
"event_data": {
"webhook_id": 102,
"account_id": 25868,
"account_name": "example",
"event_id": 1364,
"event_type": "INCIDENT_REOPEN",
"event_created_on": "2021-04-12 16:53:00.081344+00:00"
}
}

View File

@ -0,0 +1,21 @@
{
"id": "130594",
"title": "Degradation of Database Server",
"status": "",
"message": "The incident has been resolved.",
"is_private": "False",
"incident_id": "53482",
"incident_status": "Closed",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"event_data": {
"webhook_id": 102,
"account_id": 25868,
"account_name": "example",
"event_id": 1370,
"event_type": "INCIDENT_NOTE_CREATE",
"event_created_on": "2021-04-12 16:55:58.663440+00:00"
}
}

View File

@ -0,0 +1,21 @@
{
"id": "130594",
"title": "Degradation of Database Server",
"status": "",
"message": "The incident is being worked on.",
"is_private": "False",
"incident_id": "53482",
"incident_status": "Open",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"event_data": {
"webhook_id": 102,
"account_id": 25868,
"account_name": "example",
"event_id": 1370,
"event_type": "INCIDENT_NOTE_CREATE",
"event_created_on": "2021-04-12 16:55:58.663440+00:00"
}
}

View File

@ -0,0 +1,23 @@
{
"id": "53481",
"title": "Degradation of Database Server",
"source": "Monitoring System",
"end_time": "None",
"is_private": "False",
"start_time": "2021-04-12 16:29:00+00:00",
"description": "This issue is being investigated.",
"affected_services": "Database Server",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"status": "Open",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1361,
"event_type": "INCIDENT_OPEN",
"event_created_on": "2021-04-12 16:30:43.392582+00:00"
}
}

View File

@ -0,0 +1,23 @@
{
"id": "53481",
"title": "Degradation of Multiple Servers",
"source": "Monitoring System",
"end_time": "None",
"is_private": "False",
"start_time": "2021-04-12 16:29:00+00:00",
"description": "This issue is being investigated.",
"affected_services": "Database Server,Web Server,Web Server 2",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"status": "Open",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1361,
"event_type": "INCIDENT_OPEN",
"event_created_on": "2021-04-12 16:30:43.392582+00:00"
}
}

View File

@ -0,0 +1,23 @@
{
"id": "53481",
"title": "Degradation of Multiple Servers",
"source": "Monitoring System",
"end_time": "None",
"is_private": "False",
"start_time": "2021-04-12 16:29:00+00:00",
"description": "This issue is being investigated.",
"affected_services": "Database Server,Web Server,Web Server 2,Database Server 2,Active Directory Server,Exchange Server,Web Server 3",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"status": "Open",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1361,
"event_type": "INCIDENT_OPEN",
"event_created_on": "2021-04-12 16:30:43.392582+00:00"
}
}

View File

@ -0,0 +1,19 @@
{
"id": "130610",
"status": "",
"message": "The maintenance is now complete.",
"is_private": "False",
"incident_id": "53492",
"incident_status": "Closed",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"event_data": {
"webhook_id": 102,
"account_id": 25868,
"account_name": "adbwebdesigns",
"event_id": 1379,
"event_created_on": "2021-04-12 17:09:35.727448+00:00"
}
}

View File

@ -0,0 +1,21 @@
{
"id": "130610",
"title": "Scheduled Maintenance Test",
"status": "",
"message": "The maintenance is now complete.",
"is_private": "False",
"incident_id": "53492",
"incident_status": "Closed",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1379,
"event_type": "MAINTENANCE_NOTE_CREATE",
"event_created_on": "2021-04-12 17:09:35.727448+00:00"
}
}

View File

@ -0,0 +1,21 @@
{
"id": "130632",
"title": "Scheduled Maintenance Test",
"status": "",
"message": "We are about to start the maintenance.",
"is_private": "False",
"incident_id": "53490",
"incident_status": "Planned",
"notification_options": {
"send_tweet": "false",
"send_email": "false"
},
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1380,
"event_type": "MAINTENANCE_NOTE_CREATE",
"event_created_on": "2021-04-12 17:36:01.847400+00:00"
}
}

View File

@ -0,0 +1,33 @@
{
"id": "53492",
"title": "Expect some services downtime due to server maintenance",
"source": "Release",
"end_time": "2021-04-12 18:08:00+00:00",
"is_private": "False",
"start_time": "2021-04-12 17:08:00+00:00",
"description": "As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.",
"affected_services": "Sample Service",
"scheduled_end_time": "2021-04-12 18:08:00+00:00",
"notification_options": {
"email_before_one_hour": "true",
"email_before_day_hour": "true",
"email_on_start": "true",
"email_on_complete": "true",
"send_tweet": "false",
"send_email": "false"
},
"scheduled_start_time": "2021-04-12 17:08:00+00:00",
"is_auto_update_status_on_scheduled_end": "True",
"is_auto_update_status_on_scheduled_start": "True",
"is_auto_update_component_status_on_scheduled_end": "True",
"is_auto_update_component_status_on_scheduled_start": "True",
"status": "Planned",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1376,
"event_type": "MAINTENANCE_PLANNED",
"event_created_on": "2021-04-12 17:07:41.131632+00:00"
}
}

View File

@ -0,0 +1,33 @@
{
"id": "53492",
"title": "Expect some services downtime due to server maintenance",
"source": "Release",
"end_time": "2021-04-12 18:08:00+00:00",
"is_private": "False",
"start_time": "2021-04-12 17:08:00+00:00",
"description": "As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.",
"affected_services": "Sample Service,Sample Service 2",
"scheduled_end_time": "2021-04-12 18:08:00+00:00",
"notification_options": {
"email_before_one_hour": "true",
"email_before_day_hour": "true",
"email_on_start": "true",
"email_on_complete": "true",
"send_tweet": "false",
"send_email": "false"
},
"scheduled_start_time": "2021-04-12 17:08:00+00:00",
"is_auto_update_status_on_scheduled_end": "True",
"is_auto_update_status_on_scheduled_start": "True",
"is_auto_update_component_status_on_scheduled_end": "True",
"is_auto_update_component_status_on_scheduled_start": "True",
"status": "Planned",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1376,
"event_type": "MAINTENANCE_PLANNED",
"event_created_on": "2021-04-12 17:07:41.131632+00:00"
}
}

View File

@ -0,0 +1,33 @@
{
"id": "53492",
"title": "Expect some services downtime due to server maintenance",
"source": "Release",
"end_time": "2021-04-12 18:08:00+00:00",
"is_private": "False",
"start_time": "2021-04-12 17:08:00+00:00",
"description": "As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.",
"affected_services": "Sample Service,Sample Service 2,Sample Service 3,Sample Service 4,Sample Service 5,Sample Service 6,Sample Service 7",
"scheduled_end_time": "2021-04-12 18:08:00+00:00",
"notification_options": {
"email_before_one_hour": "true",
"email_before_day_hour": "true",
"email_on_start": "true",
"email_on_complete": "true",
"send_tweet": "false",
"send_email": "false"
},
"scheduled_start_time": "2021-04-12 17:08:00+00:00",
"is_auto_update_status_on_scheduled_end": "True",
"is_auto_update_status_on_scheduled_start": "True",
"is_auto_update_component_status_on_scheduled_end": "True",
"is_auto_update_component_status_on_scheduled_start": "True",
"status": "Planned",
"event_data": {
"webhook_id": 102,
"account_id": 2300,
"account_name": "example",
"event_id": 1376,
"event_type": "MAINTENANCE_PLANNED",
"event_created_on": "2021-04-12 17:07:41.131632+00:00"
}
}

View File

@ -0,0 +1,19 @@
{
"id": "1",
"title": "Sample title",
"source": "",
"end_time": "None",
"is_private": "False",
"start_time": "2021-04-12 16:53:00.080999+00:00",
"description": "Sample description",
"affected_services": "'service 1','service 2','service 3'",
"notification_options": "{notification_options}",
"event_data": {
"webhook_id": 102,
"account_id": 25868,
"account_name": "example",
"event_id": 1364,
"event_type": "INCIDENT_OPEN",
"event_created_on": "2021-04-12 16:53:00.081344+00:00"
}
}

View File

@ -0,0 +1,208 @@
from zerver.lib.send_email import FromAddress
from zerver.lib.test_classes import WebhookTestCase
from zerver.models import Recipient
from zerver.webhooks.freshstatus.view import MISCONFIGURED_PAYLOAD_ERROR_MESSAGE
class FreshstatusHookTests(WebhookTestCase):
STREAM_NAME = "freshstatus"
URL_TEMPLATE = "/api/v1/external/freshstatus?api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = "freshstatus"
def test_freshstatus_incident_open_multiple_services(self) -> None:
"""
Tests if freshstatus incident open multiple services is handled correctly
"""
expected_topic = "Degradation of Multiple Servers"
expected_message = """
The following incident has been opened: **Degradation of Multiple Servers**
**Description:** This issue is being investigated.
**Start Time:** 2021-04-12 16:29 UTC
**Affected Services:**
* Database Server
* Web Server
* Web Server 2
""".strip()
self.check_webhook(
"freshstatus_incident_open_multiple_services", expected_topic, expected_message
)
def test_freshstatus_incident_open_multiple_services_over_limit(self) -> None:
"""
Tests if freshstatus incident open multiple services over limit is handled correctly
"""
expected_topic = "Degradation of Multiple Servers"
expected_message = """
The following incident has been opened: **Degradation of Multiple Servers**
**Description:** This issue is being investigated.
**Start Time:** 2021-04-12 16:29 UTC
**Affected Services:**
* Database Server
* Web Server
* Web Server 2
* Database Server 2
* Active Directory Server
[and 2 more service(s)]
""".strip()
self.check_webhook(
"freshstatus_incident_open_multiple_services_over_limit",
expected_topic,
expected_message,
)
def test_freshstatus_incident_open(self) -> None:
"""
Tests if freshstatus incident open is handled correctly
"""
expected_topic = "Degradation of Database Server"
expected_message = """
The following incident has been opened: **Degradation of Database Server**
**Description:** This issue is being investigated.
**Start Time:** 2021-04-12 16:29 UTC
**Affected Services:**
* Database Server
""".strip()
self.check_webhook("freshstatus_incident_open", expected_topic, expected_message)
def test_freshstatus_incident_note_created(self) -> None:
"""
Tests if freshstatus incident note created is handled correctly
"""
expected_topic = "Degradation of Database Server"
expected_message = """
The following note has been added to the incident: **Degradation of Database Server**
**Note:** The incident is being worked on.
""".strip()
self.check_webhook("freshstatus_incident_note_created", expected_topic, expected_message)
def test_freshstatus_incident_closed(self) -> None:
"""
Tests if freshstatus incident closed is handled correctly
"""
expected_topic = "Degradation of Database Server"
expected_message = """
The following incident has been closed: **Degradation of Database Server**
**Note:** The incident has been resolved.
""".strip()
self.check_webhook("freshstatus_incident_closed", expected_topic, expected_message)
def test_freshstatus_scheduled_maintenance_planned(self) -> None:
"""
Tests if freshstatus scheduled maintenance planned is handled correctly
"""
expected_topic = "Expect some services downtime due to server maintenance"
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.
**Scheduled Start Time:** 2021-04-12 17:08 UTC
**Scheduled End Time:** 2021-04-12 18:08 UTC
**Affected Services:**
* Sample Service
""".strip()
self.check_webhook(
"freshstatus_scheduled_maintenance_planned", expected_topic, expected_message
)
def test_freshstatus_scheduled_maintenance_planned_multiple_services(self) -> None:
"""
Tests if freshstatus scheduled maintenance planned multiple services is handled correctly
"""
expected_topic = "Expect some services downtime due to server maintenance"
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.
**Scheduled Start Time:** 2021-04-12 17:08 UTC
**Scheduled End Time:** 2021-04-12 18:08 UTC
**Affected Services:**
* Sample Service
* Sample Service 2
""".strip()
self.check_webhook(
"freshstatus_scheduled_maintenance_planned_multiple_services",
expected_topic,
expected_message,
)
def test_freshstatus_scheduled_maintenance_planned_multiple_services_over_limit(self) -> None:
"""
Tests if freshstatus scheduled maintenance planned multiple services over limit is handled correctly
"""
expected_topic = "Expect some services downtime due to server maintenance"
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at support@example.com if you have any questions regarding this server upgrading exercise.
**Scheduled Start Time:** 2021-04-12 17:08 UTC
**Scheduled End Time:** 2021-04-12 18:08 UTC
**Affected Services:**
* Sample Service
* Sample Service 2
* Sample Service 3
* Sample Service 4
* Sample Service 5
[and 2 more service(s)]
""".strip()
self.check_webhook(
"freshstatus_scheduled_maintenance_planned_multiple_services_over_limit",
expected_topic,
expected_message,
)
def test_freshstatus_scheduled_maintenance_note_created(self) -> None:
"""
Tests if freshstatus scheduled maintenance note created is handled correctly
"""
expected_topic = "Scheduled Maintenance Test"
expected_message = """
The following note has been added to the scheduled maintenance: **Scheduled Maintenance Test**
**Note:** We are about to start the maintenance.
""".strip()
self.check_webhook(
"freshstatus_scheduled_maintenance_note_created", expected_topic, expected_message
)
def test_freshstatus_scheduled_maintenance_closed(self) -> None:
"""
Tests if freshstatus scheduled maintenance closed is handled correctly
"""
expected_topic = "Scheduled Maintenance Test"
expected_message = """
The following scheduled maintenance has been closed: **Scheduled Maintenance Test**
**Note:** The maintenance is now complete.
""".strip()
self.check_webhook(
"freshstatus_scheduled_maintenance_closed", expected_topic, expected_message
)
def test_freshstatus_test(self) -> None:
"""
Tests if freshstatus test is handled correctly
"""
expected_topic = "Freshstatus"
expected_message = "Freshstatus webhook has been successfully configured."
self.check_webhook("freshstatus_test", expected_topic, expected_message)
def test_freshstatus_event_not_supported(self) -> None:
"""
Tests if freshstatus event not supported is handled correctly
"""
expected_topic = "Sample title"
expected_message = "The event (INCIDENT_REOPEN) is not supported yet."
self.check_webhook("freshstatus_event_not_supported", expected_topic, expected_message)
def test_freshstatus_invalid_payload_with_missing_data(self) -> None:
"""
Tests if invalid Freshstatus payloads are handled correctly
"""
self.url = self.build_webhook_url()
payload = self.get_body("freshstatus_invalid_payload_with_missing_data")
result = self.client_post(self.url, payload, content_type="application/json")
self.assert_json_error(result, "Invalid payload")
expected_message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(
bot_name=self.test_user.full_name,
support_email=FromAddress.SUPPORT,
).strip()
msg = self.get_last_message()
self.assertEqual(msg.content, expected_message)
self.assertEqual(msg.recipient.type, Recipient.PERSONAL)

View File

@ -0,0 +1,165 @@
from typing import Any, Dict, List
import dateutil.parser
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from zerver.decorator import REQ, has_request_variables, webhook_view
from zerver.lib.actions import send_rate_limited_pm_notification_to_bot_owner
from zerver.lib.response import json_error, json_success
from zerver.lib.send_email import FromAddress
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
from zerver.models import UserProfile
MISCONFIGURED_PAYLOAD_ERROR_MESSAGE = """
Hi there! Your bot {bot_name} just received a Freshstatus payload that is missing
some data that Zulip requires. This usually indicates a configuration issue
in your Freshstatus webhook settings. Please make sure that you provide all the required parameters
when configuring the Freshstatus webhook. Contact {support_email} if you
need further help!
"""
FRESHSTATUS_TOPIC_TEMPLATE = "{title}".strip()
FRESHSTATUS_TOPIC_TEMPLATE_TEST = "Freshstatus"
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN = """
The following incident has been opened: **{title}**
**Description:** {description}
**Start Time:** {start_time}
**Affected Services:**
{affected_services}
""".strip()
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_CLOSED = """
The following incident has been closed: **{title}**
**Note:** {message}
""".strip()
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_NOTE_CREATED = """
The following note has been added to the incident: **{title}**
**Note:** {message}
""".strip()
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED = """
The following scheduled maintenance has been opened: **{title}**
**Description:** {description}
**Scheduled Start Time:** {scheduled_start_time}
**Scheduled End Time:** {scheduled_end_time}
**Affected Services:**
{affected_services}
""".strip()
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_CLOSED = """
The following scheduled maintenance has been closed: **{title}**
**Note:** {message}
""".strip()
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_NOTE_CREATED = """
The following note has been added to the scheduled maintenance: **{title}**
**Note:** {message}
""".strip()
FRESHSTATUS_MESSAGE_EVENT_NOT_SUPPORTED = "The event ({event_type}) is not supported yet."
FRESHSTATUS_SERVICES_ROW_TEMPLATE = "* {service_name}\n"
FRESHSTATUS_SERVICES_OTHERS_ROW_TEMPLATE = "[and {services_number} more service(s)]"
FRESHSTATUS_SERVICES_LIMIT = 5
@webhook_view("Freshstatus")
@has_request_variables
def api_freshstatus_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type="body"),
) -> HttpResponse:
try:
body = get_body_for_http_request(payload)
subject = get_subject_for_http_request(payload)
except KeyError:
message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(
bot_name=user_profile.full_name,
support_email=FromAddress.SUPPORT,
).strip()
send_rate_limited_pm_notification_to_bot_owner(user_profile, user_profile.realm, message)
return json_error(_("Invalid payload"))
check_send_webhook_message(request, user_profile, subject, body)
return json_success()
def get_services_content(services_data: List[Dict[str, Any]]) -> str:
services_content = ""
for service in services_data[:FRESHSTATUS_SERVICES_LIMIT]:
services_content += FRESHSTATUS_SERVICES_ROW_TEMPLATE.format(
service_name=service.get("service_name")
)
if len(services_data) > FRESHSTATUS_SERVICES_LIMIT:
services_content += FRESHSTATUS_SERVICES_OTHERS_ROW_TEMPLATE.format(
services_number=len(services_data) - FRESHSTATUS_SERVICES_LIMIT,
)
return services_content.rstrip()
def get_subject_for_http_request(payload: Dict[str, Any]) -> str:
event_data = payload["event_data"]
if event_data["event_type"] == "INCIDENT_OPEN" and payload["id"] == "1":
return FRESHSTATUS_TOPIC_TEMPLATE_TEST
else:
return FRESHSTATUS_TOPIC_TEMPLATE.format(title=payload["title"])
def get_body_for_maintenance_planned_event(payload: Dict[str, Any]) -> str:
services_data = []
for service in payload["affected_services"].split(","):
services_data.append({"service_name": service})
data = {
"title": payload["title"],
"description": payload["description"],
"scheduled_start_time": dateutil.parser.parse(payload["scheduled_start_time"]).strftime(
"%Y-%m-%d %H:%M %Z"
),
"scheduled_end_time": dateutil.parser.parse(payload["scheduled_end_time"]).strftime(
"%Y-%m-%d %H:%M %Z"
),
"affected_services": get_services_content(services_data),
}
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED.format(**data)
def get_body_for_incident_open_event(payload: Dict[str, Any]) -> str:
services_data = []
for service in payload["affected_services"].split(","):
services_data.append({"service_name": service})
data = {
"title": payload["title"],
"description": payload["description"],
"start_time": dateutil.parser.parse(payload["start_time"]).strftime("%Y-%m-%d %H:%M %Z"),
"affected_services": get_services_content(services_data),
}
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN.format(**data)
def get_body_for_http_request(payload: Dict[str, Any]) -> str:
event_data = payload["event_data"]
if event_data["event_type"] == "INCIDENT_OPEN" and payload["id"] == "1":
return get_setup_webhook_message("Freshstatus")
elif event_data["event_type"] == "INCIDENT_OPEN":
return get_body_for_incident_open_event(payload)
elif event_data["event_type"] == "INCIDENT_NOTE_CREATE":
if payload["incident_status"] == "Closed":
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_CLOSED.format(**payload)
elif payload["incident_status"] == "Open":
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_NOTE_CREATED.format(**payload)
elif event_data["event_type"] == "MAINTENANCE_PLANNED":
return get_body_for_maintenance_planned_event(payload)
elif event_data["event_type"] == "MAINTENANCE_NOTE_CREATE":
if payload["incident_status"] == "Closed":
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_CLOSED.format(**payload)
else:
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_NOTE_CREATED.format(**payload)
return FRESHSTATUS_MESSAGE_EVENT_NOT_SUPPORTED.format(event_type=event_data["event_type"])