"""repos 命令: 仓库管理""" from __future__ import annotations from pathlib import Path import typer from rich.console import Console from tjhlp.client import GiteaClient, GiteaClientError from tjhlp.config import get_gitea_config from tjhlp.utils import parse_repo, push_templates, sync_labels app = typer.Typer(no_args_is_help=True) console = Console() _PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent.parent.parent _TEMPLATES_DIR = _PROJECT_ROOT / "issue-templates" @app.command("create") def create_repo( repo: str = typer.Argument(help="仓库名称, 格式为 org/repo"), description: str = typer.Option("", "--desc", "-d", help="仓库描述"), private: bool = typer.Option(False, "--private", help="是否设为私有"), ) -> None: """在组织下创建仓库""" org_name, repo_name = parse_repo(repo) cfg = get_gitea_config() with GiteaClient(cfg) as client: existing = client.get_repo(org_name, repo_name) if existing: console.print(f"[yellow]仓库 {repo} 已存在[/yellow]") return result = client.create_org_repo( org_name, repo_name, description=description, private=private, auto_init=True, ) console.print(f"[green]仓库已创建: {result.html_url}[/green]") @app.command("create-homework") def create_homework( org: str = typer.Option(..., "--org", help="组织名称"), number: int = typer.Option(..., "--number", "-n", help="作业编号(如 3 对应 hw03)"), description: str = typer.Option("", "--desc", "-d", help="作业描述"), webhook_url: str = typer.Option("", "--webhook-url", help="Webhook 通知地址"), webhook_secret: str = typer.Option("", "--webhook-secret", help="Webhook 密钥"), ) -> None: """创建作业仓库(自动命名 hwXX, 推送作业模板, 同步 Labels, 配置 Webhook)""" repo_name = f"hw{number:02d}" desc = description or f"作业 {number:02d}" cfg = get_gitea_config() with GiteaClient(cfg) as client: # 创建仓库 existing = client.get_repo(org, repo_name) if existing: console.print(f"[yellow]仓库 {org}/{repo_name} 已存在[/yellow]") else: client.create_org_repo(org, repo_name, description=desc, auto_init=True) console.print(f"[green]仓库 {org}/{repo_name} 已创建[/green]") # 推送作业 Issue Templates console.print("\n推送 Issue Templates...") hw_tpl_dir = _TEMPLATES_DIR / "homework" n = push_templates(client, org, repo_name, hw_tpl_dir) console.print(f" 共推送 {n} 个模板文件") # 同步 Labels console.print("\n同步 Labels...") created = sync_labels(client, org, repo_name, cfg) console.print(f" 新增 {created} 个 Label") # 配置 Webhook if webhook_url: console.print("\n配置 Webhook...") client.create_webhook( org, repo_name, webhook_url, secret=webhook_secret, events=["issues", "issue_comment"], ) console.print(f" [green]Webhook -> {webhook_url}[/green]") # 添加到团队 console.print("\n添加到团队...") for team_name in ("ta", "students"): team = client.get_team_by_name(org, team_name) if team: try: client.add_team_repo(team.id, org, repo_name) console.print(f" [green]{team_name} -> {repo_name}[/green]") except GiteaClientError as e: console.print(f" [yellow]{team_name}: {e.message}[/yellow]") console.print(f"\n[bold green]作业仓库 {org}/{repo_name} 配置完成[/bold green]") @app.command("init-templates") def init_templates( repo: str = typer.Argument(help="仓库名称, 格式为 org/repo"), template_type: str = typer.Option( "general", "--type", "-t", help="模板类型: general 或 homework" ), ) -> None: """将 Issue Templates 推送到指定仓库""" org_name, repo_name = parse_repo(repo) tpl_dir = _TEMPLATES_DIR / template_type if not tpl_dir.exists(): console.print(f"[red]模板类型 {template_type} 不存在(路径: {tpl_dir})[/red]") raise typer.Exit(code=1) cfg = get_gitea_config() with GiteaClient(cfg) as client: n = push_templates(client, org_name, repo_name, tpl_dir) console.print(f"\n共推送 {n} 个模板文件到 {repo}") @app.command("configure") def configure_repo( repo: str = typer.Argument(help="仓库名称, 格式为 org/repo"), disable_wiki: bool = typer.Option(True, help="禁用 Wiki"), disable_projects: bool = typer.Option(True, help="禁用 Projects"), disable_packages: bool = typer.Option(True, help="禁用 Packages"), ) -> None: """配置仓库设置(禁用不需要的功能)""" org_name, repo_name = parse_repo(repo) cfg = get_gitea_config() with GiteaClient(cfg) as client: client.update_repo( org_name, repo_name, has_wiki=not disable_wiki, has_projects=not disable_projects, has_packages=not disable_packages, ) console.print(f"[green]仓库 {repo} 配置已更新[/green]")