From 6193086a9fd151d1ca9e77c0793da484b7f167be Mon Sep 17 00:00:00 2001 From: Jonathan Lucas Date: Tue, 20 Aug 2024 15:31:10 -0500 Subject: [PATCH] Add abilitiy to comment on and read comments from an issue --- tools/github_tool.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tools/github_tool.py b/tools/github_tool.py index f838a5b..24c5492 100644 --- a/tools/github_tool.py +++ b/tools/github_tool.py @@ -375,6 +375,38 @@ class GitHubTool(BaseTool): } } } + }, + { + "name": "add_issue_comment", + "description": "Add a comment to an issue", + "parameters": { + "type": "object", + "properties": { + "issue_number": { + "type": "integer", + "description": "The number of the issue" + }, + "comment": { + "type": "string", + "description": "The comment to add to the issue" + } + }, + "required": ["issue_number", "comment"] + } + }, + { + "name": "get_issue_comments", + "description": "Get comments for an issue", + "parameters": { + "type": "object", + "properties": { + "issue_number": { + "type": "integer", + "description": "The number of the issue" + } + }, + "required": ["issue_number"] + } } ] @@ -421,6 +453,10 @@ class GitHubTool(BaseTool): return self._create_issue(kwargs["title"], kwargs["body"], kwargs.get("labels", [])) elif function_name == "list_issues": return self._list_issues(kwargs.get("state", "open"), kwargs.get("per_page", 30), kwargs.get("page", 1)) + elif function_name == "add_issue_comment": + return self._add_issue_comment(kwargs["issue_number"], kwargs["comment"]) + elif function_name == "get_issue_comments": + return self._get_issue_comments(kwargs["issue_number"]) else: error_message = f"Unknown function: {function_name}" self.logger.error(error_message) @@ -775,4 +811,42 @@ class GitHubTool(BaseTool): else: error_message = f"Error listing issues: {response.status_code}\nResponse: {response.text}" self.logger.error(error_message) + return error_message + + @metrics.measure + def _add_issue_comment(self, issue_number, comment): + self.logger.info(f"Adding comment to issue: {issue_number}") + url = f"{self.base_url}/repos/{self.repo}/issues/{issue_number}/comments" + data = { + "body": comment + } + response = requests.post(url, headers=self.headers, json=data) + if response.status_code == 201: + comment_data = response.json() + success_message = f"Comment added successfully to issue {issue_number}: {comment_data['html_url']}" + self.logger.info(success_message) + return success_message + else: + error_message = f"Error adding comment to issue: {response.status_code}\nResponse: {response.text}" + self.logger.error(error_message) + return error_message + + @metrics.measure + def _get_issue_comments(self, issue_number): + self.logger.info(f"Getting comments for issue: {issue_number}") + url = f"{self.base_url}/repos/{self.repo}/issues/{issue_number}/comments" + response = requests.get(url, headers=self.headers) + if response.status_code == 200: + comments = [{ + "id": comment["id"], + "user": comment["user"]["login"], + "body": comment["body"], + "created_at": comment["created_at"], + "updated_at": comment["updated_at"] + } for comment in response.json()] + self.logger.info(f"Successfully retrieved comments for issue {issue_number}. Found {len(comments)} comments.") + return comments + else: + error_message = f"Error getting issue comments: {response.status_code}\nResponse: {response.text}" + self.logger.error(error_message) return error_message \ No newline at end of file