Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,601 Bytes
de71b62 857f2ee de71b62 5c58643 de71b62 c4b2da2 de71b62 0dbb859 de71b62 857f2ee de71b62 857f2ee c4b2da2 857f2ee de71b62 bf173b2 de71b62 857f2ee de71b62 18553a9 de71b62 857f2ee de71b62 857f2ee de71b62 857f2ee de71b62 857f2ee de71b62 857f2ee de71b62 857f2ee ecbdfe4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import os
import subprocess
import requests
import string
import random
import shutil
def github(token, folder):
# GitHubユーザー名とトークンを環境変数として定義
GITHUB_USERNAME = os.getenv("github_user")
GITHUB_TOKEN = token
GITHUB_TOKEN = os.getenv("github_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}-{folder}-{generate_random_string(4)}"
# controllersディレクトリのパス
controllers_dir = "/home/user/app/controllers"
# 指定されたフォルダーのパス
target_dir = os.path.join(controllers_dir, folder)
print(target_dir)
# 指定されたフォルダー内に新しい .git フォルダーを作成
if os.path.isdir(os.path.join(target_dir, ".git")):
shutil.rmtree(os.path.join(target_dir, ".git"))
# GitHub APIを使ってリモートリポジトリを作成
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)
# リモートリポジトリのURL (HTTPS形式)
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}" # リポジトリのWeb URL
# コマンドを実行するヘルパー関数
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)
# git filter-branchの警告を無視する設定
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
# 使用例
#token = "your_github_token"
#folder = "your_target_folder"
#github(token, folder)
#github("","test_foldersdd") |