kenken999 commited on
Commit
3a55d42
·
1 Parent(s): 6d96254
controllers/gra_03_programfromdoc/programfromdoc.py CHANGED
@@ -97,7 +97,7 @@ fastapiはrouter の作成
97
  """
98
 
99
  def send_to_google_chat(message: str):
100
- webhook_url = 'https://chat.googleapis.com/v1/spaces/your-space-id/messages?key=your-key&token=your-token'
101
  headers = {'Content-Type': 'application/json; charset=UTF-8'}
102
  data = {'text': message}
103
  response = requests.post(webhook_url, headers=headers, json=data)
 
97
  """
98
 
99
  def send_to_google_chat(message: str):
100
+ webhook_url = 'https://chat.googleapis.com/v1/spaces/AAAAF_b7vzQ/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=1HJ5FcTko66L0rh_KNoYp4j__ooq5lKmj5aGI8x6vS4'
101
  headers = {'Content-Type': 'application/json; charset=UTF-8'}
102
  data = {'text': message}
103
  response = requests.post(webhook_url, headers=headers, json=data)
controllers/test_folders CHANGED
@@ -1 +1 @@
1
- Subproject commit 3cc308e5faf6d2e34ac1321461755a49a8bbaa51
 
1
+ Subproject commit e5e0b589a299aea86abc34ce1ba65246c5a592ce
mysite/libs/github copy.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import requests
4
+ import string
5
+ import random
6
+ import shutil
7
+
8
+ def github(token, folder):
9
+ # GitHubユーザー名とトークンを環境変数として定義
10
+ GITHUB_USERNAME = os.getenv("github_user")
11
+ GITHUB_TOKEN = os.getenv("github_token")
12
+
13
+ # ランダムな文字列を生成する関数
14
+ def generate_random_string(length=6):
15
+ letters = string.ascii_lowercase
16
+ return ''.join(random.choice(letters) for i in range(length))
17
+
18
+ # リポジトリ名にランダムな文字列を追加
19
+ REPO_NAME_BASE = "gpt-engeneer"
20
+ REPO_NAME = f"{REPO_NAME_BASE}-{folder}-{generate_random_string()}"
21
+
22
+ # controllersディレクトリのパス
23
+ controllers_dir = "/home/user/app/controllers"
24
+
25
+ # 指定されたフォルダーのパス
26
+ target_dir = os.path.join(controllers_dir, folder)
27
+
28
+ # 指定されたフォルダー内に新しい .git フォルダーを作成
29
+ if os.path.isdir(os.path.join(target_dir, ".git")):
30
+ shutil.rmtree(os.path.join(target_dir, ".git"))
31
+
32
+ # GitHub APIを使ってリモートリポジトリを作成
33
+ response = requests.post(
34
+ "https://api.github.com/user/repos",
35
+ auth=(GITHUB_USERNAME, GITHUB_TOKEN),
36
+ json={"name": REPO_NAME,"public": True}
37
+ )
38
+
39
+ if response.status_code == 201:
40
+ print(f"Successfully created repository {REPO_NAME}")
41
+ else:
42
+ print(f"Failed to create repository: {response.json()}")
43
+ exit(1)
44
+
45
+ # リモートリポジトリのURL (HTTPS形式)
46
+ REPO_URL = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
47
+ REPO_WEB_URL = f"https://github.com/{GITHUB_USERNAME}/{REPO_NAME}" # リポジトリのWeb URL
48
+
49
+ # コマンドを実行するヘルパー関数
50
+ def run_command(command, cwd=None):
51
+ result = subprocess.run(command, shell=True, text=True, capture_output=True, cwd=cwd)
52
+ if result.returncode != 0:
53
+ print(f"Command failed: {command}\n{result.stderr}")
54
+ exit(1)
55
+ else:
56
+ print(result.stdout)
57
+
58
+ # 指定されたフォルダー内でローカルリポジトリを初期化してコミット
59
+ run_command("git init", cwd=target_dir)
60
+ run_command("git add -f .", cwd=target_dir)
61
+ run_command('git commit -m "Initial commit"', cwd=target_dir)
62
+
63
+ # git filter-branchの警告を無視する設定
64
+ os.environ['FILTER_BRANCH_SQUELCH_WARNING'] = '1'
65
+
66
+ # コミット履歴から機密情報を削除(必要に応じて修正)
67
+ run_command("git filter-branch --force --index-filter "
68
+ '"git rm --cached --ignore-unmatch githubs.sh" '
69
+ "--prune-empty --tag-name-filter cat -- --all", cwd=target_dir)
70
+
71
+ # 既存のリモートリポジトリを削除(存在する場合のみ)
72
+ result = subprocess.run("git remote", shell=True, text=True, capture_output=True, cwd=target_dir)
73
+ if "origin" in result.stdout:
74
+ run_command("git remote remove origin", cwd=target_dir)
75
+
76
+ # 新しいリモートリポジトリを追加して強制プッシュ
77
+ run_command(f"git remote add origin {REPO_URL}", cwd=target_dir)
78
+ run_command("git branch -M main", cwd=target_dir)
79
+ run_command("git push -f origin main", cwd=target_dir)
80
+
81
+ print(f"Successfully pushed to GitHub repository {REPO_NAME}")
82
+ print(f"Repository URL: {REPO_WEB_URL}")
83
+ return REPO_WEB_URL
84
+
85
+ # 使用例
86
+ #token = "your_github_token"
87
+ #folder = "test_folders"
88
+ #github(token, folder)
mysite/libs/github.py CHANGED
@@ -1,52 +1,39 @@
1
  import os
