Skip to content

Comments

feat(links): auto-remap base-branch GitHub URLs to PR branch#18

Merged
zeitlinger merged 1 commit intomainfrom
feat/links-remap-pr-branch
Feb 16, 2026
Merged

feat(links): auto-remap base-branch GitHub URLs to PR branch#18
zeitlinger merged 1 commit intomainfrom
feat/links-remap-pr-branch

Conversation

@zeitlinger
Copy link
Member

Summary

  • Add build_remap_args() to links.sh that generates lychee --remap flags to redirect /blob/{base}/ and /tree/{base}/ GitHub URLs to the PR branch
  • Works in CI (using GITHUB_REPOSITORY, GITHUB_BASE_REF, GITHUB_HEAD_REF) and locally (parsing git remote URL and 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

This eliminates the need for consuming repos to add boilerplate sed commands in their CI workflows to remap branch URLs during link checking (e.g., prometheus/client_java#1883).

Test plan

  • Lint passes (mise run lint)
  • On default branch (main): no remap args generated
  • CI simulation (GITHUB_REPOSITORY/GITHUB_BASE_REF/GITHUB_HEAD_REF): correct remap args
  • Fork CI simulation (with PR_HEAD_REPO): remaps to fork repo
  • Head == base branch: no remap args generated

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>
Copilot AI review requested due to automatic review settings February 16, 2026 17:53
@zeitlinger zeitlinger merged commit dd6cc61 into main Feb 16, 2026
12 checks passed
@zeitlinger zeitlinger deleted the feat/links-remap-pr-branch branch February 16, 2026 17:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --remap flags 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 via PR_HEAD_REPO

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +57 to +59
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"
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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"

Copilot uses AI. Check for mistakes.
Comment on lines +57 to +59
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"
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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"

Copilot uses AI. Check for mistakes.
Comment on lines +53 to +59
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"
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 /.

Copilot uses AI. Check for mistakes.
zeitlinger added a commit that referenced this pull request Feb 16, 2026
## 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>
zeitlinger pushed a commit that referenced this pull request Feb 16, 2026
🤖 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant