Skip to content

Commit

Permalink
Nudge incident lead to complete a report on close (#112)
Browse files Browse the repository at this point in the history
* Delete empty file

* Remind incident lead to complete a report on close

* Use a pre_save signal

post_save only has access to the updated model (it's been written to the
db, so we can't determine if the incident was just closed.

Use pre_save which allows us to retrieve the current state and compare.

* Remove unused imports / empty module
  • Loading branch information
mattrco authored and milesbxf committed Aug 21, 2019
1 parent b310e5b commit 5ffb15d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions response/slack/incident_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from response.slack.client import SlackError, reference_to_id
from datetime import datetime


@incident_command(['help'], helptext='Display a list of commands and usage')
def send_help_text(incident: Incident, user_id: str, message: str):
return True, get_help()
Expand Down
31 changes: 27 additions & 4 deletions response/slack/signals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.db.models.signals import post_save
from django.core.signals import request_finished
from urllib.parse import urljoin

from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from django.conf import settings
from django.urls import reverse

from response.core.models import Incident
from response.slack.models import HeadlinePost

from time import sleep


@receiver(post_save, sender=Incident)
def update_headline_after_incident_save(sender, instance, **kwargs):
Expand All @@ -28,6 +29,28 @@ def update_headline_after_incident_save(sender, instance, **kwargs):
)


@receiver(pre_save, sender=Incident)
def prompt_incident_report(sender, instance: Incident, **kwargs):
"""
Prompt incident lead to complete a report when an incident is closed.
"""

try:
prev_state = Incident.objects.get(pk=instance.pk)
except Incident.DoesNotExist:
# Incident hasn't been saved yet, nothing to do here.
return

if instance.is_closed() and not prev_state.is_closed():
user_to_notify = instance.lead or instance.reporter
doc_url = urljoin(
settings.SITE_URL,
reverse('incident_doc', kwargs={'incident_id': instance.pk})
)
settings.SLACK_CLIENT.send_message(
user_to_notify.external_id, f"👋 Don't forget to fill out an incident report here: {doc_url}")


@receiver(post_save, sender=HeadlinePost)
def update_headline_after_save(sender, instance, **kwargs):
"""
Expand Down
Empty file removed response/slack/slack_utils.py
Empty file.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import pytest
from unittest.mock import MagicMock

from response.slack import dialog_builder, slack_utils, block_kit
from response.slack.authentication import generate_signature
from response.slack.client import SlackClient


@pytest.fixture(autouse=True)
def mock_slack(monkeypatch):
mock_slack = MagicMock(spec=SlackClient(""))
Expand Down

0 comments on commit 5ffb15d

Please sign in to comment.