Add GetCommitHistory class for get_commit_history function with JSON definition

This commit is contained in:
2024-08-19 15:46:42 -05:00
parent 0e7b983473
commit bcea167299
@@ -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"]
}
}