From f0ba514f0bb186e09ea96568523dab21a20637d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 23 Aug 2021 19:09:42 +0200 Subject: [PATCH] Take custom ports into account when matching integrations to URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/new-cars-watch.md | 5 ++++ packages/integration/src/helpers.test.ts | 30 +++++++++++++++++++++++- packages/integration/src/helpers.ts | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/new-cars-watch.md diff --git a/.changeset/new-cars-watch.md b/.changeset/new-cars-watch.md new file mode 100644 index 0000000000..3f31cca89a --- /dev/null +++ b/.changeset/new-cars-watch.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Take custom ports into account when matching integrations to URLs. It used to be the case that an integration with e.g. `host: 'scm.mycompany.net:8080'` would not be matched by the `byUrl` method, while hosts without a custom port did match. diff --git a/packages/integration/src/helpers.test.ts b/packages/integration/src/helpers.test.ts index 13e8694c73..a69c7faec9 100644 --- a/packages/integration/src/helpers.test.ts +++ b/packages/integration/src/helpers.test.ts @@ -14,7 +14,35 @@ * limitations under the License. */ -import { defaultScmResolveUrl, isValidHost } from './helpers'; +import { BitbucketIntegration } from './bitbucket'; +import { + basicIntegrations, + defaultScmResolveUrl, + isValidHost, +} from './helpers'; + +describe('basicIntegrations', () => { + describe('byUrl', () => { + it('handles hosts without a port', () => { + const integration = new BitbucketIntegration({ host: 'host.com' }); + const integrations = basicIntegrations( + [integration], + i => i.config.host, + ); + expect(integrations.byUrl('https://host.com/a')).toBe(integration); + expect(integrations.byUrl('https://host.com:8080/a')).toBeUndefined(); + }); + it('handles hosts with a port', () => { + const integration = new BitbucketIntegration({ host: 'host.com:8080' }); + const integrations = basicIntegrations( + [integration], + i => i.config.host, + ); + expect(integrations.byUrl('https://host.com:8080/a')).toBe(integration); + expect(integrations.byUrl('https://host.com/a')).toBeUndefined(); + }); + }); +}); describe('isValidHost', () => { it.each([ diff --git a/packages/integration/src/helpers.ts b/packages/integration/src/helpers.ts index b1e74035f1..dc1ce1a502 100644 --- a/packages/integration/src/helpers.ts +++ b/packages/integration/src/helpers.ts @@ -46,7 +46,7 @@ export function basicIntegrations( byUrl(url: string | URL): T | undefined { try { const parsed = typeof url === 'string' ? new URL(url) : url; - return integrations.find(i => getHost(i) === parsed.hostname); + return integrations.find(i => getHost(i) === parsed.host); } catch { return undefined; }