|
import os |
|
import subprocess |
|
import requests |
|
import string |
|
import random |
|
import shutil |
|
|
|
def github(token, folder): |
|
|
|
GITHUB_USERNAME = os.getenv("postgre_user") |
|
GITHUB_TOKEN = token |
|
|
|
|
|
def generate_random_string(length=6): |
|
letters = string.ascii_lowercase |
|
return ''.join(random.choice(letters) for i in range(length)) |
|
|
|
|
|
REPO_NAME_BASE = "gpt-engeneer" |
|
REPO_NAME = f"{REPO_NAME_BASE}-{generate_random_string()}" |
|
|
|
|
|
controllers_dir = "/home/user/app/controllers" |
|
|
|
|
|
target_dir = os.path.join(controllers_dir, folder) |
|
|
|
|
|
if os.path.isdir(os.path.join(target_dir, ".git")): |
|
shutil.rmtree(os.path.join(target_dir, ".git")) |
|
|
|
|
|
response = requests.post( |
|
"https://api.github.com/user/repos", |
|
auth=(GITHUB_USERNAME, GITHUB_TOKEN), |
|
json={"name": REPO_NAME,"private": True} |
|
) |
|
|
|
if response.status_code == 201: |
|
print(f"Successfully created repository {REPO_NAME}") |
|
else: |
|
print(f"Failed to create repository: {response.json()}") |
|
exit(1) |
|
|
|
|
|
REPO_URL = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git" |
|
REPO_WEB_URL = f"https://github.com/{GITHUB_USERNAME}/{REPO_NAME}" |
|
|
|
|
|
def run_command(command, cwd=None): |
|
result = subprocess.run(command, shell=True, text=True, capture_output=True, cwd=cwd) |
|
if result.returncode != 0: |
|
print(f"Command failed: {command}\n{result.stderr}") |
|
exit(1) |
|
else: |
|
print(result.stdout) |
|
|
|
|
|
run_command("git init", cwd=target_dir) |
|
run_command("git add -f .", cwd=target_dir) |
|
run_command('git commit -m "Initial commit"', cwd=target_dir) |
|
|
|
|
|
os.environ['FILTER_BRANCH_SQUELCH_WARNING'] = '1' |
|
|
|
|
|
run_command("git filter-branch --force --index-filter " |
|
'"git rm --cached --ignore-unmatch githubs.sh" ' |
|
"--prune-empty --tag-name-filter cat -- --all", cwd=target_dir) |
|
|
|
|
|
result = subprocess.run("git remote", shell=True, text=True, capture_output=True, cwd=target_dir) |
|
if "origin" in result.stdout: |
|
run_command("git remote remove origin", cwd=target_dir) |
|
|
|
|
|
run_command(f"git remote add origin {REPO_URL}", cwd=target_dir) |
|
run_command("git branch -M main", cwd=target_dir) |
|
run_command("git push -f origin main", cwd=target_dir) |
|
|
|
print(f"Successfully pushed to GitHub repository {REPO_NAME}") |
|
print(f"Repository URL: {REPO_WEB_URL}") |
|
return REPO_WEB_URL |
|
|
|
|
|
|
|
|
|
|
|
|