fix: build bitbucket line anchors (#4074)
This commit is contained in:
parent
7fa23737ad
commit
91865e21f3
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)}`
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue