88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
"""只在 Gitea Wiki 创建任务归档;本地镜像由人工按需导出。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import re
|
||
from datetime import date
|
||
from pathlib import Path
|
||
|
||
from wiki_docs import (
|
||
DEFAULT_CONFIG,
|
||
WikiClient,
|
||
WikiDocsError,
|
||
load_config,
|
||
)
|
||
|
||
|
||
def safe_title(title: str) -> str:
|
||
"""把标题转换为适合 Wiki 页面名和 Windows 文件名的短文本。"""
|
||
|
||
cleaned = re.sub(r'[<>:"/\\|?*]', "-", title.strip())
|
||
cleaned = re.sub(r"\s+", "-", cleaned)
|
||
cleaned = re.sub(r"-+", "-", cleaned)
|
||
return cleaned.strip(".-")
|
||
|
||
|
||
def build_archive(
|
||
template: str,
|
||
issue_number: str,
|
||
title: str,
|
||
page_name: str,
|
||
issue_url: str,
|
||
) -> str:
|
||
content = template.replace("<工单号>", issue_number, 1)
|
||
content = content.replace("<标题>", title.strip(), 1)
|
||
content = content.replace("YYYY-MM-DD", date.today().isoformat(), 1)
|
||
content = content.replace("<链接>", issue_url, 1)
|
||
return content.replace("<页面名>", page_name, 1)
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser(
|
||
description="在 Gitea Wiki 创建任务归档,不自动导出本地镜像"
|
||
)
|
||
parser.add_argument("issue_number", help="Gitea 工单号,例如 123")
|
||
parser.add_argument("title", help="简短任务标题")
|
||
parser.add_argument("--config", default=str(DEFAULT_CONFIG), help="Wiki 映射配置")
|
||
args = parser.parse_args()
|
||
|
||
short_title = safe_title(args.title)
|
||
if not args.issue_number.isdigit():
|
||
print("错误:工单号必须是数字")
|
||
return 1
|
||
if not short_title:
|
||
print("错误:标题不能为空")
|
||
return 1
|
||
|
||
try:
|
||
config = load_config(Path(args.config).resolve())
|
||
page_name = f"Task-{args.issue_number}-{short_title}"
|
||
client = WikiClient(config)
|
||
if any(item.get("title") == page_name for item in client.list_pages()):
|
||
raise WikiDocsError(f"任务归档已经存在:{page_name}")
|
||
template = client.get_page("Task-Archive-Template").text
|
||
issue_url = (
|
||
f"{config.gitea_url}/{config.owner}/{config.repository}/issues/"
|
||
f"{args.issue_number}"
|
||
)
|
||
content = build_archive(
|
||
template, args.issue_number, args.title, page_name, issue_url
|
||
)
|
||
page = client.create_page(
|
||
page_name,
|
||
content,
|
||
f"docs: 创建任务 #{args.issue_number} 归档草稿",
|
||
)
|
||
except WikiDocsError as exc:
|
||
print(f"错误:{exc}")
|
||
return 1
|
||
|
||
print(f"已创建 Wiki:{page.html_url}")
|
||
print("未导出本地任务归档;需要时运行 export_task_archives.py")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|