2
  import subprocess
3
- import requests
4
  import string
5
  import random
6
- import shutil
7
 
8
- def github(token, folder):
9
- # GitHubユーザー名とトークンを環境変数として定義
10
  GITHUB_USERNAME = os.getenv("github_user")
11
  GITHUB_TOKEN = os.getenv("github_token")
12
 
13
- # ランダムな文字列を生成する関数
14
- def generate_random_string(length=6):
15
- letters = string.ascii_lowercase
16
- return ''.join(random.choice(letters) for i in range(length))
17
-
18
- # リポジトリ名にランダムな文字列を追加
19
- REPO_NAME_BASE = "gpt-engeneer"
20
- REPO_NAME = f"{REPO_NAME_BASE}-{folder}-{generate_random_string()}"
21
 
22
- # controllersディレクトリのパス
 
23
  controllers_dir = "/home/user/app/controllers"
24
-
25
- # 指定されたフォルダーのパス
26
  target_dir = os.path.join(controllers_dir, folder)
27
 
28
- # 指定されたフォルダー内に新しい .git フォルダーを作成
29
- if os.path.isdir(os.path.join(target_dir, ".git")):
30
- shutil.rmtree(os.path.join(target_dir, ".git"))
31
-
32
- # GitHub APIを使ってリモートリポジトリを作成
33
- response = requests.post(
34
- "https://api.github.com/user/repos",
35
- auth=(GITHUB_USERNAME, GITHUB_TOKEN),
36
- json={"name": REPO_NAME,"public": True}
37
- )
38
-
39
- if response.status_code == 201:
40
- print(f"Successfully created repository {REPO_NAME}")
41
- else:
42
- print(f"Failed to create repository: {response.json()}")
43
  exit(1)
44
 
45
- # リモートリポジトリのURL (HTTPS形式)
 
 
 
 
 
 
 
46
  REPO_URL = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
47
- REPO_WEB_URL = f"https://github.com/{GITHUB_USERNAME}/{REPO_NAME}" # リポジトリのWeb URL
48
 
49
- # コマンドを実行するヘルパー関数
50
  def run_command(command, cwd=None):
51
  result = subprocess.run(command, shell=True, text=True, capture_output=True, cwd=cwd)
52
  if result.returncode != 0:
@@ -55,34 +42,40 @@ def github(token, folder):
55
  else:
56
  print(result.stdout)
57
 
58
- # 指定されたフォルダー内でローカルリポジトリを初期化してコミット
59
- run_command("git init", cwd=target_dir)
 
 
 
 
 
 
 
 
 
 
 
60
  run_command("git add -f .", cwd=target_dir)
61
- run_command('git commit -m "Initial commit"', cwd=target_dir)
62
 
63
- # git filter-branchの警告を無視する設定
64
  os.environ['FILTER_BRANCH_SQUELCH_WARNING'] = '1'
65
-
66
- # コミット履歴から機密情報を削除(必要に応じて修正)
67
  run_command("git filter-branch --force --index-filter "
68
  '"git rm --cached --ignore-unmatch githubs.sh" '
69
  "--prune-empty --tag-name-filter cat -- --all", cwd=target_dir)
70
 
71
- # 既存のリモートリポジトリを削除(存在する場合のみ)
72
- result = subprocess.run("git remote", shell=True, text=True, capture_output=True, cwd=target_dir)
73
- if "origin" in result.stdout:
74
- run_command("git remote remove origin", cwd=target_dir)
75
 
