feat(links): auto-remap base-branch GitHub URLs to PR branch#18
feat(links): auto-remap base-branch GitHub URLs to PR branch#18zeitlinger merged 1 commit intomainfrom
Conversation
Add build_remap_args() to generate lychee --remap flags that redirect /blob/main/ and /tree/main/ URLs to the PR branch. Works in both CI (using GITHUB_* env vars) and locally (parsing git remote/branch info). Supports fork PRs via PR_HEAD_REPO env var. Skips silently when on the default branch or when repo info can't be determined. Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
There was a problem hiding this comment.
Pull request overview
This PR adds automatic URL remapping functionality to the lint:links task to redirect base-branch GitHub URLs to the PR branch during link checking. This eliminates the need for consuming repositories to add manual sed commands in their CI workflows.
Changes:
- Added
build_remap_args()function that generates--remapflags for lychee to redirect/blob/{base}/and/tree/{base}/URLs - Modified
run_lychee()to collect and pass remap arguments to lychee - Supports both CI environments (via
GITHUB_*env vars) and local execution (via git commands), with fork PR support viaPR_HEAD_REPO
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| echo "${base_url}/blob/${base_ref}/(.*) ${head_url}/blob/${head_ref}/\$1" | ||
| echo "--remap" | ||
| echo "${base_url}/tree/${base_ref}/(.*) ${head_url}/tree/${head_ref}/\$1" |
There was a problem hiding this comment.
The remap pattern is missing regex anchors (^ and $). Without anchors, the pattern could match partial URLs incorrectly. The pattern should be anchored to match complete URLs: ^${base_url}/blob/${base_ref}/(.*)$
This is particularly important because lychee uses these patterns for regex matching, and without anchors, a URL like https://example.com/link-to-https://github.com/owner/repo/blob/main/file.md could be incorrectly matched and remapped.
| echo "${base_url}/blob/${base_ref}/(.*) ${head_url}/blob/${head_ref}/\$1" | |
| echo "--remap" | |
| echo "${base_url}/tree/${base_ref}/(.*) ${head_url}/tree/${head_ref}/\$1" | |
| echo "^${base_url}/blob/${base_ref}/(.*)\$ ${head_url}/blob/${head_ref}/\$1" | |
| echo "--remap" | |
| echo "^${base_url}/tree/${base_ref}/(.*)\$ ${head_url}/tree/${head_ref}/\$1" |
| echo "${base_url}/blob/${base_ref}/(.*) ${head_url}/blob/${head_ref}/\$1" | ||
| echo "--remap" | ||
| echo "${base_url}/tree/${base_ref}/(.*) ${head_url}/tree/${head_ref}/\$1" |
There was a problem hiding this comment.
The remap pattern is missing regex anchors (^ and $). Without anchors, the pattern could match partial URLs incorrectly. The pattern should be anchored to match complete URLs: ^${base_url}/tree/${base_ref}/(.*)$
This is particularly important because lychee uses these patterns for regex matching, and without anchors, a URL like https://example.com/link-to-https://github.com/owner/repo/tree/main/path could be incorrectly matched and remapped.
| echo "${base_url}/blob/${base_ref}/(.*) ${head_url}/blob/${head_ref}/\$1" | |
| echo "--remap" | |
| echo "${base_url}/tree/${base_ref}/(.*) ${head_url}/tree/${head_ref}/\$1" | |
| echo "^${base_url}/blob/${base_ref}/(.*)$ ${head_url}/blob/${head_ref}/\$1" | |
| echo "--remap" | |
| echo "^${base_url}/tree/${base_ref}/(.*)$ ${head_url}/tree/${head_ref}/\$1" |
| local base_url="https://github.com/${repo}" | ||
| local head_url="https://github.com/${head_repo}" | ||
|
|
||
| echo "--remap" | ||
| echo "${base_url}/blob/${base_ref}/(.*) ${head_url}/blob/${head_ref}/\$1" | ||
| echo "--remap" | ||
| echo "${base_url}/tree/${base_ref}/(.*) ${head_url}/tree/${head_ref}/\$1" |
There was a problem hiding this comment.
Branch names and repository names may contain special characters (like / in branch names for feature branches like feature/my-feature) that could break the regex pattern. These values should be escaped before being used in the regex pattern to prevent regex interpretation of special characters.
Consider using a regex escaping function or quoting mechanism to handle special characters like ., *, +, ?, [, ], {, }, (, ), ^, $, |, \, and /.
## Summary - Add `^` and `$` regex anchors to the lychee `--remap` patterns to prevent partial URL matching Addresses review feedback from #18 — without anchors, a URL embedded inside another URL (e.g., `https://example.com/link-to-https://github.com/org/repo/blob/main/file.md`) could be incorrectly remapped. ## Test plan - [x] Lint passes (`mise run lint`) - [x] CI simulation: remap patterns include `^` prefix and `$` suffix Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
🤖 I have created a release *beep* *boop* --- ## [0.3.0](v0.2.0...v0.3.0) (2026-02-16) ### Features * add Renovate shareable preset for consuming repos ([#17](#17)) ([8a06590](8a06590)) * **links:** auto-remap base-branch GitHub URLs to PR branch ([#18](#18)) ([dd6cc61](dd6cc61)) ### Bug Fixes * **links:** add regex anchors to remap patterns ([#19](#19)) ([2e17348](2e17348)) * replace broken release-please PR comment with docs ([#12](#12)) ([817b37d](817b37d)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Summary
build_remap_args()tolinks.shthat generates lychee--remapflags to redirect/blob/{base}/and/tree/{base}/GitHub URLs to the PR branchGITHUB_REPOSITORY,GITHUB_BASE_REF,GITHUB_HEAD_REF) and locally (parsing git remote URL and branch info)PR_HEAD_REPOenv var; skips silently when on the default branch or when repo info can't be determinedThis eliminates the need for consuming repos to add boilerplate
sedcommands in their CI workflows to remap branch URLs during link checking (e.g., prometheus/client_java#1883).Test plan
mise run lint)main): no remap args generatedGITHUB_REPOSITORY/GITHUB_BASE_REF/GITHUB_HEAD_REF): correct remap argsPR_HEAD_REPO): remaps to fork repo