From bcea167299cd9b37fe4567ad72cac6d4b7dc3ecd Mon Sep 17 00:00:00 2001 From: bucolucas Date: Mon, 19 Aug 2024 15:46:42 -0500 Subject: [PATCH] Add GetCommitHistory class for get_commit_history function with JSON definition --- .../get_commit_history.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tools/github_tool_functions/get_commit_history.py diff --git a/tools/github_tool_functions/get_commit_history.py b/tools/github_tool_functions/get_commit_history.py new file mode 100644 index 0000000..a6d47d2 --- /dev/null +++ b/tools/github_tool_functions/get_commit_history.py @@ -0,0 +1,70 @@ +import requests +import logging + +class GetCommitHistory: + def __init__(self, base_url, token, repo): + self.base_url = base_url + self.headers = { + "Authorization": f"token {token}", + "Accept": "application/vnd.github.v3+json" + } + self.repo = repo + + # Set up logging + self.logger = logging.getLogger(__name__) + self.logger.setLevel(logging.INFO) + + # Create a file handler + file_handler = logging.FileHandler('get_commit_history.log') + file_handler.setLevel(logging.INFO) + + # Create a console handler + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.INFO) + + # Create a formatting for the logs + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + file_handler.setFormatter(formatter) + console_handler.setFormatter(formatter) + + # Add the handlers to the logger + self.logger.addHandler(file_handler) + self.logger.addHandler(console_handler) + + def __call__(self, file_path, num_commits=10): + self.logger.info(f"Getting commit history for file: {file_path}, number of commits: {num_commits}") + url = f"{self.base_url}/repos/{self.repo}/commits" + params = { + "path": file_path, + "per_page": num_commits + } + response = requests.get(url, headers=self.headers, params=params) + if response.status_code == 200: + commits = [{"sha": commit["sha"], "message": commit["commit"]["message"], "date": commit["commit"]["author"]["date"]} for commit in response.json()] + self.logger.info(f"Successfully retrieved commit history. Found {len(commits)} commits.") + return commits + else: + error_message = f"Error getting commit history: {response.status_code}" + self.logger.error(error_message) + return error_message + +# JSON definition for the get_commit_history function +get_commit_history_definition = { + "name": "get_commit_history", + "description": "Get commit history for a file", + "parameters": { + "type": "object", + "properties": { + "file_path": { + "type": "string", + "description": "Path to the file in the repository" + }, + "num_commits": { + "type": "integer", + "description": "Number of commits to retrieve", + "default": 10 + } + }, + "required": ["file_path"] + } +} \ No newline at end of file