Skip to content

Conversation

@JustinBeckwith
Copy link
Owner

Summary

Fixes a critical bug where branch names containing slashes would be duplicated in GitHub URLs during pull request link validation.

The Problem

When a branch name like release-please/branches/main contained the base branch name as a substring (e.g., main), the greedy regex pattern would incorrectly match and duplicate the branch path:

Before (broken):

https://github.com/repo/blob/release-please/branches/release-please/branches/main/FILE.md

After (correct):

https://github.com/repo/blob/release-please/branches/main/FILE.md

Root Cause

The original regex pattern (/.*/)(main)/(.*) would:

  1. Greedily match /blob/release-please/branches/ as capture group $1
  2. Match main as capture group $2
  3. Replace with: $1 + release-please/branches/main → duplication!

The Fix

1. More Specific Regex Pattern

Changed from (/.*/)(${GITHUB_BASE_REF})/(.*) to /(blob|tree)/(${escapedBaseRef})/(.*)

This prevents the greedy (/.*/) from capturing parts of branch names by only matching /blob/ or /tree/ specifically.

2. Regex Character Escaping

Added proper escaping for special regex characters in branch names (dots, asterisks, etc.) so branch names like v1.0.0 work correctly.

3. Proper Capture Groups

Preserves whether the URL uses blob or tree in the replacement.

Tests Added

Added 5 comprehensive new tests to prevent regression:

✅ Branch names with slashes (release-please/branches/main)
✅ Branch names with multiple slashes (feature/deep/nested/branch)
✅ URLs already on head branch (exact bug scenario - should not rewrite)
✅ Branch names with special regex characters (v1.0.0)
✅ Tree URLs in addition to blob URLs

Test Results

  20 passing (111ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |   98.68 |    96.15 |     100 |   98.68 |                   
 action.js |   98.68 |    96.15 |     100 |   98.68 | 93-94,112         
-----------|---------|----------|---------|---------|-------------------

Changes

  • src/action.js:78-90 - Updated regex pattern with escaping and specific matching
  • test/test.js - Added 5 new comprehensive test cases
  • test/fixtures/* - Added test fixtures for slashed branch scenarios
  • dist/* - Rebuilt distribution bundle

Fixes #85

🤖 Generated with Claude Code

Fixed a critical bug where branch names containing slashes would be
duplicated in GitHub URLs during pull request link validation.

## The Problem

When a branch name like "release-please/branches/main" contained the
base branch name as a substring, the greedy regex pattern would
incorrectly match and duplicate the branch path:

- Bad: /blob/release-please/branches/release-please/branches/main/file.md
- Good: /blob/release-please/branches/main/file.md

The root cause was the regex pattern `(/.*/)(main)/(.*)` which greedily
matched `/blob/release-please/branches/` as the first capture group,
then when replacing with the full head ref, it duplicated the path.

## The Fix

1. **More specific regex**: Changed from `(/.*/)(${GITHUB_BASE_REF})/(.*)`
   to `/(blob|tree)/(${escapedBaseRef})/(.*)` to only match `/blob/` or
   `/tree/` specifically, preventing greedy matching of branch name parts

2. **Regex escaping**: Added proper escaping for special characters in
   branch names (dots, asterisks, etc.) so patterns like `v1.0.0` work

3. **Proper capture groups**: Preserves whether it's a blob or tree URL
   in the replacement

## Tests Added (5 new comprehensive tests)

- Branch names with slashes (release-please/branches/main)
- Branch names with multiple slashes (feature/deep/nested/branch)
- URLs already on head branch (regression test for exact bug scenario)
- Branch names with special regex characters (v1.0.0)
- Tree URLs in addition to blob URLs

All 20 tests passing with 98.68% coverage.

Fixes #85

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@JustinBeckwith JustinBeckwith merged commit ba135b2 into main Oct 21, 2025
4 checks passed
@JustinBeckwith JustinBeckwith deleted the fix/issue-85-branch-name-duplication branch October 21, 2025 01:22
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.

false positive for PRs from a branch name including slashes

2 participants