56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
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"]
|
|
}
|
|
} |