SHA256
114 lines
3.7 KiB
Python
114 lines
3.7 KiB
Python
"""stats 命令: Issue 统计"""
|
|
|
|
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
|
|
from tjhlp.config import get_gitea_config
|
|
from tjhlp.utils import compute_issue_stats, parse_repo
|
|
|
|
app = typer.Typer(no_args_is_help=True)
|
|
console = Console()
|
|
|
|
|
|
@app.command("summary")
|
|
def summary(
|
|
repo: str = typer.Argument(help="仓库名称, 格式为 org/repo"),
|
|
) -> None:
|
|
"""显示 Issue 统计摘要"""
|
|
org_name, repo_name = parse_repo(repo)
|
|
cfg = get_gitea_config()
|
|
|
|
with GiteaClient(cfg) as client:
|
|
issues = client.list_issues(org_name, repo_name, state="all")
|
|
|
|
if not issues:
|
|
console.print("暂无 Issue")
|
|
return
|
|
|
|
stats = compute_issue_stats(issues)
|
|
|
|
console.print(f"\n[bold]Issue 统计 - {repo}[/bold]\n")
|
|
|
|
overview = Table(show_header=False)
|
|
overview.add_column("指标", style="bold")
|
|
overview.add_column("数量")
|
|
overview.add_row("总计", str(stats["total"]))
|
|
overview.add_row("开启中", f"[green]{stats['open']}[/green]")
|
|
overview.add_row("已关闭", f"[dim]{stats['closed']}[/dim]")
|
|
overview.add_row("有回复", str(stats["with_comments"]))
|
|
overview.add_row("待回复(开启且无回复)", f"[red]{stats['no_reply']}[/red]")
|
|
console.print(overview)
|
|
|
|
if stats["label_counts"]:
|
|
console.print("\n[bold]按 Label 分布[/bold]")
|
|
label_table = Table()
|
|
label_table.add_column("Label")
|
|
label_table.add_column("数量", justify="right")
|
|
for name, count in stats["label_counts"].most_common():
|
|
label_table.add_row(name, str(count))
|
|
console.print(label_table)
|
|
|
|
if stats["user_counts"]:
|
|
console.print("\n[bold]提问排行 (Top 10)[/bold]")
|
|
user_table = Table()
|
|
user_table.add_column("用户")
|
|
user_table.add_column("提问数", justify="right")
|
|
for name, count in stats["user_counts"].most_common(10):
|
|
user_table.add_row(name, str(count))
|
|
console.print(user_table)
|
|
|
|
|
|
@app.command("export")
|
|
def export_stats(
|
|
repo: str = typer.Argument(help="仓库名称, 格式为 org/repo"),
|
|
output: Path = typer.Option("issues_export.csv", "--output", "-o", help="导出文件路径"),
|
|
) -> None:
|
|
"""导出 Issue 数据到 CSV"""
|
|
org_name, repo_name = parse_repo(repo)
|
|
cfg = get_gitea_config()
|
|
|
|
with GiteaClient(cfg) as client:
|
|
issues = client.list_issues(org_name, repo_name, state="all")
|
|
|
|
if not issues:
|
|
console.print("暂无 Issue")
|
|
return
|
|
|
|
with open(output, "w", newline="", encoding="utf-8") as f:
|
|
writer = csv.DictWriter(
|
|
f,
|
|
fieldnames=[
|
|
"number",
|
|
"title",
|
|
"state",
|
|
"user",
|
|
"labels",
|
|
"comments",
|
|
"created_at",
|
|
"updated_at",
|
|
],
|
|
)
|
|
writer.writeheader()
|
|
for issue in issues:
|
|
writer.writerow(
|
|
{
|
|
"number": issue.get("number", ""),
|
|
"title": issue.get("title", ""),
|
|
"state": issue.get("state", ""),
|
|
"user": issue.get("user", {}).get("login", ""),
|
|
"labels": "; ".join(lbl.get("name", "") for lbl in issue.get("labels", [])),
|
|
"comments": issue.get("comments", 0),
|
|
"created_at": issue.get("created_at", ""),
|
|
"updated_at": issue.get("updated_at", ""),
|
|
}
|
|
)
|
|
|
|
console.print(f"[green]已导出 {len(issues)} 条 Issue 到 {output}[/green]")
|