diff --git a/app/modules/incident/incident_helper.py b/app/modules/incident/incident_helper.py index 959129cc..99006216 100644 --- a/app/modules/incident/incident_helper.py +++ b/app/modules/incident/incident_helper.py @@ -302,9 +302,9 @@ def close_incident(client, body, ack): # get the current chanel id and name channel_id = body["channel_id"] channel_name = body["channel_name"] + user_id = body["user_id"] if not channel_name.startswith("incident-"): - user_id = body["user_id"] try: response = client.chat_postEphemeral( text=f"Channel {channel_name} is not an incident channel. Please use this command in an incident channel.", @@ -352,6 +352,12 @@ def close_incident(client, body, ack): logging.error( "Could not archive the channel %s - %s", channel_name, response["error"] ) + else: + # post a message that the channel has been archived by the user + client.chat_postMessage( + channel=channel_id, + text=f"<@{user_id}> has archived this channel 👋", + ) def stale_incidents(client, body, ack): diff --git a/app/tests/modules/incident/test_incident_helper.py b/app/tests/modules/incident/test_incident_helper.py index c3acbb81..c1e9e0c2 100644 --- a/app/tests/modules/incident/test_incident_helper.py +++ b/app/tests/modules/incident/test_incident_helper.py @@ -440,7 +440,11 @@ def test_close_incident(mock_extract_id, mock_update_spreadsheet, mock_close_doc # Call close_incident incident_helper.close_incident( mock_client, - {"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"}, + { + "channel_id": "C12345", + "channel_name": "incident-2024-01-12-test", + "user_id": "U12345", + }, mock_ack, ) @@ -479,7 +483,11 @@ def test_close_incident_no_bookmarks( # Call close_incident incident_helper.close_incident( mock_client, - {"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"}, + { + "channel_id": "C12345", + "channel_name": "incident-2024-01-12-test", + "user_id": "U12345", + }, mock_ack, ) @@ -508,7 +516,11 @@ def test_close_incident_no_bookmarks_error( # Call close_incident incident_helper.close_incident( mock_client, - {"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"}, + { + "channel_id": "C12345", + "channel_name": "incident-2024-01-12-test", + "user_id": "U12345", + }, mock_ack, ) @@ -622,7 +634,11 @@ def test_conversations_archive_fail( # Call close_incident incident_helper.close_incident( mock_client, - {"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"}, + { + "channel_id": "C12345", + "channel_name": "incident-2024-01-12-test", + "user_id": "U12345", + }, mock_ack, ) @@ -669,7 +685,11 @@ def test_conversations_archive_fail_error_message( # Call close_incident incident_helper.close_incident( mock_client, - {"channel_id": "C12345", "channel_name": "incident-2024-01-12-test"}, + { + "channel_id": "C12345", + "channel_name": "incident-2024-01-12-test", + "user_id": "U12345", + }, mock_ack, ) @@ -687,6 +707,52 @@ def test_conversations_archive_fail_error_message( ) +@patch("modules.incident.incident_helper.google_drive.close_incident_document") +@patch( + "modules.incident.incident_helper.google_drive.update_spreadsheet_close_incident" +) +@patch( + "integrations.google_workspace.google_docs.extract_google_doc_id", + return_value="dummy_document_id", +) +def test_conversations_archive_succeeds_post_message_who_archived( + mock_extract_id, mock_update_spreadsheet, mock_close_document, caplog +): + mock_client = MagicMock() + mock_ack = MagicMock() + body = { + "channel_id": "channel_id", + "channel_name": "incident-channel_name", + "user_id": "user_id", + } + incident_helper.close_incident(mock_client, body, mock_ack) + + # 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 success + mock_client.conversations_archive.return_value = { + "ok": True, + } + + # assert that the channel was archived + mock_client.conversations_archive.assert_called_once_with(channel="channel_id") + + # assert message was posted to archived channel. + mock_client.chat_postMessage.assert_any_call( + text="<@user_id> has archived this channel 👋", + channel="channel_id", + ) + + 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"