-
Notifications
You must be signed in to change notification settings - Fork 6
feat: enrich review comments with clickable file:line links #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DGouron
merged 3 commits into
DGouron:master
from
CreatibOfficiel:feat/clickable-links-in-review-comments
Mar 4, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * Replace file:line references in a comment body with clickable GitLab/GitHub blob links. | ||
| * | ||
| * Pattern matches references like: | ||
| * file.py:42 | ||
| * `file.py:42` | ||
| * (file.py:42) | ||
| * users/api.py:247 | ||
| * | ||
| * Produces markdown links: | ||
| * [`file.py:42`](baseUrl/project/-/blob/sha/file.py#L42) | ||
| * | ||
| * Does NOT match: | ||
| * - URLs (http://localhost:8000, https://example.com:443) | ||
| * - Bare filenames without line numbers (docker-compose.yml) | ||
| */ | ||
|
|
||
| // File path must start with a word char, contain at least one dot (extension), then :lineNumber. | ||
| // The path must NOT start with // (to exclude URLs). | ||
| const FILE_LINE_PATTERN = | ||
| /(?<prefix>[`(]?)(?<filePath>[\w][\w./-]*\.[\w]+):(?<line>\d+)(?<suffix>[`)]?)/g | ||
|
|
||
| export function enrichCommentWithLinks( | ||
| body: string, | ||
| baseUrl: string, | ||
| projectPath: string, | ||
| headSha: string, | ||
| ): string { | ||
| return body.replace(FILE_LINE_PATTERN, (match, prefix, filePath, line, suffix, offset) => { | ||
| // Skip if preceded by :// (URL pattern like https://example.com:443) | ||
| const beforeMatch = body.slice(Math.max(0, offset - 10), offset) | ||
| if (/:\/{1,2}$/.test(beforeMatch) || /:\/{1,2}[\w.-]*$/.test(beforeMatch)) { | ||
| return match | ||
| } | ||
|
|
||
| // Skip if the filePath portion doesn't look like a real file (must contain a dot for extension) | ||
| // Already handled by regex, but double check for edge cases | ||
| if (!filePath.includes('.')) { | ||
| return match | ||
| } | ||
|
|
||
CreatibOfficiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const blobUrl = `${baseUrl}/${projectPath}/-/blob/${headSha}/${filePath}#L${line}` | ||
| // When wrapped in backticks, the link markdown already includes backticks — don't double them | ||
| const outerPrefix = prefix === '`' ? '' : prefix | ||
| const outerSuffix = suffix === '`' ? '' : suffix | ||
| return `${outerPrefix}[\`${filePath}:${line}\`](${blobUrl})${outerSuffix}` | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
| import { enrichCommentWithLinks } from '../../../services/commentLinkEnricher.js' | ||
DGouron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const BASE_URL = 'https://gitlab.example.com' | ||
| const PROJECT_PATH = 'my-org/my-project' | ||
| const HEAD_SHA = 'abc123def456' | ||
|
|
||
| function enrich(body: string): string { | ||
| return enrichCommentWithLinks(body, BASE_URL, PROJECT_PATH, HEAD_SHA) | ||
| } | ||
|
|
||
| describe('enrichCommentWithLinks', () => { | ||
| it('should replace a simple file:line reference with a link', () => { | ||
| const result = enrich('See file.py:42 for details') | ||
| expect(result).toBe( | ||
| 'See [`file.py:42`](https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/file.py#L42) for details' | ||
| ) | ||
| }) | ||
|
|
||
| it('should replace a backtick-wrapped file:line reference', () => { | ||
| const result = enrich('Check `file.py:42` for the issue') | ||
| expect(result).toBe( | ||
| 'Check [`file.py:42`](https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/file.py#L42) for the issue' | ||
| ) | ||
| }) | ||
|
|
||
| it('should replace a parenthesized file:line reference', () => { | ||
| const result = enrich('See issue (file.py:42)') | ||
| expect(result).toBe( | ||
| 'See issue ([`file.py:42`](https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/file.py#L42))' | ||
| ) | ||
| }) | ||
|
|
||
| it('should handle nested path references', () => { | ||
| const result = enrich('Found at users/api.py:247') | ||
| expect(result).toBe( | ||
| 'Found at [`users/api.py:247`](https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/users/api.py#L247)' | ||
| ) | ||
| }) | ||
|
|
||
| it('should handle deeply nested paths', () => { | ||
| const result = enrich('See src/components/posts/post-card.js:15') | ||
| expect(result).toBe( | ||
| 'See [`src/components/posts/post-card.js:15`](https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/src/components/posts/post-card.js#L15)' | ||
| ) | ||
| }) | ||
|
|
||
| it('should NOT match http:// URLs', () => { | ||
| const body = 'Visit http://localhost:8000/api' | ||
| expect(enrich(body)).toBe(body) | ||
| }) | ||
|
|
||
| it('should NOT match https:// URLs', () => { | ||
| const body = 'See https://example.com:443/path' | ||
| expect(enrich(body)).toBe(body) | ||
| }) | ||
|
|
||
| it('should NOT match filenames without line numbers', () => { | ||
| const body = 'Edit docker-compose.yml for config' | ||
| expect(enrich(body)).toBe(body) | ||
| }) | ||
|
|
||
| it('should handle multiple references in the same body', () => { | ||
| const result = enrich('Issues in file.py:10 and users/views.py:25 and lib/utils.py:100') | ||
| expect(result).toContain('[`file.py:10`]') | ||
| expect(result).toContain('[`users/views.py:25`]') | ||
| expect(result).toContain('[`lib/utils.py:100`]') | ||
| }) | ||
|
|
||
| it('should return body unchanged when all parameters are provided but no matches exist', () => { | ||
| const body = 'No file references here, just plain text.' | ||
| expect(enrich(body)).toBe(body) | ||
| }) | ||
|
|
||
| it('should handle file references at start and end of body', () => { | ||
| const result = enrich('file.py:1 is the start and end is file.py:99') | ||
| expect(result).toContain('[`file.py:1`]') | ||
| expect(result).toContain('[`file.py:99`]') | ||
| }) | ||
|
|
||
| it('should handle references in markdown context', () => { | ||
| const result = enrich('- **Blocking**: Missing validation in `users/api.py:247`') | ||
| expect(result).toContain('[`users/api.py:247`]') | ||
| expect(result).toContain('https://gitlab.example.com/my-org/my-project/-/blob/abc123def456/users/api.py#L247') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.