From 67d21cf0102680fa26adceb5f878f05e7a9bbfd4 Mon Sep 17 00:00:00 2001 From: rclarke0 Date: Thu, 27 Jun 2024 13:29:32 -0400 Subject: [PATCH] feat(abr-testing): add error lines as comments to jira ticket --- .../abr_testing/automation/jira_tool.py | 31 ++++++++++++++++--- .../data_collection/abr_robot_error.py | 30 ++++++++++++++++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/abr-testing/abr_testing/automation/jira_tool.py b/abr-testing/abr_testing/automation/jira_tool.py index 58f4af669bf..47f7c7fd301 100644 --- a/abr-testing/abr_testing/automation/jira_tool.py +++ b/abr-testing/abr_testing/automation/jira_tool.py @@ -5,7 +5,7 @@ import json import webbrowser import argparse -from typing import List, Dict, Any +from typing import List, Dict, Any, Tuple import os @@ -61,7 +61,7 @@ def create_ticket( components: list, affects_versions: str, robot: str, - ) -> str: + ) -> Tuple[str, str]: """Create ticket.""" data = { "fields": { @@ -98,15 +98,15 @@ def create_ticket( response_str = str(response.content) issue_url = response.json().get("self") issue_key = response.json().get("key") - print(f"issue key {issue_key}") - print(f"issue url{issue_url}") + print(f"issue key: {issue_key}") + print(f"issue url: {issue_url}") if issue_key is None: print("Error: Could not create issue. No key returned.") except requests.exceptions.HTTPError: print(f"HTTP error occurred. Response content: {response_str}") except json.JSONDecodeError: print(f"JSON decoding error occurred. Response content: {response_str}") - return issue_key + return issue_key, issue_url def post_attachment_to_ticket(self, issue_id: str, attachment_path: str) -> None: """Adds attachments to ticket.""" @@ -185,6 +185,27 @@ def get_project_components(self, project_id: str) -> List[Dict[str, str]]: components_list = response.json() return components_list + def comment(self, comment_str: str, issue_url: str, comment_type: str) -> None: + """Leave comment on JIRA Ticket.""" + comment_url = issue_url + "/comment" + payload = json.dumps( + { + "body": { + "type": "doc", + "version": 1, + "content": [ + { + "type": comment_type, + "content": [{"type": "text", "text": comment_str}], + } + ], + } + } + ) + requests.request( + "POST", comment_url, data=payload, headers=self.headers, auth=self.auth + ) + if __name__ == "__main__": """Create ticket for specified robot.""" diff --git a/abr-testing/abr_testing/data_collection/abr_robot_error.py b/abr-testing/abr_testing/data_collection/abr_robot_error.py index 31261415d96..dad2e50163c 100644 --- a/abr-testing/abr_testing/data_collection/abr_robot_error.py +++ b/abr-testing/abr_testing/data_collection/abr_robot_error.py @@ -12,6 +12,30 @@ import re +def read_each_log(folder_path: str, issue_url: str) -> None: + """Read log and comment error portion on JIRA ticket.""" + for file_name in os.listdir(folder_path): + file_path = os.path.join(folder_path, file_name) + print(file_path) + if file_path.endswith(".log"): + with open(file_path) as file: + lines = file.readlines() + word = "error" + error_lines = "" + for line_index, line in enumerate(lines): + if word in line: + lines_before = max(0, line_index - 20) + lines_after = min(len(lines), line_index + 20) + print("Line Number:", line_index + 1) + error_lines = "".join(lines[lines_before:lines_after]) + message = f"Error found in {file_path}" + ticket.comment(error_lines, issue_url, "codeBlock") + break + if len(error_lines) < 1: + message = f"No error found in {file_name}" + ticket.comment(message, issue_url, "paragraph") + + def match_error_to_component( project_id: str, error_message: str, components: List[str] ) -> List[str]: @@ -252,6 +276,7 @@ def get_run_error_info_from_robot( ip, storage_directory ) file_paths = read_robot_logs.get_logs(storage_directory, ip) + print(f"Making ticket for {summary}.") # TODO: make argument or see if I can get rid of with using board_id. project_key = "RABR" @@ -259,7 +284,7 @@ def get_run_error_info_from_robot( parent_key = project_key + "-" + robot.split("ABR")[1] # TODO: read board to see if ticket for run id already exists. # CREATE TICKET - issue_key = ticket.create_ticket( + issue_key, raw_issue_url = ticket.create_ticket( summary, whole_description_str, project_key, @@ -288,7 +313,8 @@ def get_run_error_info_from_robot( continue # OPEN FOLDER DIRECTORY subprocess.Popen(["explorer", error_folder_path]) - + # ADD ERROR COMMENTS TO TICKET + read_each_log(error_folder_path, raw_issue_url) # WRITE ERRORED RUN TO GOOGLE SHEET if len(run_or_other) < 1: # CONNECT TO GOOGLE DRIVE