Skip to content

Commit

Permalink
Add slack webhook retries & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson committed Oct 6, 2024
1 parent ce9724a commit 942d8e7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
44 changes: 44 additions & 0 deletions src/meshapi/tests/test_install_create_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,47 @@ def test_no_events_for_install_edit(self, request_mocker):
install.save()

self.assertEqual(len(request_mocker.request_history), 0)

@patch(
"meshapi.util.events.join_requests_slack_channel.SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL",
"http://example.com/test-url-slack",
)
@patch(
"meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT",
"http://example.com/test-url-os-ticket",
)
@patch(
"meshapi.util.events.osticket_creation.OSTICKET_API_TOKEN",
"mock-token",
)
@requests_mock.Mocker()
def test_many_retry_no_crash_on_integration_404(self, request_mocker):
request_mocker.post("http://example.com/test-url-slack", text="Not found", status_code=404)
request_mocker.post("http://example.com/test-url-os-ticket", text="Not found", status_code=404)

enable_flag("INTEGRATION_ENABLED_SEND_JOIN_REQUEST_SLACK_MESSAGES")
enable_flag("INTEGRATION_ENABLED_CREATE_OSTICKET_TICKETS")

install = Install(**self.sample_install_copy)
install.save()

self.assertEqual(
len(
[
request
for request in request_mocker.request_history
if request.url == "http://example.com/test-url-os-ticket"
]
),
4,
)
self.assertEqual(
len(
[
request
for request in request_mocker.request_history
if request.url == "http://example.com/test-url-slack"
]
),
4,
)
25 changes: 17 additions & 8 deletions src/meshapi/util/events/join_requests_slack_channel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import time

import requests
from django.db.models.base import ModelBase
Expand Down Expand Up @@ -29,14 +30,22 @@ def send_join_request_slack_message(sender: ModelBase, instance: Install, create
building_height = str(int(install.building.altitude)) + "m" if install.building.altitude else "Altitude not found"
roof_access = "Roof access" if install.roof_access else "No roof access"

response = requests.post(
SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL,
json={
"text": f"*<https://www.nycmesh.net/map/nodes/{install.install_number}"
f"|{install.building.one_line_complete_address}>*\n"
f"{building_height} · {roof_access} · No LoS Data Available"
},
)
attempts = 0
while attempts < 4:
attempts += 1
response = requests.post(
SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL,
json={
"text": f"*<https://www.nycmesh.net/map/nodes/{install.install_number}"
f"|{install.building.one_line_complete_address}>*\n"
f"{building_height} · {roof_access} · No LoS Data Available"
},
)

if response.status_code == 200:
break

time.sleep(1)

if response.status_code != 200:
logging.error(
Expand Down

0 comments on commit 942d8e7

Please sign in to comment.