import requests\nimport os\nimport zipfile\nimport io\nimport re\nimport logging\nfrom .base_tool import BaseTool # Added\nfrom .metrics import metrics # Added\n\n# Configure logging for the tool - This will be handled by the logger instance now\n# logger = logging.getLogger(__name__) # Commented out or removed\n\nclass GitHubCIHelper(BaseTool): # Inherits from BaseTool\n """\n A helper class to interact with GitHub Actions CI,\n specifically for fetching and analyzing test logs.\n """\n def __init__(self, repo_owner: str, repo_name: str, github_token: str = None, session=None, logger_instance=None): # Added session and logger_instance\n """\n Initializes the GitHubCIHelper.\n\n Args:\n repo_owner (str): The owner of the GitHub repository (e.g., \'\'\'bucolucas\'\'\').\n repo_name (str): The name of the GitHub repository (e.g., \'\'\'cyclop\'\'\').\n github_token (str, optional): A GitHub Personal Access Token (PAT)\n for API authentication. Recommended for\n private repos or higher rate limits.\n Can also be set via GITHUB_TOKEN env var.\n session (requests.Session, optional): An external requests session to use.\n logger_instance (logging.Logger, optional): An external logger instance.\n """\n self.repo_owner = repo_owner\n self.repo_name = repo_name\n self.base_url = f"https://api.github.com/repos/{self.repo_owner}/{self.repo_name}"\n self._token = github_token or os.environ.get(\'GITHUB_TOKEN\') # Renamed to _token for consistency\n \n self.headers = {\n "Accept": "application/vnd.github.v3+json"\n }\n if self._token: # Use self._token\n self.headers["Authorization"] = f"token {self._token}"\n\n if session:\n self.session = session\n else:\n self.session = requests.Session()\n # Headers are applied per-request in _make_request or directly where self.session is used if needed globally for session\n\n self.logger = logger_instance if logger_instance else logging.getLogger(__name__)\n if not self.logger.handlers:\n self.logger.addHandler(logging.NullHandler())\n\n def get_functions(self):\n return [\n {\n "type": "function",\n "function": {\n "name": "get_pr_workflow_runs",\n "description": "Gets all workflow runs associated with a specific pull request.",\n "parameters": {\n "type": "object",\n "properties": {\n "pull_request_number": {"type": "integer", "description": "The number of the pull request."}\n },\n "required": ["pull_request_number"]\n }\n }\n },\n {\n "type": "function",\n "function": {\n "name": "get_latest_failed_run_for_pr",\n "description": "Gets the latest failed workflow run for a specific pull request and workflow name.",\n "parameters": {\n "type": "object",\n "properties": {\n "pull_request_number": {"type": "integer", "description": "The number of the pull request."},\n "workflow_name": {"type": "string", "description": "The display name of the workflow (e.g., \'\'\'Python CI\'\'\').", "default": "Python CI"}\n },\n "required": ["pull_request_number"]\n }\n }\n },\n {\n "type": "function",\n "function": {\n "name": "get_job_logs_for_run",\n "description": "Downloads and returns the logs for a specific job within a workflow run.",\n "parameters": {\n "type": "object",\n "properties": {\n "run_id": {"type": "integer", "description": "The ID of the workflow run."},\n "job_name": {"type": "string", "description": "The name of the job (e.g., \'\'\'test\'\'\').", "default": "test"}\n },\n "required": ["run_id"]\n }\n }\n },\n {\n "type": "function",\n "function": {\n "name": "parse_unittest_failures_from_log",\n "description": "Parses unittest failure details from log content. This is a basic parser and might need adjustments based on specific log formats.",\n "parameters": {\n "type": "object",\n "properties": {\n "log_content": {"type": "string", "description": "The string content of the job log."}\n },\n "required": ["log_content"]\n }\n }\n }\n ]\n\n @metrics.measure\n def execute(self, function_name, **kwargs):\n self.logger.info(f"Executing GitHub CI Helper function: {function_name} with args: {kwargs}")\n # Dispatch to the appropriate public method\n if hasattr(self, function_name) and callable(getattr(self, function_name)) and not function_name.startswith("_"):\n method = getattr(self, function_name)\n try:\n return method(**kwargs)\n except Exception as e:\n self.logger.error(f"Error executing {function_name}: {e}", exc_info=True)\n return f"Error during {function_name} execution: {str(e)}"\n else:\n error_message = f"Unknown or private function: {function_name}"\n self.logger.error(error_message)\n return error_message\n \n def clear(self):\n """Clears any sensitive state if necessary. For this tool, it\'s a no-op but present for interface consistency."""\n self.logger.info("GitHubCIHelper state cleared (no specific state to clear).")\n\n\n @metrics.measure\n def _make_request(self, method: str, url: str, **kwargs): # Added @metrics.measure\n """Helper function for making HTTP requests."""\n try:\n # Use self.session instead of requests directly\n response = self.session.request(method, url, headers=self.headers, **kwargs)\n response.raise_for_status()\n if response.content and response.headers.get(\'Content-Type\', \'\').startswith(\'application/json\'):\n return response.json()\n elif response.content: # For non-JSON content like zip files or plain text logs\n return response \n return None\n except requests.exceptions.HTTPError as e:\n self.logger.error(f"HTTP error occurred: {e} - {e.response.text if e.response else \'No response text\'}") # Use self.logger\n raise\n except requests.exceptions.RequestException as e:\n self.logger.error(f"Request failed: {e}") # Use self.logger\n raise\n\n @metrics.measure\n def get_pr_workflow_runs(self, pull_request_number: int): # Added @metrics.measure\n """\n Gets all workflow runs associated with a specific pull request.\n """\n self.logger.info(f"Getting workflow runs for PR #{pull_request_number}")\n pr_url = f"{self.base_url}/pulls/{pull_request_number}"\n try:\n pr_response = self._make_request("GET", pr_url) # this returns a response object or parsed JSON\n pr_data = pr_response if isinstance(pr_response, dict) else pr_response.json() # Ensure pr_data is dict\n\n if not pr_data or \'head\' not in pr_data or \'sha\' not in pr_data[\'head\']:\n self.logger.error(f"Could not get head SHA for PR {pull_request_number}. Response: {pr_data}")\n return None\n head_sha = pr_data[\'head\'][\'sha\']\n\n runs_url = f"{self.base_url}/actions/runs?event=pull_request&head_sha={head_sha}"\n runs_response = self._make_request("GET", runs_url)\n runs_data = runs_response if isinstance(runs_response, dict) else runs_response.json()\n\n return runs_data.get("workflow_runs") if runs_data else None\n except Exception as e:\n self.logger.error(f"Failed to get PR workflow runs for PR {pull_request_number}: {e}", exc_info=True)\n return None\n\n\n @metrics.measure\n def get_latest_failed_run_for_pr(self, pull_request_number: int, workflow_name: str = "Python CI"): # Added @metrics.measure\n """\n Gets the latest failed workflow run for a specific pull request and workflow name.\n """\n self.logger.info(f"Getting latest failed run for PR #{pull_request_number}, workflow: \'{workflow_name}\'")\n runs = self.get_pr_workflow_runs(pull_request_number)\n if not runs:\n self.logger.info(f"No runs found for PR #{pull_request_number} to check for failures.")\n return None\n\n for run in sorted(runs, key=lambda r: r[\'created_at\'], reverse=True):\n if run[\'name\'] == workflow_name and run[\'conclusion\'] == \'failure\':\n self.logger.info(f"Found failed run {run[\'id\']} for workflow \'{workflow_name}\' in PR #{pull_request_number}")\n return run\n self.logger.info(f"No failed run for workflow \'{workflow_name}\' found for PR #{pull_request_number}") # Use self.logger\n return None\n\n @metrics.measure\n def get_job_logs_for_run(self, run_id: int, job_name: str = "test"): # Added @metrics.measure\n """\n Downloads and returns the logs for a specific job within a workflow run.\n """\n self.logger.info(f"Getting job logs for run ID {run_id}, job name \'{job_name}\'")\n jobs_url = f"{self.base_url}/actions/runs/{run_id}/jobs"\n target_job = None # Initialize target_job here to ensure it\'s defined for later logging\n try:\n jobs_response = self._make_request("GET", jobs_url)\n jobs_data = jobs_response if isinstance(jobs_response, dict) else jobs_response.json()\n\n if not jobs_data or "jobs" not in jobs_data:\n self.logger.error(f"Could not retrieve jobs for run ID {run_id}. Response: {jobs_data}")\n return None\n\n for job in jobs_data["jobs"]:\n if job["name"] == job_name:\n target_job = job\n break\n\n if not target_job:\n self.logger.error(f"Job \'{job_name}\' not found in run ID {run_id}")\n return None\n\n if target_job[\'status\'] != \'completed\':\n self.logger.info(f"Job \'{job_name}\' in run ID {run_id} has not completed. Status: {target_job[\'status\']}")\n return f"Job \'{job_name}\' not yet completed (status: {target_job[\'status\']}). Logs may be unavailable."\n\n\n logs_url = f"{self.base_url}/actions/jobs/{target_job[\'id\']}/logs"\n self.logger.info(f"Attempting to download logs from: {logs_url}")\n \n log_response = self.session.get(logs_url, headers=self.headers, allow_redirects=True, stream=True)\n log_response.raise_for_status()\n\n if \'application/zip\' in log_response.headers.get(\'Content-Type\', \'\'):\n self.logger.info(f"Received zip file for logs of job ID {target_job[\'id\']}.")\n with zipfile.ZipFile(io.BytesIO(log_response.content)) as zf:\n log_file_names = [name for name in zf.namelist() if not name.endswith(\'/\')]\n if not log_file_names:\n self.logger.error(f"No files found in the downloaded log zip for job ID {target_job[\'id\']}.")\n return None\n \n actual_log_file_name = log_file_names[0]\n for name in log_file_names:\n # Improved heuristic for log file name\n if job_name in name or "test" in name.lower() or "log" in name.lower() or "out" in name.lower() or "step" in name:\n actual_log_file_name = name\n break\n \n self.logger.info(f"Extracting log file: {actual_log_file_name} from zip for job ID {target_job[\'id\']}.")\n with zf.open(actual_log_file_name) as log_file:\n return log_file.read().decode(\'utf-8\')\n else:\n self.logger.info(f"Received plain text logs for job ID {target_job[\'id\']}.")\n return log_response.text\n\n except requests.exceptions.HTTPError as e:\n self.logger.error(f"HTTP error downloading logs for job ID {target_job.get(\'id\', \'unknown\') if target_job else \'unknown\'}: {e} - {e.response.text if e.response else \'No response text\'}", exc_info=True)\n if e.response and e.response.status_code == 404:\n self.logger.error("Log download URL might be invalid or logs expired.")\n return f"Error downloading logs: {e}"\n except requests.exceptions.RequestException as e:\n self.logger.error(f"Request failed downloading logs for job ID {target_job.get(\'id\', \'unknown\') if target_job else \'unknown\'}: {e}", exc_info=True)\n return f"Error during log download request: {e}"\n except zipfile.BadZipFile:\n self.logger.error(f"Failed to unzip logs for job ID {target_job.get(\'id\', \'unknown\') if target_job else \'unknown\'}.", exc_info=True)\n # Adding response text for BadZipFile can be risky if it's large binary data.\n # Consider logging only a snippet or specific headers if this occurs frequently.\n return "Failed to unzip logs."\n except Exception as e:\n self.logger.error(f"An unexpected error occurred while processing logs for job {target_job.get(\'id\', \'unknown\') if target_job else \'unknown\'}: {e}", exc_info=True)\n return f"Unexpected error processing logs: {e}"\n\n\n @metrics.measure\n def parse_unittest_failures_from_log(self, log_content: str): # Added @metrics.measure\n """\n Parses unittest failure details from log content.\n """\n if not log_content:\n self.logger.info("Log content is empty, no failures to parse.")\n return []\n\n self.logger.info(f"Parsing unittest failures from log content (length: {len(log_content)}).")\n \n # Regex to capture standard unittest failure blocks\n # It looks for "FAIL:" or "ERROR:", the test name (e.g., test_my_method), the class (e.g., my_module.MyTestClass),\n # then a line of hyphens, "Traceback (most recent call last):", and the traceback details.\n # It stops before the next failure block or common summary lines.\n failure_pattern = re.compile(\n r"^(FAIL|ERROR): (.*?)\s*\\((.*?)\\)\s*\\n-{5,}\\s*\\nTraceback \\(most recent call last\\):\\s*\\n(.*?)(?=\\n(?:FAIL:|ERROR:)|\\n-{5,}\\s*\\nRan \\d+ tests? in|\\Z)",\n re.DOTALL | re.MULTILINE\n )\n \n failures = []\n for match in failure_pattern.finditer(log_content):\n failure_type = match.group(1) # FAIL or ERROR\n test_name = match.group(2).strip() # e.g., test_specific_behavior\n test_module_class = match.group(3).strip() # e.g., tests.test_module.TestMyFeature\n traceback_details = match.group(4).strip() # The actual traceback\n \n # Reconstruct a readable failure block\n failure_block = (\n f"{failure_type}: {test_name} ({test_module_class})\\n"\n f"---------------------\\n"\n f"Traceback (most recent call last):\\n"\n f"{traceback_details}"\n )\n failures.append(failure_block)\n\n if failures:\n self.logger.info(f"Parsed {len(failures)} failure blocks using primary regex.")\n return failures\n\n # Fallback: A more general pattern if the above doesn't match (e.g., due to slight variations in formatting)\n # This looks for "FAIL:" or "ERROR:", a line for the test name, then captures content until common separators.\n general_failure_pattern = re.compile(\n r"^(FAIL|ERROR): ([^\\n]+)\\n(.*?)(?=\\n(?:FAIL:|ERROR:)|\\n-{20,}\\n|Ran \\d+ tests? in|\\Z)",\n re.DOTALL | re.MULTILINE\n )\n for match in general_failure_pattern.finditer(log_content):\n failure_type = match.group(1)\n test_header = match.group(2).strip()\n details = match.group(3).strip()\n full_block = f"{failure_type}: {test_header}\\n{details}"\n # Avoid adding essentially duplicate or overly broad captures if specific ones exist\n if not any(f.startswith(f"{failure_type}: {test_header}") for f in failures): \n failures.append(full_block)\n \n if failures: # Check if fallback added anything\n self.logger.info(f"Parsed {len(failures)} failure blocks using general fallback regex.")\n return failures\n\n # Last resort: if specific "FAILURES!!!" section is found, often this contains a summary.\n # This might be too broad or not structured enough, but better than nothing.\n if "FAILURES!!!" in log_content or "SUMMARY OF FAILURES" in log_content:\n summary_marker = "FAILURES!!!" if "FAILURES!!!" in log_content else "SUMMARY OF FAILURES"\n start_index = log_content.find(summary_marker)\n if start_index != -1:\n # Try to find a reasonable end for this summary block\n end_pattern = re.compile(r"Ran \\d+ tests? in [\\d\\.]+s|\\n-{70,}")\n end_match = end_pattern.search(log_content, start_index)\n end_index = end_match.start() if end_match else len(log_content)\n failure_summary_block = log_content[start_index:end_index].strip()\n if failure_summary_block:\n failures.append(f"FAILURE SUMMARY BLOCK:\\n{failure_summary_block}")\n self.logger.info("Captured a general failure summary block.")\n return failures\n\n if not failures:\n self.logger.info("No specific unittest failure blocks parsed with available patterns.")\n \n return failures\n\n\n# --- Example Usage (Illustrative) ---\nif __name__ == "__main__":\n # This example assumes you have GITHUB_TOKEN environment variable set\n # And that \'requests\' is installed.\n # Replace with your actual repo owner, name, and PR number.\n pr_number = 206 # Example PR\n repo_owner = "bucolucas" # Example owner\n repo_name = "cyclop" # Example repo\n\n # Setup basic logging for the example\n # In a real app, logger would be configured externally\n logging.basicConfig(level=logging.INFO, format=\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\')\n example_logger = logging.getLogger("GitHubCIHelperExample")\n\n\n # Pass the logger to the helper\n helper = GitHubCIHelper(repo_owner, repo_name, logger_instance=example_logger)\n\n example_logger.info(f"Looking for failed runs for PR #{pr_number} in {repo_owner}/{repo_name}")\n failed_run = helper.get_latest_failed_run_for_pr(pull_request_number=pr_number, workflow_name="Python CI")\n\n if failed_run:\n example_logger.info(f"Found failed run: ID {failed_run[\'id\']}, Status {failed_run[\'conclusion\']}")\n example_logger.info(f"Attempting to download logs for job \'test\' in run {failed_run[\'id\']}...")\n \n log_content = helper.get_job_logs_for_run(run_id=failed_run[\'id\'], job_name="test")\n\n if isinstance(log_content, str) and not log_content.startswith("Error") and not log_content.startswith("Job") and not log_content.startswith("Failed"):\n example_logger.info(f"Successfully downloaded logs (length: {len(log_content)} characters).")\n \n example_logger.info("\\n--- Parsing unittest failures ---")\n failures = helper.parse_unittest_failures_from_log(log_content)\n if failures:\n for i, failure_details in enumerate(failures):\n print(f"\\nFailure {i+1}:\\n{failure_details}")\n else:\n print("No specific unittest failures parsed by the tool.")\n # Consider logging the beginning of the log if parsing fails, for debugging the regexes\n # print(f"Log start:\\n{log_content[:2000]}") \n elif log_content is None:\n example_logger.error("Could not retrieve log content (returned None).")\n else: # If it\'s an error message string from the function itself\n example_logger.error(f"Failed to get/process logs: {log_content}")\n\n else:\n example_logger.info(f"No failed \'Python CI\' workflow run found for PR #{pr_number} or the PR doesn\'t exist/no runs yet.")\n\n