如何让GitLab Changelog API生成适配GitLab Pages的绝对链接
解决GitLab Changelog API生成绝对URL的问题
GitLab Changelog API会向CHANGELOG.md追加提交标题和合并请求信息,但默认生成的链接是GitLab UI专用的短格式,无法直接用于mkdocs静态站点发布。需要将这些短链接替换为GitLab的绝对URL格式。
默认生成格式 vs 期望格式
- 提交链接默认:
[Commit message](group/repo@abcdef123456hash) - 提交链接期望:
[Commit message](https://gitlab.com/group/repo/-/commit/abcdef123456hash) - 合并请求链接默认:
group@repo!42 - 合并请求链接期望:
https://gitlab.com/group/repo/-/merge_requests/42
修改changelog_config.yml模板实现绝对URL
GitLab Changelog API的模板支持直接拼接URL,利用模板中提供的commit.hash(提交哈希)、merge_request.iid(MR编号)和项目完整路径,手动构建绝对链接:
template: | {% if categories %} {% each categories %} ### {{ title }} ({% if single_change %}1 change{% else %}{{ count }} changes{% end %}) {% each entries %} - [{{ title }}](https://gitlab.com/{{ project.full_path }}/-/commit/{{ commit.hash }})\ {% if author.credit %} by {{ author.reference }}{% end %}\ {% if commit.trailers.MR %}\ ([merge request](https://gitlab.com/{{ project.full_path }}/-/merge_requests/{{ commit.trailers.MR | split('!') | last }}))\ {% else %}\ {% if merge_request %}\ ([merge request](https://gitlab.com/{{ project.full_path }}/-/merge_requests/{{ merge_request.iid }}))\ {% end %}\ {% end %} {% end %} {% end %} {% else %} No changes. {% end %}
关键修改说明
- 提交链接:用
https://gitlab.com/{{ project.full_path }}/-/commit/{{ commit.hash }}替代原有的{{ commit.reference }},直接拼接项目完整路径和提交哈希生成绝对URL。 - 合并请求链接:
- 对于提交 trailer 中的MR(
commit.trailers.MR),格式是group/repo!42,用split('!') | last提取MR编号,再拼接成绝对URL。 - 对于直接关联的
merge_request,直接使用merge_request.iid(MR的内部编号)拼接URL。
- 对于提交 trailer 中的MR(
如果你的GitLab是自托管实例,只需将https://gitlab.com替换为你的实例域名即可。
内容的提问来源于stack exchange,提问作者astrochun
相关产品推荐
相关产品推荐

