Autonomous-Software-Developer / github_manager.py
Falln87's picture
Upload 9 files
f2bc6e2 verified
# FallnAI Autonomous Software Developer
# github_manager.py
import os
from github import Github, InputGitTreeElement
class GitHubManager:
"""A tool to manage GitHub repositories and push code."""
def __init__(self, token):
self.github = Github(token)
def create_and_push_repo(self, repo_name, commit_message, files):
"""
Creates a new public repository and pushes a set of files to it.
:param repo_name: The name for the new repository.
:param commit_message: The message for the initial commit.
:param files: A dictionary of {file_path: file_content}.
:return: The URL of the new repository on success, or an error message.
"""
try:
user = self.github.get_user()
# Check if repo already exists
try:
user.get_repo(repo_name)
return f"Error: Repository '{repo_name}' already exists."
except Exception:
pass # Repo doesn't exist, which is what we want
# Create the repository
repo = user.create_repo(repo_name, private=False)
main_branch = repo.get_branch("main")
# Prepare files for the commit
elements = []
for file_path, content in files.items():
elements.append(InputGitTreeElement(file_path, '100644', 'blob', content))
# Create the initial commit
base_tree = repo.get_git_tree(main_branch.commit.sha)
new_tree = repo.create_git_tree(elements, base_tree)
new_commit = repo.create_git_commit(commit_message, new_tree, [main_branch.commit])
main_branch.set_reference(f"refs/heads/main", new_commit.sha)
return f"Success: Repository created and code pushed. URL: {repo.html_url}"
except Exception as e:
return f"Error pushing to GitHub: {e}"