diff --git a/.changeset/tough-pets-beg.md b/.changeset/tough-pets-beg.md new file mode 100644 index 0000000000..7fdcc6dc01 --- /dev/null +++ b/.changeset/tough-pets-beg.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Fix Bitbucket Cloud and Bitbucket Server line number reference. diff --git a/packages/integration/src/bitbucket/BitbucketIntegration.test.ts b/packages/integration/src/bitbucket/BitbucketIntegration.test.ts index 81b9be4e25..6bbe1be27b 100644 --- a/packages/integration/src/bitbucket/BitbucketIntegration.test.ts +++ b/packages/integration/src/bitbucket/BitbucketIntegration.test.ts @@ -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); diff --git a/packages/integration/src/bitbucket/BitbucketIntegration.ts b/packages/integration/src/bitbucket/BitbucketIntegration.ts index 6c21d716b7..0162463e3c 100644 --- a/packages/integration/src/bitbucket/BitbucketIntegration.ts +++ b/packages/integration/src/bitbucket/BitbucketIntegration.ts @@ -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 {