-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_gh_secrets_tui.py
More file actions
454 lines (368 loc) · 17.2 KB
/
copy_gh_secrets_tui.py
File metadata and controls
454 lines (368 loc) · 17.2 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python3
"""
GitHub CLI 密钥和环境变量复制工具 (TUI版本)
功能:
- 从其他GitHub项目复制secrets和variables
- 支持项目搜索和选择
- 交互式TUI界面,支持上下键选择
- 复制前确认和修改
- 批量操作支持
使用前提:
- 安装GitHub CLI (gh)
- 已登录GitHub账户:gh auth login
"""
import subprocess
import json
import sys
import re
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
import inquirer
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.text import Text
from rich import print as rprint
console = Console()
@dataclass
class Repository:
"""仓库信息"""
name: str
full_name: str
description: str
private: bool
@dataclass
class Secret:
"""密钥信息"""
name: str
updated_at: str
@dataclass
class Variable:
"""环境变量信息"""
name: str
value: str
updated_at: str
class GitHubSecretsManager:
"""GitHub密钥和环境变量管理器"""
def __init__(self):
self.check_gh_cli()
def check_gh_cli(self):
"""检查GitHub CLI是否安装和登录"""
try:
# 检查gh cli是否安装
result = subprocess.run(['gh', '--version'], capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
version = result.stdout.split('\n')[0]
console.print(f"✓ GitHub CLI 已安装: {version.split()[-1]}", style="green")
# 检查是否已登录
result = subprocess.run(['gh', 'auth', 'status'], capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
console.print("✓ GitHub CLI 已登录", style="green")
except subprocess.CalledProcessError as e:
if 'gh auth status' in str(e.cmd):
console.print("❌ 请先登录GitHub CLI: gh auth login", style="red")
sys.exit(1)
else:
console.print("❌ 请先安装GitHub CLI: https://cli.github.com", style="red")
sys.exit(1)
except FileNotFoundError:
console.print("❌ 请先安装GitHub CLI: https://cli.github.com", style="red")
sys.exit(1)
def search_repositories(self, query: str = "", limit: int = 20) -> List[Repository]:
"""搜索用户自己的仓库"""
try:
# 首先获取当前用户信息
user_result = subprocess.run(['gh', 'api', 'user'], capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
user_data = json.loads(user_result.stdout)
username = user_data['login']
# 列出用户自己的仓库
cmd = ['gh', 'repo', 'list', username, '--limit', str(limit), '--json',
'name,nameWithOwner,description,isPrivate']
result = subprocess.run(cmd, capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
repos_data = json.loads(result.stdout)
repositories = []
for repo in repos_data:
# 如果有查询条件,进行过滤
if query:
if (query.lower() not in repo['name'].lower() and
query.lower() not in repo.get('description', '').lower()):
continue
repositories.append(Repository(
name=repo['name'],
full_name=repo['nameWithOwner'],
description=repo.get('description', ''),
private=repo['isPrivate']
))
return repositories
except subprocess.CalledProcessError as e:
console.print(f"❌ 搜索仓库失败: {e.stderr}", style="red")
return []
except json.JSONDecodeError:
console.print("❌ 解析仓库数据失败", style="red")
return []
def get_secrets(self, repo_full_name: str) -> List[Secret]:
"""获取仓库的secrets"""
try:
cmd = ['gh', 'secret', 'list', '--repo', repo_full_name, '--json',
'name,updatedAt']
result = subprocess.run(cmd, capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
secrets_data = json.loads(result.stdout)
secrets = []
for secret in secrets_data:
secrets.append(Secret(
name=secret['name'],
updated_at=secret['updatedAt']
))
return secrets
except subprocess.CalledProcessError as e:
console.print(f"❌ 获取secrets失败: {e.stderr}", style="red")
return []
except json.JSONDecodeError:
console.print("❌ 解析secrets数据失败", style="red")
return []
def get_variables(self, repo_full_name: str) -> List[Variable]:
"""获取仓库的variables"""
try:
cmd = ['gh', 'variable', 'list', '--repo', repo_full_name, '--json',
'name,value,updatedAt']
result = subprocess.run(cmd, capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
variables_data = json.loads(result.stdout)
variables = []
for var in variables_data:
variables.append(Variable(
name=var['name'],
value=var['value'],
updated_at=var['updatedAt']
))
return variables
except subprocess.CalledProcessError as e:
console.print(f"❌ 获取variables失败: {e.stderr}", style="red")
return []
except json.JSONDecodeError:
console.print("❌ 解析variables数据失败", style="red")
return []
def select_repository_tui(repositories: List[Repository]) -> Optional[Repository]:
"""使用TUI选择仓库"""
if not repositories:
return None
choices = []
for repo in repositories:
visibility = "🔒 私有" if repo.private else "🌐 公开"
desc = repo.description[:50] + "..." if len(repo.description) > 50 else repo.description
choice_text = f"{repo.name} ({visibility}) - {desc}"
choices.append((choice_text, repo))
questions = [
inquirer.List('repo',
message="请选择源仓库",
choices=choices,
carousel=True)
]
try:
answer = inquirer.prompt(questions)
if answer:
return answer['repo']
except KeyboardInterrupt:
return None
return None
def copy_secrets_tui(secrets: List[Secret], target_repo: str, manager: GitHubSecretsManager):
"""使用TUI交互式复制secrets"""
if not secrets:
console.print("📝 没有secrets需要复制", style="yellow")
return
console.print(f"\n🔐 开始处理 {len(secrets)} 个Secrets", style="bold blue")
for i, secret in enumerate(secrets, 1):
console.print(f"\n[{i}/{len(secrets)}] 处理 Secret: [bold]{secret.name}[/bold]")
console.print(f" 更新时间: {secret.updated_at}")
# 使用inquirer选择操作
questions = [
inquirer.List('action',
message=f"选择对 {secret.name} 的操作",
choices=[
('直接复制', 'copy'),
('修改名称后复制', 'rename'),
('跳过此项', 'skip')
],
default='copy', # 默认选择直接复制
carousel=True)
]
try:
answer = inquirer.prompt(questions)
if not answer or answer['action'] == 'skip':
console.print(f"⏭️ 跳过 {secret.name}", style="yellow")
continue
secret_name = secret.name
if answer['action'] == 'rename':
new_name_question = [
inquirer.Text('new_name',
message=f"输入新名称 (当前: {secret.name})",
default=secret.name)
]
name_answer = inquirer.prompt(new_name_question)
if name_answer and name_answer['new_name'].strip():
secret_name = name_answer['new_name'].strip()
# 获取secret值
value_question = [
inquirer.Password('value',
message=f"请输入 {secret_name} 的值")
]
value_answer = inquirer.prompt(value_question)
if not value_answer or not value_answer['value'].strip():
console.print(f"❌ 跳过 {secret_name} (未提供值)", style="red")
continue
secret_value = value_answer['value'].strip()
# 设置secret
try:
cmd = ['gh', 'secret', 'set', secret_name, '--repo', target_repo, '--body', secret_value]
result = subprocess.run(cmd, capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
console.print(f"✅ 成功复制 secret: {secret_name}", style="green")
except subprocess.CalledProcessError as e:
console.print(f"❌ 复制 secret {secret_name} 失败: {e.stderr}", style="red")
except KeyboardInterrupt:
console.print("\n👋 用户中断操作", style="yellow")
break
def copy_variables_tui(variables: List[Variable], target_repo: str, manager: GitHubSecretsManager):
"""使用TUI交互式复制variables"""
if not variables:
console.print("📝 没有variables需要复制", style="yellow")
return
console.print(f"\n🔧 开始处理 {len(variables)} 个Variables", style="bold blue")
for i, variable in enumerate(variables, 1):
console.print(f"\n[{i}/{len(variables)}] 处理 Variable: [bold]{variable.name}[/bold]")
console.print(f" 当前值: {variable.value}")
console.print(f" 更新时间: {variable.updated_at}")
# 使用inquirer选择操作
questions = [
inquirer.List('action',
message=f"选择对 {variable.name} 的操作",
choices=[
('直接复制', 'copy'),
('修改名称/值后复制', 'modify'),
('跳过此项', 'skip')
],
default='copy', # 默认选择直接复制
carousel=True)
]
try:
answer = inquirer.prompt(questions)
if not answer or answer['action'] == 'skip':
console.print(f"⏭️ 跳过 {variable.name}", style="yellow")
continue
variable_name = variable.name
variable_value = variable.value
if answer['action'] == 'modify':
# 修改名称
name_question = [
inquirer.Text('new_name',
message=f"输入新名称 (当前: {variable.name}, 回车保持不变)",
default=variable.name)
]
name_answer = inquirer.prompt(name_question)
if name_answer and name_answer['new_name'].strip():
variable_name = name_answer['new_name'].strip()
# 修改值
value_question = [
inquirer.Text('new_value',
message=f"输入新值 (当前: {variable.value}, 回车保持不变)",
default=variable.value)
]
value_answer = inquirer.prompt(value_question)
if value_answer and value_answer['new_value'].strip():
variable_value = value_answer['new_value'].strip()
# 设置variable
try:
cmd = ['gh', 'variable', 'set', variable_name, '--repo', target_repo, '--body', variable_value]
result = subprocess.run(cmd, capture_output=True, text=True, check=True,
encoding='utf-8', errors='ignore')
console.print(f"✅ 成功复制 variable: {variable_name} = {variable_value}", style="green")
except subprocess.CalledProcessError as e:
console.print(f"❌ 复制 variable {variable_name} 失败: {e.stderr}", style="red")
except KeyboardInterrupt:
console.print("\n👋 用户中断操作", style="yellow")
break
def display_secrets_and_variables_rich(secrets: List[Secret], variables: List[Variable]):
"""使用Rich显示secrets和variables"""
# 显示Secrets
if secrets:
secrets_table = Table(title="🔐 Secrets", show_header=True, header_style="bold magenta")
secrets_table.add_column("序号", style="dim", width=6)
secrets_table.add_column("名称", style="cyan")
secrets_table.add_column("更新时间", style="green")
for i, secret in enumerate(secrets, 1):
secrets_table.add_row(str(i), secret.name, secret.updated_at)
console.print(secrets_table)
else:
console.print(Panel("无secrets", title="🔐 Secrets", border_style="dim"))
# 显示Variables
if variables:
variables_table = Table(title="🔧 Variables", show_header=True, header_style="bold magenta")
variables_table.add_column("序号", style="dim", width=6)
variables_table.add_column("名称", style="cyan")
variables_table.add_column("值", style="yellow")
variables_table.add_column("更新时间", style="green")
for i, var in enumerate(variables, 1):
variables_table.add_row(str(i), var.name, var.value, var.updated_at)
console.print(variables_table)
else:
console.print(Panel("无variables", title="🔧 Variables", border_style="dim"))
def main():
"""主函数"""
console.print(Panel.fit("🚀 GitHub 密钥和环境变量复制工具 (TUI版)", style="bold blue"))
manager = GitHubSecretsManager()
# 搜索源仓库
while True:
search_query = console.input("\n🔍 搜索您的仓库 (回车显示所有仓库): ").strip()
with console.status("[bold green]搜索仓库中..."):
repositories = manager.search_repositories(search_query, limit=30)
if not repositories:
console.print("❌ 没有找到仓库,请重新搜索", style="red")
continue
source_repo = select_repository_tui(repositories)
if source_repo is None:
console.print("👋 已退出", style="yellow")
return
console.print(f"\n✓ 选择源仓库: [bold cyan]{source_repo.full_name}[/bold cyan]")
break
# 获取secrets和variables
with console.status("[bold green]获取仓库配置..."):
secrets = manager.get_secrets(source_repo.full_name)
variables = manager.get_variables(source_repo.full_name)
display_secrets_and_variables_rich(secrets, variables)
if not secrets and not variables:
console.print("❌ 该仓库没有secrets或variables可复制", style="red")
return
# 输入目标仓库
target_repo_question = [
inquirer.Text('target_repo',
message="🎯 输入目标仓库 (格式: owner/repo)",
validate=lambda _, x: re.match(r'^[\w\-\.]+/[\w\-\.]+$', x) is not None)
]
try:
target_answer = inquirer.prompt(target_repo_question)
if not target_answer:
console.print("👋 已退出", style="yellow")
return
target_repo = target_answer['target_repo']
console.print(f"\n🔄 开始交互式复制到 [bold cyan]{target_repo}[/bold cyan]")
console.print("=" * 60)
# 交互式复制secrets
if secrets:
copy_secrets_tui(secrets, target_repo, manager)
# 交互式复制variables
if variables:
copy_variables_tui(variables, target_repo, manager)
console.print(f"\n✅ 交互式复制完成!", style="bold green")
except KeyboardInterrupt:
console.print("\n👋 用户中断,已退出", style="yellow")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
console.print("\n\n👋 用户中断,已退出", style="yellow")
except Exception as e:
console.print(f"❌ 发生错误: {e}", style="red")
raise