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:
@@ -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