fix(bitbucket): fix line number references

Use the actual line number syntax for Bitbucket Cloud
and Bitbucket Server.

Closes: #9764

Signed-off-by: Patrick Jungermann <Patrick.Jungermann@gmail.com>
This commit is contained in:
Patrick Jungermann
2022-02-25 00:58:18 +01:00
parent dd2e1eb59a
commit 33d5e79822
3 changed files with 35 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/integration': patch
---
Fix Bitbucket Cloud and Bitbucket Server line number reference.
@@ -45,8 +45,10 @@ describe('BitbucketIntegration', () => {
expect(integration.title).toBe('h.com');
});
it('resolves url line number correctly', () => {
const integration = new BitbucketIntegration({ host: 'h.com' } as any);
it('resolves url line number correctly for Bitbucket Cloud', () => {
const integration = new BitbucketIntegration({
host: 'bitbucket.org',
} as any);
expect(
integration.resolveUrl({
@@ -55,10 +57,22 @@ describe('BitbucketIntegration', () => {
lineNumber: 14,
}),
).toBe(
'https://bitbucket.org/my-owner/my-project/src/master/a.yaml#a.yaml-14',
'https://bitbucket.org/my-owner/my-project/src/master/a.yaml#lines-14',
);
});
it('resolves url line number correctly for Bitbucket Server', () => {
const integration = new BitbucketIntegration({ host: 'h.com' } as any);
expect(
integration.resolveUrl({
url: './a.yaml',
base: 'https://bitbucket.org/my-owner/my-project/src/master/README.md',
lineNumber: 14,
}),
).toBe('https://bitbucket.org/my-owner/my-project/src/master/a.yaml#14');
});
it('resolve edit URL', () => {
const integration = new BitbucketIntegration({ host: 'h.com' } as any);
@@ -60,17 +60,21 @@ export class BitbucketIntegration implements ScmIntegration {
lineNumber?: number;
}): string {
const resolved = defaultScmResolveUrl(options);
// Bitbucket line numbers use the syntax #example.txt-42, rather than #L42
if (options.lineNumber) {
const url = new URL(resolved);
const filename = url.pathname.split('/').slice(-1)[0];
url.hash = `${filename}-${options.lineNumber}`;
return url.toString();
if (!options.lineNumber) {
return resolved;
}
return resolved;
const url = new URL(resolved);
if (this.integrationConfig.host === 'bitbucket.org') {
// Bitbucket Cloud uses the syntax #lines-{start}[:{end}][,...]
url.hash = `lines-${options.lineNumber}`;
} else {
// Bitbucket Server uses the syntax #{start}[-{end}][,...]
url.hash = `${options.lineNumber}`;
}
return url.toString();
}
resolveEditUrl(url: string): string {