File size: 3,141 Bytes
de71b62
 
 
 
 
 
 
321ea21
de71b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a7cbac
 
de71b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321ea21
de71b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import requests
import string
import random
import shutil

def github(token:any,folder:any):
    # GitHubユーザー名とトークンを環境変数として定義
    GITHUB_USERNAME = "miyataken999"
    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()}"

    # .gitignore ファイルに github.sh と github.py を追加
    with open(".gitignore", "a") as gitignore_file:
        gitignore_file.write("githubs.sh\n")
        gitignore_file.write("githubs.py\n")

    # 既存の .git フォルダーを削除
    if os.path.isdir(".git"):
        shutil.rmtree(".git")

    # GitHub APIを使ってリモートリポジトリを作成
    response = requests.post(
        "https://api.github.com/user/repos",
        auth=(GITHUB_USERNAME, GITHUB_TOKEN),
        json={"name": REPO_NAME}
    )

    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):
        result = subprocess.run(command, shell=True, text=True, capture_output=True)
        if result.returncode != 0:
            print(f"Command failed: {command}\n{result.stderr}")
            exit(1)
        else:
            print(result.stdout)

    # ローカルリポジトリを初期化してコミット
    run_command("git init")
    run_command(f"git add -f /home/user/app/controllers/{folder}/*")
    run_command('git commit -m "Initial commit"')

    # git filter-branchの警告を無視する設定
    os.environ['FILTER_BRANCH_SQUELCH_WARNING'] = '1'

    # コミット履歴から機密情報を削除(必要に応じて修正)
    run_command("git filter-branch --force --index-filter "
                '"git rm --cached --ignore-unmatch github.sh" '
                "--prune-empty --tag-name-filter cat -- --all")

    # 既存のリモートリポジトリを削除(存在する場合のみ)
    result = subprocess.run("git remote", shell=True, text=True, capture_output=True)
    if "origin" in result.stdout:
        run_command("git remote remove origin")

    # 新しいリモートリポジトリを追加して強制プッシュ
    run_command(f"git remote add origin {REPO_URL}")
    run_command("git branch -M main")
    run_command("git push -f origin main")

    print(f"Successfully pushed to GitHub repository {REPO_NAME}")
    print(f"Repository URL: {REPO_WEB_URL}")
    return REPO_WEB_URL