FallnAI/Autonomous-Software-Developer
0
1# FallnAI Autonomous Software Developer2# github_manager.py 3 4import os5from github import Github, InputGitTreeElement6 7class GitHubManager:8 """A tool to manage GitHub repositories and push code."""9 10 def __init__(self, token):11 self.github = Github(token)12 13 def create_and_push_repo(self, repo_name, commit_message, files):14 """15 Creates a new public repository and pushes a set of files to it.16 17 :param repo_name: The name for the new repository.18 :param commit_message: The message for the initial commit.19 :param files: A dictionary of {file_path: file_content}.20 :return: The URL of the new repository on success, or an error message.21 """22 try:23 user = self.github.get_user()24 25 # Check if repo already exists26 try:27 user.get_repo(repo_name)28 return f"Error: Repository '{repo_name}' already exists."29 except Exception:30 pass # Repo doesn't exist, which is what we want31 32 # Create the repository33 repo = user.create_repo(repo_name, private=False)34 main_branch = repo.get_branch("main")35 36 # Prepare files for the commit37 elements = []38 for file_path, content in files.items():39 elements.append(InputGitTreeElement(file_path, '100644', 'blob', content))40 41 # Create the initial commit42 base_tree = repo.get_git_tree(main_branch.commit.sha)43 new_tree = repo.create_git_tree(elements, base_tree)44 new_commit = repo.create_git_commit(commit_message, new_tree, [main_branch.commit])45 main_branch.set_reference(f"refs/heads/main", new_commit.sha)46 47 return f"Success: Repository created and code pushed. URL: {repo.html_url}"48 49 except Exception as e:50 return f"Error pushing to GitHub: {e}"51 