From cb2e19d82d95ac242fbc4781e7f168a698965377 Mon Sep 17 00:00:00 2001 From: Gustaf Lundh Date: Thu, 17 Aug 2023 12:56:02 +0200 Subject: [PATCH] Gitiles: Fix issue where the auth prefix was misplaced Bug: #19428 Signed-off-by: Gustaf Lundh --- .changeset/orange-houses-fly.md | 5 +++ packages/integration/src/gerrit/core.test.ts | 32 ++++++++++++++++++++ packages/integration/src/gerrit/core.ts | 10 ++++-- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 .changeset/orange-houses-fly.md diff --git a/.changeset/orange-houses-fly.md b/.changeset/orange-houses-fly.md new file mode 100644 index 0000000000..546484304c --- /dev/null +++ b/.changeset/orange-houses-fly.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Gitiles: Fixed auth prefix issue diff --git a/packages/integration/src/gerrit/core.test.ts b/packages/integration/src/gerrit/core.test.ts index c00bae384f..6f6a8ed58f 100644 --- a/packages/integration/src/gerrit/core.test.ts +++ b/packages/integration/src/gerrit/core.test.ts @@ -39,6 +39,14 @@ describe('gerrit core', () => { host: 'gerrit.com', gitilesBaseUrl: 'https://gerrit.com/gitiles', }; + const configWithPath: GerritIntegrationConfig = { + host: 'gerrit.com', + gitilesBaseUrl: 'https://gerrit.com/path/gitiles', + }; + const configWithPathSlash: GerritIntegrationConfig = { + host: 'gerrit.com', + gitilesBaseUrl: 'https://gerrit.com/path1/path2/gitiles/', + }; it('can create an archive url for a branch', () => { expect(buildGerritGitilesArchiveUrl(config, 'repo', 'dev', '')).toEqual( 'https://gerrit.com/gitiles/repo/+archive/refs/heads/dev.tar.gz', @@ -67,6 +75,30 @@ describe('gerrit core', () => { 'https://gerrit.com/a/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz', ); }); + it('can create an authenticated url when auth is enabled and an url-path is used', () => { + const authConfig = { + ...configWithPath, + username: 'username', + password: 'password', + }; + expect( + buildGerritGitilesArchiveUrl(authConfig, 'repo', 'dev', 'docs'), + ).toEqual( + 'https://gerrit.com/path/a/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz', + ); + }); + it('can create an authenticated url when auth is enabled and an url-path + slash is used', () => { + const authConfig = { + ...configWithPathSlash, + username: 'username', + password: 'password', + }; + expect( + buildGerritGitilesArchiveUrl(authConfig, 'repo', 'dev', 'docs'), + ).toEqual( + 'https://gerrit.com/path1/path2/a/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz', + ); + }); }); describe('buildGerritGitilesUrl', () => { diff --git a/packages/integration/src/gerrit/core.ts b/packages/integration/src/gerrit/core.ts index 107d5a4469..e104ffc7fb 100644 --- a/packages/integration/src/gerrit/core.ts +++ b/packages/integration/src/gerrit/core.ts @@ -157,9 +157,13 @@ export function getGitilesAuthenticationUrl( config: GerritIntegrationConfig, ): string { const parsedUrl = new URL(config.gitilesBaseUrl!); - return `${parsedUrl.protocol}//${parsedUrl.host}${getAuthenticationPrefix( - config, - )}${parsedUrl.pathname.substring(1)}`; + parsedUrl.pathname = parsedUrl.pathname.replace(/\/?$/, ''); + parsedUrl.pathname = parsedUrl.pathname.replace( + /\/([^\/]*)$/, + `${getAuthenticationPrefix(config)}$1`, + ); + + return parsedUrl.toString(); } /**