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:
2026-09-11 17:07:11 +08:00
parent 44b174d319
commit 68fb567cb3
9 changed files with 139 additions and 18 deletions
+50 -2
View File
@@ -1,10 +1,17 @@
"""utils.py 单元测试: parse_repo, compute_issue_stats"""
"""utils.py 单元测试: parse_repo, compute_issue_stats, render_template_placeholders"""
from __future__ import annotations
import pytest
from pydantic import SecretStr
from tjhlp.utils import compute_issue_stats, parse_repo
from tjhlp.config import GiteaConfig
from tjhlp.utils import (
compute_issue_stats,
parse_repo,
render_template_placeholders,
web_base_url,
)
# typer.Exit 在非 CLI 上下文中抛出 click.exceptions.Exit
_EXIT_EXCEPTIONS = (SystemExit, Exception)
@@ -103,3 +110,44 @@ class TestComputeIssueStats:
]
stats = compute_issue_stats(issues)
assert stats["no_reply"] == 1
class TestRenderTemplatePlaceholders:
def test_replaces_all_placeholders(self):
out = render_template_placeholders(
'url: "{{BASE_URL}}/{{ORG}}/{{REPO}}/issues"',
org="tjhlp-2025f",
repo="hw01",
web_base="https://cpp.tongji.sh/QA/",
)
assert out == 'url: "https://cpp.tongji.sh/QA/tjhlp-2025f/hw01/issues"'
def test_strips_trailing_slash_of_web_base(self):
out = render_template_placeholders(
"{{BASE_URL}}/assets/x.html", "org", "repo", "http://h/QA//"
)
assert out == "http://h/QA/assets/x.html"
def test_leaves_base_url_when_not_configured(self):
# public_url 没配时应保留占位符(由 push_templates 打警告), 而不是生成坏链接
out = render_template_placeholders("{{BASE_URL}}/x", "org", "repo")
assert "{{BASE_URL}}" in out
def test_text_without_placeholders_is_unchanged(self):
text = "blank_issues_enabled: false\n"
assert render_template_placeholders(text, "org", "repo", "http://h") == text
class TestWebBaseUrl:
def _cfg(self, **kw):
base = {"url": "https://cpp.tongji.sh:3000", "token": SecretStr("x")}
return GiteaConfig(**{**base, **kw})
def test_prefers_public_url(self):
assert (
web_base_url(self._cfg(public_url="https://cpp.tongji.sh/QA/"))
== "https://cpp.tongji.sh/QA"
)
def test_falls_back_to_url(self):
assert web_base_url(self._cfg()) == "https://cpp.tongji.sh:3000"