76
- # 新しいリモートリ��ジトリを追加して強制プッシュ
77
- run_command(f"git remote add origin {REPO_URL}", cwd=target_dir)
78
- run_command("git branch -M main", cwd=target_dir)
79
- run_command("git push -f origin main", cwd=target_dir)
80
 
81
- print(f"Successfully pushed to GitHub repository {REPO_NAME}")
82
- print(f"Repository URL: {REPO_WEB_URL}")
83
- return REPO_WEB_URL
84
 
85
  # 使用例
86
- #token = "your_github_token"
87
- #folder = "test_folders"
88
- #github(token, folder)
 
1
  import os
2
  import subprocess
 
3
  import string
4
  import random
5
+ import datetime
6
 
7
+ def github_branch(folder):
8
+ # GitHubユーザー名とトークンを環境変数から取得
9
  GITHUB_USERNAME = os.getenv("github_user")
10
  GITHUB_TOKEN = os.getenv("github_token")
11
 
12
+ if not GITHUB_USERNAME or not GITHUB_TOKEN:
13
+ print("❌ github_user または github_token が未設定です")
14
+ exit(1)
 
 
 
 
 
15
 
16
+ # 固定リポジトリ名(既に GitHub 上に存在している必要あり)
17
+ REPO_NAME = "gpt-engeneer"
18
  controllers_dir = "/home/user/app/controllers"
 
 
19
  target_dir = os.path.join(controllers_dir, folder)
20
 
21
+ if not os.path.isdir(target_dir):
22
+ print(f"❌ 指定フォルダが存在しません: {target_dir}")
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  exit(1)
24
 
25
+ # ランダムなブランチ名を作成(例: folder-20250507-ab12f3)
26
+ def generate_random_string(length=6):
27
+ return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
28
+
29
+ date_part = datetime.datetime.now().strftime("%Y%m%d")
30
+ branch_name = f"{folder}-{date_part}-{generate_random_string()}"
31
+
32
+ # GitHubリポジトリURL
33
  REPO_URL = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
34
+ WEB_URL = f"https://github.com/{GITHUB_USERNAME}/{REPO_NAME}/tree/{branch_name}"
35
 
36
+ # コマンド実行関数
37
  def run_command(command, cwd=None):
38
  result = subprocess.run(command, shell=True, text=True, capture_output=True, cwd=cwd)
39
  if result.returncode != 0:
 
42
  else:
43
  print(result.stdout)
44
 
45
+ # .git がなければ初期化
46
+ if not os.path.isdir(os.path.join(target_dir, ".git")):
47
+ run_command("git init", cwd=target_dir)
48
+ run_command(f"git remote add origin {REPO_URL}", cwd=target_dir)
49
+ print("📁 git 初期化と origin 追加")
50
+
51
+ # 現在の変更をクリーンにする
52
+ run_command("git reset", cwd=target_dir)
53
+
54
+ # 新しいブランチを作成して移動
55
+ run_command(f"git checkout -b {branch_name}", cwd=target_dir)
56
+
57
+ # ステージングとコミット
58
  run_command("git add -f .", cwd=target_dir)
59
+ run_command(f'git commit -m "Initial commit on branch {branch_name}"', cwd=target_dir)
60
 
61
+ # 機密ファイル(githubs.shなど)を履歴から削除
62
  os.environ['FILTER_BRANCH_SQUELCH_WARNING'] = '1'
 
 
63
  run_command("git filter-branch --force --index-filter "
64
  '"git rm --cached --ignore-unmatch githubs.sh" '
65
  "--prune-empty --tag-name-filter cat -- --all", cwd=target_dir)
66
 
67
+ # push 先の origin がなければ追加(すでにチェック済みだが念のため)
68
+ remotes = subprocess.run("git remote", shell=True, text=True, capture_output=True, cwd=target_dir)
69
+ if "origin" not in remotes.stdout:
70
+ run_command(f"git remote add origin {REPO_URL}", cwd=target_dir)
71
 
72
+ # ブランチを push(強制ではなく通常pushでOK)
73
+ run_command(f"git push -u origin {branch_name}", cwd=target_dir)
 
 
74
 
75
+ print(f"Successfully pushed to GitHub branch: {branch_name}")
76
+ print(f"🔗 {WEB_URL}")
77
+ return WEB_URL
78
 
79
  # 使用例
80
+ #
81
+ github_branch("test_folders")