Skip to content

Commit

Permalink
Adding more logging when the archiving of the channel fails (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamclaughlin authored Jan 30, 2024
1 parent a2843c7 commit c838ad4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/commands/helpers/incident_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,13 @@ def close_incident(client, body, ack):
google_drive.update_spreadsheet_close_incident(return_channel_name(channel_name))

# archive the channel
client.conversations_archive(channel=channel_id)
response = client.conversations_archive(channel=channel_id)

# if the response is not successful, then we have to log the message
if not response["ok"]:
logging.error(
"Could not archive the channel %s - %s", channel_name, response["error"]
)


def stale_incidents(client, body, ack):
Expand Down
58 changes: 57 additions & 1 deletion app/tests/commands/helpers/test_incident_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from commands.helpers import incident_helper
import logging


from unittest.mock import ANY, MagicMock, patch
Expand Down Expand Up @@ -567,7 +568,10 @@ def test_conversations_archive_fail(
}

# Mock the response of client.conversations_archive to indicate failure
mock_client.conversations_archive.return_value = {"ok": False}
mock_client.conversations_archive.return_value = {
"ok": False,
"error": "not_in_channel",
}

# Call close_incident
incident_helper.close_incident(
Expand All @@ -585,6 +589,58 @@ def test_conversations_archive_fail(
mock_client.conversations_archive.assert_called_once_with(channel="C12345")


@patch("commands.helpers.incident_helper.google_drive.close_incident_document")
@patch(
"commands.helpers.incident_helper.google_drive.update_spreadsheet_close_incident"
)
@patch(
"commands.helpers.incident_helper.extract_google_doc_id",
return_value="dummy_document_id",
)
def test_conversations_archive_fail_error_message(
mock_extract_id, mock_update_spreadsheet, mock_close_document, caplog
):
mock_client = MagicMock()
mock_ack = MagicMock()
# Mock the response of client.bookmarks_list with a valid bookmark
mock_client.bookmarks_list.return_value = {
"ok": True,
"bookmarks": [
{
"title": "Incident report",
"link": "https://docs.google.com/document/d/dummy_document_id/edit",
}
],
}

# Mock the response of client.conversations_archive to indicate failure
mock_client.conversations_archive.return_value = {
"ok": True,
"error": "not_in_channel",
}

with caplog.at_level(logging.ERROR, logger="commands.helpers.incident_helper"):
# Call close_incident
incident_helper.close_incident(
mock_client,
{"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"},
mock_ack,
)

# Assertions
# Ensure that the Google Drive document update method was called even if archiving fails
mock_close_document.assert_called_once_with("dummy_document_id")
mock_update_spreadsheet.assert_called_once_with("#2024-01-12-test")

# Ensure that the client's conversations_archive method was called
mock_client.conversations_archive.assert_called_once_with(channel="C12345")

assert (
"Could not archive the channel incident-2024-01-12-test - not_in_channel"
not in caplog.text
)


def test_return_channel_name_with_prefix():
# Test the function with a string that includes the prefix.
assert incident_helper.return_channel_name("incident-abc123") == "#abc123"
Expand Down

0 comments on commit c838ad4

Please sign in to comment.