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:
@@ -1,5 +1,8 @@
|
||||
[default.gitea]
|
||||
url = "https://cpp.tongji.sh:3000"
|
||||
# 同学实际访问的平台地址(Apache 反代 /QA -> 127.0.0.1:2080)。
|
||||
# 用于 Issue Template 等面向用户的链接; url 只用于 CLI 调 API。
|
||||
public_url = "https://cpp.tongji.sh/QA"
|
||||
email_domain = "tongji.edu.cn"
|
||||
default_org = ""
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user