-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map git possibilities #3
Comments
Steps to investigate
|
First Attemptimport os
import subprocess
# Allowed git commands:
commands = ['init','clone','checkout','pull','commit','push'] # create, review and merge PRs
# TODO: Replace with pygithub
class GitHubRepo:
def __init__(self, name, title=None, remove_existing=False):
self.name = name
self.user = 'cruxsj'
self.url = f'https://github.com/{self.user}/{name}.git'
self.path = os.path.join(os.getcwd(), name)
self.title = title
self.branch = 'main'
self.create(remove_existing)
self.cd()
print(f'Current working directory: {os.getcwd()}')
def __str__(self):
return f'GitHubRepo(name={self.name!r}, title={self.title!r}, path={self.path!r}, branch={self.branch!r})'
def __repr__(self):
return str(self)
def _run_system_command(self, command):
print(f'Running command: {command!r}')
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Checking the status
if result.returncode == 0:
print("Command executed successfully!")
if result.stdout:
print(f"Output: {result.stdout}")
else:
raise Exception(f'Command failed with return code: {result.returncode!r}: {result.stderr!r}')
def create(self, remove_existing):
if remove_existing and os.path.exists(self.path):
self._run_system_command(f'rm -rf {self.path}')
if not os.path.exists(self.name):
os.mkdir(self.path)
self.cd()
self._run_system_command('git init')
try:
self._run_system_command(f'gh repo create -s demo_gpt_repo -d "On a mission to mine" --public')
except:
print('Repo already exists')
try:
self._run_system_command(f'git remote remove origin')
except:
print('No remote to remove')
self._run_system_command(f'git remote add origin {self.url}')
else:
print(f'Repo {self.name!r} already exists')
return self
def cd(self):
if os.getcwd() != self.path:
print('Changing dir')
os.chdir(self.path)
else:
print('Already in the right directory')
print(f'Current working directory: {os.getcwd()}, contents: {os.listdir()}')
def checkout(self, branch=None):
if branch is None:
branch = 'main'
self._run_system_command(f'git checkout -b {branch}')
self.branch = branch
def commit(self, files, message, branch=None):
if branch is not None:
self.checkout(branch)
for filename, content in files.items():
path = os.path.join(self.path, filename)
with open(path, 'w') as f:
print(f'Writing to {path}')
f.write(content)
self._run_system_command(f'git add {path}')
self._run_system_command(f'git commit -m "{message}"')
return self
def push(self, branch=None):
if branch is None:
branch = self.branch
self._run_system_command(f'git push -u origin {branch}')
return self
def pull(self, branch=None):
if branch is None:
branch = self.branch
self._run_system_command(f'git pull origin {branch}')
return self
def create_pr(self, branch_name, title, body):
pass
def merge_pr(self, pr_id):
pass
repo = GitHubRepo(name='demo_gpt_repo', title='Demo GPT Repo', remove_existing=True)
# Imitate files coming from miner
files = {
'README.md': f'# Demo GPT Repo \n\nThis is a demo repo for GPT. ',
'main.py': f'print("Hello world")',
}
repo.commit(files, message='First commit').push() |
Pre-existing Github+Python Solutionshttps://chat.openai.com/share/91e6e4da-68ab-4594-ba89-d7de08b9f060 The best Python API for interacting with GitHub depends on your specific needs and preferences. However, a few popular options are widely used due to their robustness, ease of use, and comprehensive feature sets:
The "best" choice depends on your specific requirements:
It's also a good idea to check the latest state of these libraries, as the open-source landscape can change rapidly. |
No description provided.
The text was updated successfully, but these errors were encountered: