SHA256
forked from course-team/tjhlp-gitea
feat: Issue 模板引导链接改指课程官网, 平台地址改由 public_url 注入
- 两个 config.yaml 的 contact_links 从 example.com 换成真实入口:
官网《学会提问》/ 课程官网 / 平台提问速查, 让答疑平台与官网互相导流
- 模板支持 {{BASE_URL}}/{{ORG}}/{{REPO}} 占位符, 推送时替换,
换学期建新组织不用改模板; 未配置时保留占位符并告警而不是推坏链接
- scripts: 新增 gitea.public_url(对外地址, 与 CLI 用的 url 区分),
utils.web_base_url()/render_template_placeholders() 及单元测试
This commit is contained in:
@@ -9,7 +9,7 @@ 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
|
||||
from tjhlp.utils import parse_repo, push_templates, sync_labels, web_base_url
|
||||
|
||||
app = typer.Typer(no_args_is_help=True)
|
||||
console = Console()
|
||||
@@ -69,7 +69,7 @@ def create_homework(
|
||||
# 推送作业 Issue Templates
|
||||
console.print("\n推送 Issue Templates...")
|
||||
hw_tpl_dir = _TEMPLATES_DIR / "homework"
|
||||
n = push_templates(client, org, repo_name, hw_tpl_dir)
|
||||
n = push_templates(client, org, repo_name, hw_tpl_dir, web_base_url(cfg))
|
||||
console.print(f" 共推送 {n} 个模板文件")
|
||||
|
||||
# 同步 Labels
|
||||
@@ -120,7 +120,7 @@ def init_templates(
|
||||
|
||||
cfg = get_gitea_config()
|
||||
with GiteaClient(cfg) as client:
|
||||
n = push_templates(client, org_name, repo_name, tpl_dir)
|
||||
n = push_templates(client, org_name, repo_name, tpl_dir, web_base_url(cfg))
|
||||
console.print(f"\n共推送 {n} 个模板文件到 {repo}")
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ from rich.console import Console
|
||||
|
||||
from tjhlp.client import GiteaClient, GiteaClientError
|
||||
from tjhlp.config import get_gitea_config
|
||||
from tjhlp.utils import push_templates, sync_labels
|
||||
from tjhlp.utils import push_templates, sync_labels, web_base_url
|
||||
|
||||
app = typer.Typer(no_args_is_help=True)
|
||||
console = Console()
|
||||
@@ -86,7 +86,7 @@ def init(
|
||||
console.print("\n[bold]4. 推送 Issue Templates 到 general 仓库[/bold]")
|
||||
general_tpl_dir = _TEMPLATES_DIR / "general"
|
||||
if general_tpl_dir.exists():
|
||||
n = push_templates(client, org, "general", general_tpl_dir)
|
||||
n = push_templates(client, org, "general", general_tpl_dir, web_base_url(cfg))
|
||||
console.print(f" 共推送 {n} 个模板文件")
|
||||
else:
|
||||
console.print(f" [red]模板目录不存在: {general_tpl_dir}[/red]")
|
||||
|
||||
@@ -55,6 +55,10 @@ class GiteaConfig(BaseModel):
|
||||
token: SecretStr
|
||||
email_domain: str = "tongji.edu.cn"
|
||||
default_org: str = ""
|
||||
# 面向用户的访问地址(Apache 反代后的地址, 如 https://cpp.tongji.sh/QA)。
|
||||
# url 是 CLI 调 API 用的地址, 可能带内部端口, 不能直接写进 Issue Template 等
|
||||
# 给同学看的链接里; 留空时回退到 url。
|
||||
public_url: str = ""
|
||||
labels: list[LabelDef] = []
|
||||
|
||||
|
||||
|
||||
@@ -34,11 +34,38 @@ def parse_repo(repo: str) -> tuple[str, str]:
|
||||
# ---------------------------------------------------------------------------
|
||||
# Issue Template 推送
|
||||
# ---------------------------------------------------------------------------
|
||||
# 模板占位符: 让 Issue Template 不必硬编码学期组织名/访问地址
|
||||
# {{BASE_URL}} -> gitea.public_url(面向同学的地址)
|
||||
# {{ORG}} / {{REPO}} -> 推送目标
|
||||
TEMPLATE_PLACEHOLDERS = ("{{BASE_URL}}", "{{ORG}}", "{{REPO}}")
|
||||
|
||||
|
||||
def web_base_url(cfg: GiteaConfig) -> str:
|
||||
"""面向同学的平台访问地址: 优先 gitea.public_url, 否则退回 gitea.url。"""
|
||||
return (cfg.public_url or str(cfg.url)).rstrip("/")
|
||||
|
||||
|
||||
def render_template_placeholders(text: str, org: str, repo: str, web_base: str = "") -> str:
|
||||
"""替换 Issue Template 中的占位符, 返回渲染后的文本。"""
|
||||
base = (web_base or "").rstrip("/")
|
||||
if base:
|
||||
text = text.replace("{{BASE_URL}}", base)
|
||||
text = text.replace("{{ORG}}", org).replace("{{REPO}}", repo)
|
||||
left = [p for p in TEMPLATE_PLACEHOLDERS if p in text]
|
||||
if left:
|
||||
console.print(
|
||||
f"[yellow]警告: 模板中仍有未替换的占位符 {', '.join(left)}"
|
||||
f"(检查 settings.toml 的 gitea.public_url)[/yellow]"
|
||||
)
|
||||
return text
|
||||
|
||||
|
||||
def push_templates(
|
||||
client: GiteaClient,
|
||||
org: str,
|
||||
repo: str,
|
||||
template_dir: Path,
|
||||
web_base: str = "",
|
||||
) -> int:
|
||||
"""将本地 issue template 目录推送到远端仓库, 返回推送的文件数。"""
|
||||
count = 0
|
||||
@@ -51,7 +78,13 @@ def push_templates(
|
||||
if not filepath.is_file():
|
||||
continue
|
||||
remote_path = f".gitea/ISSUE_TEMPLATE/{filepath.name}"
|
||||
content_b64 = base64.b64encode(filepath.read_bytes()).decode()
|
||||
text = render_template_placeholders(
|
||||
filepath.read_text(encoding="utf-8"),
|
||||
org=org,
|
||||
repo=repo,
|
||||
web_base=web_base,
|
||||
)
|
||||
content_b64 = base64.b64encode(text.encode("utf-8")).decode()
|
||||
|
||||
existing = client.get_file_contents(org, repo, remote_path)
|
||||
if existing:
|
||||
|
||||
Reference in New Issue
Block a user