From 648949e49212418a018db344d5193e42d37ae07e Mon Sep 17 00:00:00 2001 From: bucolucas Date: Mon, 19 Aug 2024 15:47:39 -0500 Subject: [PATCH] Add GetBranchSHA class for get_branch_sha function with JSON definition --- tools/github_tool_functions/get_branch_sha.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tools/github_tool_functions/get_branch_sha.py diff --git a/tools/github_tool_functions/get_branch_sha.py b/tools/github_tool_functions/get_branch_sha.py new file mode 100644 index 0000000..f981d11 --- /dev/null +++ b/tools/github_tool_functions/get_branch_sha.py @@ -0,0 +1,56 @@ +import requests +import logging + +class GetBranchSHA: + 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_branch_sha.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, branch): + url = f"{self.base_url}/repos/{self.repo}/git/refs/heads/{branch}" + response = requests.get(url, headers=self.headers) + if response.status_code == 200: + return response.json()["object"]["sha"] + else: + return f"Error getting branch SHA: {response.status_code}" + +# JSON definition for the get_branch_sha function +get_branch_sha_definition = { + "name": "get_branch_sha", + "description": "Get the SHA of the latest commit on a branch", + "parameters": { + "type": "object", + "properties": { + "branch": { + "type": "string", + "description": "Name of the branch" + } + }, + "required": ["branch"] + } +} \ No newline at end of file