diff --git a/app/commands/incident.py b/app/commands/incident.py index 9900a924..4b75a9c2 100644 --- a/app/commands/incident.py +++ b/app/commands/incident.py @@ -28,7 +28,7 @@ INCIDENT_CHANNEL = os.environ.get("INCIDENT_CHANNEL") SLACK_SECURITY_USER_GROUP_ID = os.environ.get("SLACK_SECURITY_USER_GROUP_ID") -START_HEADING = "Detailed Timeline" +START_HEADING = "DO NOT REMOVE this line as the SRE bot needs it as a placeholder." END_HEADING = "Trigger" @@ -401,7 +401,6 @@ def handle_reaction_added(client, ack, body, logger): ) if document_id == "": logger.error("No incident document found for this channel.") - for message in messages: # convert the time which is now in epoch time to standard ET Time message_date_time = convert_epoch_to_datetime_est(message["ts"]) @@ -476,7 +475,6 @@ def handle_reaction_removed(client, ack, body, logger): ) if document_id == "": logger.error("No incident document found for this channel.") - # Retrieve the current content of the timeline content = get_timeline_section(document_id) @@ -491,7 +489,7 @@ def handle_reaction_removed(client, ack, body, logger): # Remove the message if message_to_remove in content: - content = content.replace(message_to_remove, "") + content = content.replace(message_to_remove, "\n") # Update the timeline content result = replace_text_between_headings( diff --git a/app/integrations/google_drive.py b/app/integrations/google_drive.py index 11982c9e..184d8088 100644 --- a/app/integrations/google_drive.py +++ b/app/integrations/google_drive.py @@ -13,7 +13,7 @@ SRE_INCIDENT_FOLDER = os.environ.get("SRE_INCIDENT_FOLDER") INCIDENT_TEMPLATE = os.environ.get("INCIDENT_TEMPLATE") INCIDENT_LIST = os.environ.get("INCIDENT_LIST") -START_HEADING = "Detailed Timeline" +START_HEADING = "DO NOT REMOVE this line as the SRE bot needs it as a placeholder." END_HEADING = "Trigger" PICKLE_STRING = os.environ.get("PICKLE_STRING", False) @@ -298,7 +298,6 @@ def get_timeline_section(document_id): service = get_google_service("docs", "v1") document = service.documents().get(documentId=document_id).execute() content = document.get("body").get("content") - print("In google, content is", content) timeline_content = "" record = False @@ -354,8 +353,17 @@ def replace_text_between_headings(doc_id, new_content, start_heading, end_headin break if start_index is not None and end_index is not None: - # Format new content with new lines for proper insertion - formatted_content = "\n" + new_content + "\n" + # Format new content with new lines for proper insertion. We need to make sure that the formatted string contains only one + # leading and trailing newline character + # Split the string into three parts: leading newlines, core content, and trailing newlines + leading_newlines = len(new_content) - len(new_content.lstrip("\n")) + trailing_newlines = len(new_content) - len(new_content.rstrip("\n")) + core_content = new_content[ + leading_newlines : len(new_content) - trailing_newlines + ] + + # Ensure only one newline at the start and one at the end, preserving internal newlines + formatted_content = "\n" + core_content + "\n" content_length = len(formatted_content) # Perform the replacement @@ -395,7 +403,7 @@ def replace_text_between_headings(doc_id, new_content, start_heading, end_headin { "updateParagraphStyle": { "range": { - "startIndex": start_index + 1, + "startIndex": start_index, "endIndex": ( start_index + content_length ), # Adjust this index based on the length of the text diff --git a/app/tests/intergrations/test_google_drive.py b/app/tests/intergrations/test_google_drive.py index e17a5dea..ad178b19 100644 --- a/app/tests/intergrations/test_google_drive.py +++ b/app/tests/intergrations/test_google_drive.py @@ -6,7 +6,7 @@ from unittest.mock import patch # Constants for the test -START_HEADING = "Detailed Timeline" +START_HEADING = "DO NOT REMOVE this line as the SRE bot needs it as a placeholder." END_HEADING = "Trigger" @@ -251,7 +251,6 @@ def test_extract_timeline_content(mock_service): # Mock document content content = [START_HEADING, "Timeline content", END_HEADING] mock_document = create_mock_document(content) - print("Mock document is ", mock_document) mock_service.return_value.documents().get().execute.return_value = mock_document result = google_drive.get_timeline_section("document_id")