bff5e8a02c
Implements a new tool to read the README.md file from the repository root. This tool will allow direct access to the project's documentation. Addresses issue #226
4 lines
67 KiB
Python
4 lines
67 KiB
Python
# tools/github_tool.py\nfrom .base_tool import BaseTool\nimport requests\nimport os\nimport base64\nimport logging\n\nclass GitHubTool(BaseTool):\n def __init__(self, session=None, token=None, repo=None, base_url=None, initial_branch=\"main\", logger=None):\n self.base_url = base_url if base_url else \"https://api.github.com\"\n self._token = token if token else os.environ.get(\"GITHUB_TOKEN\")\n self._repo = repo if repo else os.environ.get(\"GITHUB_REPOSITORY\")\n \n if not self._token:\n # In a real scenario, might raise an error or operate in a degraded mode.\n # For this tool, token is essential.\n raise ValueError(\"GitHub token must be provided either as an argument or via GITHUB_TOKEN env var.\")\n if not self._repo:\n raise ValueError(\"GitHub repository (e.g., \'owner/repo\') must be provided either as an argument or via GITHUB_REPOSITORY env var.\")\n\n if session:\n self.session = session\n else:\n self.session = requests.Session()\n self.session.headers.update({\n \"Authorization\": f\"token {self._token}\",\n \"Accept\": \"application/vnd.github.v3+json\"\n })\n \n self.current_branch = initial_branch\n\n # Use provided logger or get a new one for the module\n # The application using this tool should configure the logging handlers and formatting.\n self.logger = logger if logger else logging.getLogger(__name__)\n # If no handlers are configured by the application, add a NullHandler\n # to prevent \"No handler found\" warnings if the tool logs something.\n if not self.logger.handlers:\n self.logger.addHandler(logging.NullHandler())\n\n def clear(self):\n if self.current_branch != \"main\":\n self._set_current_branch(\"main\")\n self.logger.info(f\"GitHubTool state cleared. Current branch is {self.current_branch}\")\n\n def get_functions(self):\n return [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"read_file\",\n \"description\": \"Read a file from the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"path\": {\"type\": \"string\", \"description\": \"Path to the file in the repository\"}\n },\n \"required\": [\"path\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"read_readme\",\n \"description\": \"Read the README.md file from the root of the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {}\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"list_files\",\n \"description\": \"List files in a directory of the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"path\": {\"type\": \"string\", \"description\": \"Path to the directory in the repository\"}\n },\n \"required\": [\"path\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"search_code\",\n \"description\": \"Search for code in the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"query\": {\"type\": \"string\", \"description\": \"Search query\"}\n },\n \"required\": [\"query\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_branch\",\n \"description\": \"Create a new branch in the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"branch_name\": {\"type\": \"string\", \"description\": \"Name of the new branch\"},\n \"base_branch\": {\"type\": \"string\", \"description\": \"Name of the base branch\", \"default\": \"main\"}\n },\n \"required\": [\"branch_name\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"commit_file\",\n \"description\": \"Commit a file to a branch (not main)\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"file_path\": {\"type\": \"string\", \"description\": \"Path to the file in the repository\"},\n \"commit_message\": {\"type\": \"string\", \"description\": \"Commit message\"},\n \"content\": {\"type\": \"string\", \"description\": \"Content of the file\"}\n },\n \"required\": [\"file_path\", \"commit_message\", \"content\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_pull_request\",\n \"description\": \"Create a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"title\": {\"type\": \"string\", \"description\": \"Title of the pull request\"},\n \"body\": {\"type\": \"string\", \"description\": \"Body of the pull request\"},\n \"base\": {\"type\": \"string\", \"description\": \"The name of the branch you want the changes pulled into\", \"default\": \"main\"}\n },\
|
|
\"required\": [\"title\", \"body\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_commit_history\",\n \"description\": \"Get commit history for a file\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"file_path\": {\"type\": \"string\", \"description\": \"Path to the file in the repository\"},\n \"num_commits\": {\"type\": \"integer\", \"description\": \"Number of commits to retrieve\", \"default\": 10}\n },\n \"required\": [\"file_path\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"view_commit_details_for_file\",\n \"description\": \"View commit history and details for a specific file, including commit messages.\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"file_path\": {\"type\": \"string\", \"description\": \"Path to the file in the repository\"},\n \"num_commits\": {\"type\": \"integer\", \"description\": \"Number of commits to retrieve\", \"default\": 10}\n },\n \"required\": [\"file_path\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_branch_sha\",\n \"description\": \"Get the SHA of the latest commit on a branch\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"branch\": {\"type\": \"string\", \"description\": \"Name of the branch\"}\n },\n \"required\": [\"branch\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_current_branch\",\n \"description\": \"Get the name of the current branch\",\n \"parameters\": { \"type\": \"object\", \"properties\": {} }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"set_current_branch\",\n \"description\": \"Set the current branch\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"branch_name\": {\"type\": \"string\", \"description\": \"Name of the branch to set as current\"}\n },\n \"required\": [\"branch_name\"]\n }\n },\n \"_tags\": [\"read\", \"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_file_at_commit\",\n \"description\": \"Get the contents of a file at a specific commit\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"file_path\": {\"type\": \"string\", \"description\": \"Path to the file in the repository\"},\n \"commit_sha\": {\"type\": \"string\", \"description\": \"SHA of the commit to retrieve the file from\"}\n },\n \"required\": [\"file_path\", \"commit_sha\"]\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"list_branches\",\n \"description\": \"List all branches in the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"per_page\": {\"type\": \"integer\", \"description\": \"Number of branches to return per page (max 100)\", \"default\": 100},\n \"all_pages\": {\"type\": \"boolean\", \"description\": \"Whether to fetch all pages of results\", \"default\": True}\n }\n }\n },\n \"_tags\": [\"read\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"approve_pull_request\",\n \"description\": \"Approve a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"close_pull_request\",\n \"description\": \"Close a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"merge_pull_request\",\n \"description\": \"Merge a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"},\n \"commit_title\": {\"type\": \"string\", \"description\": \"Title for the automatic commit message\", \"default\": \"Merge pull request\"},\n \"commit_message\": {\"type\": \"string\", \"description\": \"Extra detail to append to automatic commit message\", \"default\": \"\"},\n \"merge_method\": {\n \"type\": \"string\",\n \"description\": \"Merge method to use\",\n \"enum\": [\"merge\", \"squash\", \"rebase\"],\n \"default\": \"merge\"\n }\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"delete_branch\",\n \"description\": \"Delete a branch\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"branch_name\": {\"type\": \"string\", \"description\": \"Name of the branch to delete\"}\n },\
|
|
\"required\": [\"branch_name\"]\n }\n },\n \"_tags\": [\"write\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_issue_details\",\n \"description\": \"Get details of a specific issue\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"issue_number\": {\"type\": \"integer\", \"description\": \"The number of the issue\"}\n },\n \"required\": [\"issue_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_issue\",\n \"description\": \"Create a new issue in the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"title\": {\"type\": \"string\", \"description\": \"Title of the issue\"},\n \"body\": {\"type\": \"string\", \"description\": \"Body of the issue\"},\n \"labels\": {\n \"type\": \"array\",\n \"items\": {\"type\": \"string\"},\n \"description\": \"Labels to apply to the issue\"\n }\n },\n \"required\": [\"title\", \"body\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"list_issues\",\n \"description\": \"List issues in the repository\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"state\": {\"type\": \"string\", \"enum\": [\"open\", \"closed\", \"all\"], \"default\": \"open\", \"description\": \"State of the issues to retrieve\"},\n \"per_page\": {\"type\": \"integer\", \"default\": 30, \"description\": \"Number of issues to return per page\"},\n \"page\": {\"type\": \"integer\", \"default\": 1, \"description\": \"Page number of the results to fetch\"}\n }\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"add_issue_comment\",\n \"description\": \"Add a comment to an issue\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"issue_number\": {\"type\": \"integer\", \"description\": \"The number of the issue\"},\n \"comment\": {\"type\": \"string\", \"description\": \"The comment to add to the issue\"}\n },\
|
|
\"required\": [\"issue_number\", \"comment\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_issue_comments\",\n \"description\": \"Get comments for an issue\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"issue_number\": {\"type\": \"integer\", \"description\": \"The number of the issue\"}\n },\n \"required\": [\"issue_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n { \n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_pull_request_general_comments\",\n \"description\": \"Get general comments posted on a pull request itself (not specific to file lines).\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request.\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_project_board\",\n \"description\": \"Create a new project board\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\"type\": \"string\", \"description\": \"Name of the project board\"},\n \"body\": {\"type\": \"string\", \"description\": \"Body of the project board\"}\n },\n \"required\": [\"name\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_project_column\",\n \"description\": \"Create a new column in a project board\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"project_id\": {\"type\": \"integer\", \"description\": \"ID of the project board\"},\n \"column_name\": {\"type\": \"string\", \"description\": \"Name of the column\"}\n },\n \"required\": [\"project_id\", \"column_name\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_project_card\",\n \"description\": \"Create a new card in a project column\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"column_id\": {\"type\": \"integer\", \"description\": \"ID of the project column\"},\n \"note\": {\"type\": \"string\", \"description\": \"Note for the project card\"}\n },\n \"required\": [\"column_id\", \"note\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"move_project_card\",\n \"description\": \"Move a card to a new position\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"card_id\": {\"type\": \"integer\", \"description\": \"ID of the project card\"},\n \"position\": {\"type\": \"string\", \"description\": \"New position of the card\"},\n \"column_id\": {\"type\": \"integer\", \"description\": \"ID of the target column\"}\n },\n \"required\": [\"card_id\", \"position\", \"column_id\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"link_issue_to_project_card\",\n \"description\": \"Link an issue or pull request to a project card\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"card_id\": {\"type\": \"integer\", \"description\": \"ID of the project card\"},\n \"content_id\": {\"type\": \"integer\", \"description\": \"ID of the issue or pull request\"},\n \"content_type\": {\"type\": \"string\", \"description\": \"Type of the content (Issue or PullRequest)\"}\n },\n \"required\": [\"card_id\", \"content_id\", \"content_type\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"list_project_boards\",\n \"description\": \"List project boards associated with the repository\",\n \"parameters\": { \"type\": \"object\", \"properties\": {} }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"view_project_board_items\",\n \"description\": \"View items (columns and cards) in a specific project board\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"project_id\": {\"type\": \"integer\", \"description\": \"ID of the project board\"}\n },\n \"required\": [\"project_id\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_pull_request_details\",\n \"description\": \"Get detailed information about a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_pull_request_diff\",\n \"description\": \"Get the diff of a pull request\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_pull_request_files\",\n \"description\": \"Get a list of files changed in a pull request, with their patch details\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"create_pull_request_review_comment\",\n \"description\": \"Add a comment to a specific line of a file in a pull request review\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"},\n \"body\": {\"type\": \"string\", \"description\": \"The text of the comment\"},\n \"commit_id\": {\"type\": \"string\", \"description\": \"The SHA of the commit to comment on\"},\n \"path\": {\"type\": \"string\", \"description\": \"The path to the file being commented on\"},\n \"position\": {\"type\": \"integer\", \"description\": \"The line index in the diff to comment on (starting at 1)\"},\n \"side\": {\"type\": \"string\", \"enum\": [\"LEFT\", \"RIGHT\"], \"description\": \"The side of the diff to comment on (LEFT for old file, RIGHT for new file)\", \"default\": \"RIGHT\"},\n \"start_line\": {\"type\": \"integer\", \"description\": \"The start line of the diff if commenting on a range\"},\n \"start_side\": {\"type\": \"string\", \"enum\": [\"LEFT\", \"RIGHT\"], \"description\": \"The side of the diff for the start line\"}\n },\n \"required\": [\"pull_number\", \"body\", \"commit_id\", \"path\", \"position\"]\n }\n },\n \"_tags\": [\"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"list_pull_request_review_comments\",\n \"description\": \"List comments on a pull request review\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"}\n },\n \"required\": [\"pull_number\"]\n }\n },\n \"_tags\": [\"read\", \"communicate\"]\n },\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"submit_pull_request_review\",\n \"description\": \"Submit a formal pull request review (APPROVE, REQUEST_CHANGES, COMMENT)\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"pull_number\": {\"type\": \"integer\", \"description\": \"The number of the pull request\"},\n \"event\": {\"type\": \"string\", \"enum\": [\"APPROVE\", \"REQUEST_CHANGES\", \"COMMENT\"], \"description\": \"The type of review event\"},\n \"body\": {\"type\": \"string\", \"description\": \"The body of the review (required for REQUEST_CHANGES, optional for others)\"}\n },\n \"required\": [\"pull_number\", \"event\"]\n }\n },\n \"_tags\": [\"communicate\"]\n }\n ]\n\n def execute(self, function_name, **kwargs):\n self.logger.info(f\"Executing GitHub Tool function: {function_name} with args: {kwargs}\")\n # Dispatch to the appropriate private method\n method_name = f\"_{function_name}\"\n if hasattr(self, method_name):\n method = getattr(self, method_name)\n try:\n return method(**kwargs) # Ensure only expected args are passed if method signature is strict\n except Exception as e:\n self.logger.error(f\"Error executing {method_name}: {e}\", exc_info=True)\n return f\"Error during {function_name} execution: {str(e)}\"\n else:\n error_message = f\"Unknown function: {function_name}\"\n self.logger.error(error_message)\n return error_message\n\n # Private methods for each function, using self.session for HTTP requests\n\n def _read_file(self, path):\n self.logger.info(f\"Reading file: {path} from branch: {self.current_branch}\")\n url = f\"{self.base_url}/repos/{self._repo}/contents/{path}\"\n response = self.session.get(url, params={\"ref\": self.current_branch})\n if response.status_code == 200:\n content = response.json()[\"content\"]\n decoded_content = base64.b64decode(content).decode(\'utf-8\')\n self.logger.info(f\"Successfully read file: {path}\")\n return decoded_content\n else:\n error_message = f\"Error reading file ({path}): {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _read_readme(self):\n self.logger.info(\"Reading README.md from the root of the repository.\")\n return self._read_file(\"README.md\")\n\n def _create_branch(self, branch_name, base_branch=\"main\"):\n self.logger.info(f\"Creating branch: {branch_name} from base: {base_branch}\")\n # Get SHA of base branch\n ref_url = f\"{self.base_url}/repos/{self._repo}/git/refs/heads/{base_branch}\"\n response_sha = self.session.get(ref_url)\n if response_sha.status_code != 200:\n error_message = f\"Error getting base branch SHA ({base_branch}): {response_sha.status_code} - {response_sha.text}\"\n self.logger.error(error_message)\n return error_message\n sha = response_sha.json()[\"object\"][\"sha\"]\n \n # Create new branch\n create_ref_url = f\"{self.base_url}/repos/{self._repo}/git/refs\"\n data = {\"ref\": f\"refs/heads/{branch_name}\", \"sha\": sha}\n response_create = self.session.post(create_ref_url, json=data)\n if response_create.status_code == 201:\n self.current_branch = branch_name\n success_message = f\"Branch \'{branch_name}\' created successfully from \'{base_branch}\' and set as current branch.\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error creating branch \'{branch_name}\': {response_create.status_code} - {response_create.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _commit_file(self, file_path, content, commit_message):\n self.logger.info(f\"Committing file: {file_path} to branch: {self.current_branch} with message: \'{commit_message}\'\")\n if self.current_branch == \"main\":\n error_message = \"Action directly to main branch is not allowed. Please create and switch to a new branch first.\"\n self.logger.warning(error_message)\n return error_message\n\n url = f\"{self.base_url}/repos/{self._repo}/contents/{file_path}\"\n encoded_content = base64.b64encode(content.encode(\'utf-8\')).decode(\'utf-8\')\n data = {\n \"message\": commit_message,\n \"content\": encoded_content,\n \"branch\": self.current_branch\n }\n\n # Check if file exists to get its SHA for update\n self.logger.info(f\"Checking if file \'{file_path}\' exists on branch \'{self.current_branch}\'\")\n get_response = self.session.get(url, params={\"ref\": self.current_branch})\n if get_response.status_code == 200:\n data[\"sha\"] = get_response.json()[\"sha\"]\n self.logger.info(f\"File \'{file_path}\' exists, will update.\")\n elif get_response.status_code == 404:\n self.logger.info(f\"File \'{file_path}\' does not exist, will create.\")\n else:\n error_message = f\"Error checking file existence for \'{file_path}\': {get_response.status_code} - {get_response.text}\"\n self.logger.error(error_message)\n return error_message\n\n response = self.session.put(url, json=data)\n if response.status_code in [200, 201]: # 200 for update, 201 for create\n commit_sha = response.json().get(\"commit\", {}).get(\"sha\", \"N/A\")\n success_message = f\"File \'{file_path}\' committed successfully to branch \'{self.current_branch}\'. Commit SHA: {commit_sha}\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error committing file \'{file_path}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _create_pull_request(self, title, body, base=\"main\"):\n self.logger.info(f\"Creating pull request: \'{title}\' from branch \'{self.current_branch}\' to \'{base}\'\")\n if self.current_branch == base:\n error_message = f\"Cannot create a pull request from branch \'{self.current_branch}\' to itself (\'{base}\').\"\n self.logger.warning(error_message)\n return error_message\n \n url = f\"{self.base_url}/repos/{self._repo}/pulls\"\n data = {\"title\": title, \"body\": body, \"head\": self.current_branch, \"base\": base}\n response = self.session.post(url, json=data)\n if response.status_code == 201:\n pr_html_url = response.json().get(\"html_url\", \"N/A\")\n pr_number = response.json().get(\"number\", \"N/A\")\n success_message = f\"Pull request \'{title}\' created successfully: {pr_html_url} (Number: {pr_number})\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error creating pull request: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_branch_sha(self, branch):\n self.logger.info(f\"Getting SHA for branch: {branch}\")\n url = f\"{self.base_url}/repos/{self._repo}/git/refs/heads/{branch}\"\n response = self.session.get(url)\n if response.status_code == 200:\n sha = response.json()[\"object\"][\"sha\"]\n self.logger.info(f\"SHA for branch \'{branch}\' is {sha}\")\n return sha\n else:\n error_message = f\"Error getting SHA for branch \'{branch}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _list_files(self, path):\n self.logger.info(f\"Listing files in path: \'{path}\' on branch: \'{self.current_branch}\'\")\n url = f\"{self.base_url}/repos/{self._repo}/contents/{path.strip(\'/\')}\" # Ensure no leading/trailing slashes for consistency\n response = self.session.get(url, params={\"ref\": self.current_branch})\n if response.status_code == 200:\n items = response.json()\n results = []\n if isinstance(items, list): # It\'s a directory listing\n for item in items:\n results.append({\"name\": item[\"name\"], \"type\": item[\"type\"], \"path\": item[\"path\"]})\n elif isinstance(items, dict) and \'type\' in items: # It\'s a single file response\n results.append({\"name\": items[\"name\"], \"type\": items[\"type\"], \"path\": items[\"path\"]})\n self.logger.info(f\"Successfully listed {len(results)} items in \'{path}\'.\")\n return results\n elif response.status_code == 404:\n self.logger.warning(f\"Path \'{path}\' not found on branch \'{self.current_branch}\'.\")\n return f\"Error: Path \'{path}\' not found.\"\n else:\n error_message = f\"Error listing files in \'{path}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _search_code(self, query):\n self.logger.info(f\"Searching code with query: \'{query}\' in repo: \'{self._repo}\'\")\n url = f\"{self.base_url}/search/code\"\n params = {\"q\": f\"{query} repo:{self._repo}\"}\n response = self.session.get(url, params=params)\n if response.status_code == 200:\n search_results = response.json().get(\"items\", [])\n results = [{\"path\": item[\"path\"], \"url\": item[\"html_url\"]} for item in search_results]\n self.logger.info(f\"Code search for \'{query}\' found {len(results)} items.\")\n return results\n else:\n error_message = f\"Error searching code for \'{query}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_commit_history(self, file_path, num_commits=10):\n self.logger.info(f\"Getting last {num_commits} commit(s) for file: \'{file_path}\' on branch \'{self.current_branch}\'\")\n url = f\"{self.base_url}/repos/{self._repo}/commits\"\n params = {\"path\": file_path, \"sha\": self.current_branch, \"per_page\": num_commits}\n response = self.session.get(url, params=params)\n if response.status_code == 200:\n commits_data = response.json()\n commits = [{\n \"sha\": commit[\"sha\"],\n \"message\": commit[\"commit\"][\"message\"],\n \"author\": commit[\"commit\"][\"author\"][\"name\"],\n \"date\": commit[\"commit\"][\"author\"][\"date\"]\n } for commit in commits_data]\n self.logger.info(f\"Successfully retrieved {len(commits)} commit(s) for \'{file_path}\'.\")\n return commits\n else:\n error_message = f\"Error getting commit history for \'{file_path}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _view_commit_details_for_file(self, file_path, num_commits=10):\n # This function is essentially the same as get_commit_history based on its description.\n self.logger.info(f\"Viewing commit details for file \'{file_path}\' (last {num_commits} commits) - using _get_commit_history.\")\n return self._get_commit_history(file_path, num_commits)\n\n def _get_current_branch(self):\n self.logger.info(f\"Current branch is: {self.current_branch}\")\n return self.current_branch\n\n def _set_current_branch(self, branch_name):\n self.logger.info(f\"Attempting to set current branch to: {branch_name}\")\n # Check if branch exists by trying to get its SHA\n sha_info = self._get_branch_sha(branch_name)\n if isinstance(sha_info, str) and sha_info.startswith(\"Error getting SHA\"): # Crude check for error string\n error_message = f\"Cannot set current branch: Branch \'{branch_name}\' not found or error accessing it. Details: {sha_info}\"\n self.logger.warning(error_message)\n return error_message \n \n self.current_branch = branch_name\n success_message = f\"Current branch set to: {self.current_branch}\"\n self.logger.info(success_message)\n return success_message\n\n def _get_file_at_commit(self, file_path, commit_sha):\n self.logger.info(f\"Getting file \'{file_path}\' at commit SHA: {commit_sha}\")\n url = f\"{self.base_url}/repos/{self._repo}/contents/{file_path}\"\n response = self.session.get(url, params={\"ref\": commit_sha})\n if response.status_code == 200:\n content = response.json()[\"content\"]\n decoded_content = base64.b64decode(content).decode(\'utf-8\')\n self.logger.info(f\"Successfully retrieved file \'{file_path}\' at commit {commit_sha}.\")\n return decoded_content\n else:\n error_message = f\"Error reading file \'{file_path}\' at commit {commit_sha}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _list_branches(self, per_page=100, all_pages=True):\n self.logger.info(f\"Listing branches for repo \'{self._repo}\'. Per_page={per_page}, All_pages={all_pages}\")\n url = f\"{self.base_url}/repos/{self._repo}/branches\"\n params = {\"per_page\": min(per_page, 100)} # Respect GitHub API limit\n branches_list = []\n page = 1\n while url:\n self.logger.debug(f\"Fetching page {page} from {url} with params {params if page==1 else {}}\")\n response = self.session.get(url, params=params if page == 1 else None) # params only for first page if paginating via links\n if response.status_code != 200:\n error_message = f\"Error listing branches: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n \n current_page_branches = [branch[\"name\"] for branch in response.json()]\n branches_list.extend(current_page_branches)\n self.logger.debug(f\"Fetched {len(current_page_branches)} branches on page {page}.\")\n\n if not all_pages or not response.links.get(\"next\"):\n break\n url = response.links[\"next\"][\"url\"]\n page += 1\n params = {} # Clear params for subsequent calls using a link that includes them\n\n self.logger.info(f\"Successfully listed {len(branches_list)} branches.\")\n return branches_list\n\n def _approve_pull_request(self, pull_number):\n self.logger.info(f\"Approving pull request #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/reviews\"\n data = {\"event\": \"APPROVE\"}\n response = self.session.post(url, json=data)\n if response.status_code == 200:\n success_message = f\"Pull request #{pull_number} approved successfully.\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error approving pull request #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _close_pull_request(self, pull_number):\n self.logger.info(f\"Closing pull request #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}\"\n data = {\"state\": \"closed\"}\n response = self.session.patch(url, json=data) # Use PATCH for update\n if response.status_code == 200:\n success_message = f\"Pull request #{pull_number} closed successfully.\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error closing pull request #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _merge_pull_request(self, pull_number, commit_title=\"Merge pull request\", commit_message=\"\", merge_method=\"merge\"):\n self.logger.info(f\"Merging pull request #{pull_number} using method \'{merge_method}\'\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/merge\"\n data = {\"commit_title\": commit_title, \"commit_message\": commit_message, \"merge_method\": merge_method}\n response = self.session.put(url, json=data)\n if response.status_code == 200:\n success_message = f\"Pull request #{pull_number} merged successfully.\"\n self.logger.info(success_message)\n return success_message\n elif response.status_code == 405: # Method Not Allowed (e.g., PR not mergeable)\n error_message = f\"Error merging pull request #{pull_number}: Not mergeable. {response.json().get(\'message\', response.text)}\"\n self.logger.warning(error_message)\n return error_message\n elif response.status_code == 409: # Conflict\n error_message = f\"Error merging pull request #{pull_number}: Merge conflict. {response.json().get(\'message\', response.text)}\"\n self.logger.warning(error_message)\n return error_message\n else:\n error_message = f\"Error merging pull request #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _delete_branch(self, branch_name):\n self.logger.info(f\"Deleting branch: {branch_name}\")\n if branch_name == \"main\" or (hasattr(self, \'default_branch\') and branch_name == self.default_branch) :\n # Add a check for a configurable default branch if necessary\n error_message = f\"Cannot delete protected branch: {branch_name}\"\n self.logger.warning(error_message)\n return error_message\n\n url = f\"{self.base_url}/repos/{self._repo}/git/refs/heads/{branch_name}\"\n response = self.session.delete(url)\n if response.status_code == 204:\n success_message = f\"Branch \'{branch_name}\' deleted successfully.\"\n self.logger.info(success_message)\n if self.current_branch == branch_name:\n self.current_branch = \"main\" # Or some other default\n self.logger.info(f\"Current branch was {branch_name}, reset to {self.current_branch}.\")\n return success_message\n else:\n error_message = f\"Error deleting branch \'{branch_name}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_issue_details(self, issue_number):\n self.logger.info(f\"Getting details for issue #{issue_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/issues/{issue_number}\"\n response = self.session.get(url)\n if response.status_code == 200:\n self.logger.info(f\"Successfully retrieved details for issue #{issue_number}.\")\n return response.json() # Return raw JSON data for now\n else:\n error_message = f\"Error getting details for issue #{issue_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _create_issue(self, title, body, labels=None):\n self.logger.info(f\"Creating new issue with title: \'{title}\'\")\n url = f\"{self.base_url}/repos/{self._repo}/issues\"\n data = {\"title\": title, \"body\": body}\n if labels: # Ensure labels is a list of strings\n data[\"labels\"] = labels if isinstance(labels, list) else [labels]\n \n response = self.session.post(url, json=data)\n if response.status_code == 201:\n issue_html_url = response.json().get(\"html_url\", \"N/A\")\n issue_number = response.json().get(\"number\", \"N/A\")\n success_message = f\"Issue \'{title}\' created successfully: {issue_html_url} (Number: {issue_number})\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error creating issue \'{title}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _list_issues(self, state=\"open\", per_page=30, page=1):\n self.logger.info(f\"Listing issues with state: {state}, per_page: {per_page}, page: {page}\")\n url = f\"{self.base_url}/repos/{self._repo}/issues\"\n params = {\"state\": state, \"per_page\": per_page, \"page\": page}\n response = self.session.get(url, params=params)\n if response.status_code == 200:\n issues_data = response.json()\n self.logger.info(f\"Successfully listed {len(issues_data)} issues.\")\n # Return a summary or full data based on needs\n return [{ \"title\": i[\"title\"], \"number\": i[\"number\"], \"state\": i[\"state\"], \"url\": i[\"html_url\"] } for i in issues_data]\n else:\n error_message = f\"Error listing issues: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _add_issue_comment(self, issue_number, comment):\n self.logger.info(f\"Adding comment to issue #{issue_number}: \'{comment[:50]}...\'\")\n url = f\"{self.base_url}/repos/{self._repo}/issues/{issue_number}/comments\"\n data = {\"body\": comment}\n response = self.session.post(url, json=data)\n if response.status_code == 201:\n comment_html_url = response.json().get(\"html_url\", \"N/A\")\n success_message = f\"Comment added to issue #{issue_number} successfully: {comment_html_url}\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error adding comment to issue #{issue_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_issue_comments(self, issue_number):\n self.logger.info(f\"Getting comments for issue #{issue_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/issues/{issue_number}/comments\"\n response = self.session.get(url)\n if response.status_code == 200:\n comments_data = response.json()\n self.logger.info(f\"Successfully retrieved {len(comments_data)} comments for issue #{issue_number}.\")\n # Return summary or full data\n return [{ \"user\": c[\"user\"][\"login\"], \"body\": c[\"body\"], \"created_at\": c[\"created_at\"] } for c in comments_data]\n else:\n error_message = f\"Error getting comments for issue #{issue_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_pull_request_general_comments(self, pull_number):\n self.logger.info(f\"Getting general comments for pull request #{pull_number}\")\n # In GitHub API, PR comments (general, not review comments on lines) are issue comments.\n # The PR is also an issue, so use the issue comments endpoint.\n return self._get_issue_comments(issue_number=pull_number)\n\n def _create_project_board(self, name, body=None):\n self.logger.info(f\"Creating project board: \'{name}\'\")\n url = f\"{self.base_url}/repos/{self._repo}/projects\"\n headers = self.session.headers.copy() # Get existing session headers\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\" # Required for Projects API\n data = {\"name\": name}\n if body: data[\"body\"] = body\n response = self.session.post(url, headers=headers, json=data)\n if response.status_code == 201:\n project_data = response.json()\n success_message = f\"Project board \'{name}\' created successfully with ID: {project_data[\'id\']}\"\n self.logger.info(success_message)\n return project_data # Return full project data\n else:\n error_message = f\"Error creating project board \'{name}\': {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _create_project_column(self, project_id, column_name):\n self.logger.info(f\"Creating column \'{column_name}\' for project ID: {project_id}\")\n url = f\"{self.base_url}/projects/{project_id}/columns\"\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n data = {\"name\": column_name}\n response = self.session.post(url, headers=headers, json=data)\n if response.status_code == 201:\n column_data = response.json()\n success_message = f\"Column \'{column_name}\' created successfully for project {project_id} with ID: {column_data[\'id\']}\"\n self.logger.info(success_message)\n return column_data\n else:\n error_message = f\"Error creating column \'{column_name}\' for project {project_id}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _create_project_card(self, column_id, note=None, content_id=None, content_type=None):\n self.logger.info(f\"Creating card in column ID: {column_id}\")\n url = f\"{self.base_url}/projects/columns/{column_id}/cards\"\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n data = {}\n if note:\n data[\"note\"] = note\n if content_id and content_type:\n data[\"content_id\"] = content_id\n data[\"content_type\"] = content_type\n elif (content_id and not content_type) or (not content_id and content_type):\n err = \"Both content_id and content_type must be provided to link content to a project card.\"\n self.logger.warning(err)\n return err\n \n if not data:\n return \"Error: Card must have a note or content to link.\"\n\n response = self.session.post(url, headers=headers, json=data)\n if response.status_code == 201:\n card_data = response.json()\n success_message = f\"Card created successfully in column {column_id} with ID: {card_data[\'id\']}\"\n self.logger.info(success_message)\n return card_data\n else:\n error_message = f\"Error creating card in column {column_id}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _move_project_card(self, card_id, position, column_id=None):\n self.logger.info(f\"Moving card ID: {card_id} to position: {position}\" + (f\" in column ID: {column_id}\" if column_id else \"\"))\n url = f\"{self.base_url}/projects/columns/cards/{card_id}/moves\"\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n data = {\"position\": position}\n if column_id:\n data[\"column_id\"] = column_id\n \n response = self.session.post(url, headers=headers, json=data)\n if response.status_code == 201: # Successful move returns 201 with empty body\n success_message = f\"Card {card_id} moved successfully to position {position}\" + (f\" in column {column_id}\" if column_id else \".\")\n self.logger.info(success_message)\n return success_message # Return success message as body is empty\n else:\n error_message = f\"Error moving card {card_id}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n # _link_issue_to_project_card is effectively handled by _create_project_card if content_id and content_type are passed.\n # The API used to have a separate link endpoint, but now it is part of card creation/update.\n # For updating an existing card to link an issue, one would PATCH the card\'s content_id/content_type.\n # Let\'s assume the function intends to update an existing card if it\'s a separate function.\n # However, the provided API spec for `link_issue_to_project_card` uses PATCH on card_id, so let\'s implement that.\n def _link_issue_to_project_card(self, card_id, content_id, content_type):\n self.logger.info(f\"Linking content_id {content_id} (type: {content_type}) to card_id {card_id}\")\n url = f\"{self.base_url}/projects/cards/{card_id}\" # Note: API docs suggest /projects/columns/cards/{card_id} or /projects/cards/{card_id}\n # Using /projects/cards/{card_id} as it seems more general for card update.\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n data = {\"content_id\": content_id, \"content_type\": content_type}\n\n response = self.session.patch(url, headers=headers, json=data)\n if response.status_code == 200:\n updated_card = response.json()\n success_message = f\"{content_type} {content_id} linked to card {card_id} successfully.\"\n self.logger.info(success_message)\n return updated_card\n else:\n error_message = f\"Error linking {content_type} {content_id} to card {card_id}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _list_project_boards(self):\n self.logger.info(f\"Listing project boards for repo: {self._repo}\")\n url = f\"{self.base_url}/repos/{self._repo}/projects\"\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n response = self.session.get(url, headers=headers)\n if response.status_code == 200:\n projects_data = response.json()\n self.logger.info(f\"Successfully listed {len(projects_data)} project boards.\")\n return projects_data\n else:\n error_message = f\"Error listing project boards: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _view_project_board_items(self, project_id):\n self.logger.info(f\"Viewing items for project ID: {project_id}\")\n columns_url = f\"{self.base_url}/projects/{project_id}/columns\"\n headers = self.session.headers.copy()\n headers[\"Accept\"] = \"application/vnd.github.inertia-preview+json\"\n \n columns_response = self.session.get(columns_url, headers=headers)\n if columns_response.status_code != 200:\n error_message = f\"Error fetching columns for project {project_id}: {columns_response.status_code} - {columns_response.text}\"\n self.logger.error(error_message)\n return error_message\n \n columns_data = columns_response.json()\n project_items = []\n for column in columns_data:\n column_info = {\"id\": column[\"id\"], \"name\": column[\"name\"], \"cards\": []}\n cards_url = column[\"cards_url\"]\n cards_response = self.session.get(cards_url, headers=headers)\n if cards_response.status_code == 200:\n column_info[\"cards\"] = cards_response.json()\n else:\n self.logger.error(f\"Error fetching cards for column {column[\'id\']}(\'{column[\'name\']}\'): {cards_response.status_code} - {cards_response.text}\")\n column_info[\"cards\"] = \"Error fetching cards\"\n project_items.append(column_info)\n \n self.logger.info(f\"Successfully retrieved items for project ID: {project_id}.\")\n return project_items\n\n def _get_pull_request_details(self, pull_number):\n self.logger.info(f\"Getting details for PR #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}\"\n response = self.session.get(url)\n if response.status_code == 200:\n self.logger.info(f\"Successfully retrieved details for PR #{pull_number}.\")\n return response.json()\n else:\n error_message = f\"Error getting details for PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_pull_request_diff(self, pull_number):\n self.logger.info(f\"Getting diff for PR #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}\"\n diff_headers = self.session.headers.copy()\n diff_headers[\"Accept\"] = \"application/vnd.github.diff\"\n response = self.session.get(url, headers=diff_headers)\n if response.status_code == 200:\n self.logger.info(f\"Successfully retrieved diff for PR #{pull_number}.\")\n return response.text\n else:\n error_message = f\"Error getting diff for PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _get_pull_request_files(self, pull_number):\n self.logger.info(f\"Getting files for PR #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/files\"\n response = self.session.get(url)\n if response.status_code == 200:\n self.logger.info(f\"Successfully retrieved files for PR #{pull_number}.\")\n return response.json()\n else:\n error_message = f\"Error getting files for PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _create_pull_request_review_comment(self, pull_number, body, commit_id, path, position, side=\"RIGHT\", start_line=None, start_side=None):\n self.logger.info(f\"Creating review comment on PR #{pull_number}, file \'{path}\', position {position}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/comments\"\n data = {\"body\": body, \"commit_id\": commit_id, \"path\": path, \"position\": position, \"side\": side}\n if start_line is not None: data[\"start_line\"] = start_line\n if start_side is not None: data[\"start_side\"] = start_side\n \n response = self.session.post(url, json=data)\n if response.status_code == 201:\n comment_url = response.json().get(\"html_url\", \"N/A\")\n success_message = f\"Review comment created successfully on PR #{pull_number}: {comment_url}\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error creating review comment on PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _list_pull_request_review_comments(self, pull_number):\n self.logger.info(f\"Listing review comments for PR #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/comments\"\n response = self.session.get(url)\n if response.status_code == 200:\n self.logger.info(f\"Successfully retrieved review comments for PR #{pull_number}.\")\n return response.json()\n else:\n error_message = f\"Error listing review comments for PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n\n def _submit_pull_request_review(self, pull_number, event, body=None):\n self.logger.info(f\"Submitting \'{event}\' review for PR #{pull_number}\")\n url = f\"{self.base_url}/repos/{self._repo}/pulls/{pull_number}/reviews\"\n data = {\"event\": event.upper()} # Ensure event is uppercase as per API\n if body: data[\"body\"] = body\n \n response = self.session.post(url, json=data)\n if response.status_code == 200:\n review_url = response.json().get(\"html_url\", \"N/A\")\n success_message = f\"Review ({event}) submitted successfully for PR #{pull_number}: {review_url}\"\n self.logger.info(success_message)\n return success_message\n else:\n error_message = f\"Error submitting review for PR #{pull_number}: {response.status_code} - {response.text}\"\n self.logger.error(error_message)\n return error_message\n |