Files
cyclop/tools/github_tool_functions/create_issue.py
T

79 lines
2.6 KiB
Python

import requests
import logging
class CreateIssue:
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('create_issue.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 - %(levellevel)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, title, body, labels=None):
self.logger.info(f"Creating issue: {title}")
url = f"{self.base_url}/repos/{self.repo}/issues"
data = {
"title": title,
"body": body
}
if labels:
data["labels"] = labels
response = requests.post(url, headers=self.headers, json=data)
if response.status_code == 201:
issue = response.json()
success_message = f"Issue created successfully: {issue['html_url']}"
self.logger.info(success_message)
return success_message
else:
error_message = f"Error creating issue: {response.status_code}\nResponse: {response.text}"
self.logger.error(error_message)
return error_message
# JSON definition for the create_issue function
create_issue_definition = {
"name": "create_issue",
"description": "Create a new issue in the repository",
"parameters": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "Title of the issue"
},
"body": {
"type": "string",
"description": "Body of the issue"
},
"labels": {
"type": "array",
"items": {
"type": "string"
},
"description": "Labels to apply to the issue"
}
},
"required": ["title", "body"]
}
}