fix: build bitbucket line anchors (#4074)

This commit is contained in:
Neil 2026-05-31 03:25:45 -07:00 committed by GitHub
parent 7fa23737ad
commit 91865e21f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -54,7 +54,7 @@ describe('hosted remote URLs', () => {
expect(
buildHostedRemoteFileUrl('git@bitbucket.org:team/repo.git', 'src/a.ts', 'feature/x', 7)
).toBe('https://bitbucket.org/team/repo/src/feature%2Fx/src/a.ts#L7')
).toBe('https://bitbucket.org/team/repo/src/feature%2Fx/src/a.ts#a.ts-7')
expect(
buildHostedRemoteFileUrl(
@ -66,6 +66,12 @@ describe('hosted remote URLs', () => {
).toBe('https://github.com/Org/Repo/blob/feature%2Fx/src/a.ts#L5')
})
it('builds Bitbucket line fragments from the target file name', () => {
expect(
buildHostedRemoteFileUrl('https://bitbucket.org/team/repo.git', 'src/a file.ts', 'main', 29)
).toBe('https://bitbucket.org/team/repo/src/main/src/a%20file.ts#a%20file.ts-29')
})
it('rejects unsupported hosts and incomplete repo paths', () => {
expect(parseHostedRemote('git@example.com:team/repo.git')).toBeNull()
expect(parseHostedRemote('git@github.com:repo.git')).toBeNull()

View File

@ -96,6 +96,11 @@ function encodeRelativePath(path: string): string {
return path.replaceAll('\\', '/').split('/').filter(Boolean).map(encodeURIComponent).join('/')
}
function encodeBitbucketFileLineFragment(path: string, line: number): string {
const fileName = path.replaceAll('\\', '/').split('/').filter(Boolean).at(-1)
return fileName ? `#${encodeURIComponent(`${fileName}-${line}`)}` : ''
}
export function buildHostedRemoteFileUrl(
remoteUrl: string,
relativePath: string,
@ -119,5 +124,5 @@ export function buildHostedRemoteFileUrl(
if (remote.provider === 'gitlab') {
return `${baseUrl}/-/blob/${encodedBranch}${filePathSuffix}#L${line}`
}
return `${baseUrl}/src/${encodedBranch}${filePathSuffix}#L${line}`
return `${baseUrl}/src/${encodedBranch}${filePathSuffix}${encodeBitbucketFileLineFragment(relativePath, line)}`
}