"""orgs 命令: 组织与团队管理""" from __future__ import annotations import csv from pathlib import Path import typer from rich.console import Console from rich.table import Table from tjhlp.client import GiteaClient, GiteaClientError from tjhlp.config import get_gitea_config from tjhlp.models import MemberRecord app = typer.Typer(no_args_is_help=True) console = Console() @app.command("create") def create_org( name: str = typer.Argument(help="组织名称(如 tjhlp-2025f)"), full_name: str = typer.Option("", "--full-name", help="组织全名"), description: str = typer.Option("课程答疑平台", "--desc", "-d", help="组织描述"), visibility: str = typer.Option("public", "--visibility", help="可见性: public/limited/private"), ) -> None: """创建课程组织""" cfg = get_gitea_config() with GiteaClient(cfg) as client: existing = client.get_org(name) if existing: console.print(f"[yellow]组织 {name} 已存在[/yellow]") return org = client.create_org( name, full_name=full_name, description=description, visibility=visibility, ) console.print(f"[green]组织 {org.name} 已创建[/green]") @app.command("create-team") def create_team( org: str = typer.Argument(help="组织名称"), team_name: str = typer.Argument(help="团队名称"), permission: str = typer.Option("read", "--permission", "-p", help="权限: read/write/admin"), ) -> None: """在组织下创建团队""" cfg = get_gitea_config() with GiteaClient(cfg) as client: existing = client.get_team_by_name(org, team_name) if existing: console.print(f"[yellow]团队 {team_name} 已存在[/yellow]") return team = client.create_team(org, team_name, permission=permission) console.print(f"[green]团队 {team.name} 已创建(权限: {permission})[/green]") @app.command("add-members") def add_members( org: str = typer.Option(..., "--org", help="组织名称"), file: Path = typer.Option(..., "--file", "-f", help="成员名单 CSV 文件路径"), ) -> None: """根据 CSV 批量添加成员到组织对应团队(student -> students, ta -> ta)""" if not file.exists(): console.print(f"[red]文件不存在: {file}[/red]") raise typer.Exit(code=1) cfg = get_gitea_config() records: list[MemberRecord] = [] with open(file, encoding="utf-8-sig") as f: reader = csv.DictReader(f) for row in reader: records.append(MemberRecord.model_validate(row)) with GiteaClient(cfg) as client: ta_team = client.get_team_by_name(org, "ta") students_team = client.get_team_by_name(org, "students") if not ta_team or not students_team: console.print("[red]未找到 ta 或 students 团队, 请先执行 setup init[/red]") raise typer.Exit(code=1) stats = {"added": 0, "failed": 0} for member in records: team = ta_team if member.role == "ta" else students_team try: client.add_team_member(team.id, member.username) console.print(f" [green]添加[/green] {member.username} -> {team.name}") stats["added"] += 1 except GiteaClientError as e: console.print(f" [red]失败[/red] {member.username}: {e.message}") stats["failed"] += 1 console.print(f"\n完成: 添加 {stats['added']}, 失败 {stats['failed']}") @app.command("list-teams") def list_teams( org: str = typer.Argument(help="组织名称"), ) -> None: """列出组织下的所有团队""" cfg = get_gitea_config() with GiteaClient(cfg) as client: teams = client.list_teams(org) table = Table(title=f"组织 {org} 的团队") table.add_column("ID", style="dim") table.add_column("名称") table.add_column("权限") table.add_column("单元") for t in teams: table.add_row(str(t.id), t.name, t.permission, ", ".join(t.units)) console.print(table)