SHA256
143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
"""共享工具函数: 仓库名解析、Issue Template 推送、Label 同步、统计计算"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import typer
|
|
from rich.console import Console
|
|
|
|
from tjhlp.client import GiteaClient, GiteaClientError
|
|
from tjhlp.config import GiteaConfig
|
|
|
|
console = Console()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 仓库名解析
|
|
# ---------------------------------------------------------------------------
|
|
def parse_repo(repo: str) -> tuple[str, str]:
|
|
"""将 'org/repo' 格式的字符串解析为 (org, repo) 元组。
|
|
|
|
格式不正确时打印错误并退出。
|
|
"""
|
|
parts = repo.split("/")
|
|
if len(parts) != 2 or not parts[0] or not parts[1]:
|
|
console.print("[red]仓库名称格式应为 org/repo[/red]")
|
|
raise typer.Exit(code=1)
|
|
return parts[0], parts[1]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Issue Template 推送
|
|
# ---------------------------------------------------------------------------
|
|
def push_templates(
|
|
client: GiteaClient,
|
|
org: str,
|
|
repo: str,
|
|
template_dir: Path,
|
|
) -> int:
|
|
"""将本地 issue template 目录推送到远端仓库, 返回推送的文件数。"""
|
|
count = 0
|
|
gitea_dir = template_dir / ".gitea" / "ISSUE_TEMPLATE"
|
|
if not gitea_dir.exists():
|
|
console.print(f"[yellow]模板目录不存在: {gitea_dir}[/yellow]")
|
|
return 0
|
|
|
|
for filepath in sorted(gitea_dir.iterdir()):
|
|
if not filepath.is_file():
|
|
continue
|
|
remote_path = f".gitea/ISSUE_TEMPLATE/{filepath.name}"
|
|
content_b64 = base64.b64encode(filepath.read_bytes()).decode()
|
|
|
|
existing = client.get_file_contents(org, repo, remote_path)
|
|
if existing:
|
|
sha = existing.get("sha", "")
|
|
client.create_or_update_file(
|
|
org,
|
|
repo,
|
|
remote_path,
|
|
content_b64,
|
|
f"更新 Issue Template: {filepath.name}",
|
|
sha=sha,
|
|
)
|
|
console.print(f" [blue]更新[/blue] {remote_path}")
|
|
else:
|
|
client.create_or_update_file(
|
|
org,
|
|
repo,
|
|
remote_path,
|
|
content_b64,
|
|
f"添加 Issue Template: {filepath.name}",
|
|
)
|
|
console.print(f" [green]创建[/green] {remote_path}")
|
|
count += 1
|
|
|
|
return count
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Label 同步
|
|
# ---------------------------------------------------------------------------
|
|
def sync_labels(client: GiteaClient, org: str, repo: str, cfg: GiteaConfig) -> int:
|
|
"""将预定义 Label 同步到仓库, 返回新增数量。"""
|
|
existing = {lbl.name for lbl in client.list_labels(org, repo)}
|
|
created = 0
|
|
for lbl_def in cfg.labels:
|
|
if lbl_def.name in existing:
|
|
continue
|
|
try:
|
|
client.create_label(org, repo, lbl_def.name, lbl_def.color, lbl_def.description)
|
|
created += 1
|
|
except GiteaClientError as e:
|
|
console.print(f" [red]创建 Label {lbl_def.name} 失败: {e.message}[/red]")
|
|
return created
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Issue 统计计算(纯函数, 无外部依赖)
|
|
# ---------------------------------------------------------------------------
|
|
def compute_issue_stats(issues: list[dict[str, Any]]) -> dict[str, Any]:
|
|
"""从 Issue 列表中计算统计数据。
|
|
|
|
返回值结构:
|
|
{
|
|
"total": int,
|
|
"open": int,
|
|
"closed": int,
|
|
"with_comments": int,
|
|
"no_reply": int,
|
|
"label_counts": Counter,
|
|
"user_counts": Counter,
|
|
}
|
|
"""
|
|
total = len(issues)
|
|
open_count = sum(1 for i in issues if i.get("state") == "open")
|
|
closed_count = sum(1 for i in issues if i.get("state") == "closed")
|
|
with_comments = sum(1 for i in issues if i.get("comments", 0) > 0)
|
|
no_reply = sum(1 for i in issues if i.get("state") == "open" and i.get("comments", 0) == 0)
|
|
|
|
label_counter: Counter[str] = Counter()
|
|
for issue in issues:
|
|
for label in issue.get("labels", []):
|
|
label_counter[label.get("name", "unknown")] += 1
|
|
|
|
user_counter: Counter[str] = Counter()
|
|
for issue in issues:
|
|
user = issue.get("user")
|
|
if user:
|
|
user_counter[user.get("login", "unknown")] += 1
|
|
|
|
return {
|
|
"total": total,
|
|
"open": open_count,
|
|
"closed": closed_count,
|
|
"with_comments": with_comments,
|
|
"no_reply": no_reply,
|
|
"label_counts": label_counter,
|
|
"user_counts": user_counter,
|
|
}
|