diff --git a/.changeset/grumpy-papayas-tie.md b/.changeset/grumpy-papayas-tie.md new file mode 100644 index 0000000000..a5635afc67 --- /dev/null +++ b/.changeset/grumpy-papayas-tie.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Gerrit: Gitiles auth-links could still be broken diff --git a/packages/integration/src/gerrit/core.test.ts b/packages/integration/src/gerrit/core.test.ts index 6f6a8ed58f..3226c7bf3e 100644 --- a/packages/integration/src/gerrit/core.test.ts +++ b/packages/integration/src/gerrit/core.test.ts @@ -37,15 +37,17 @@ describe('gerrit core', () => { describe('buildGerritGitilesArchiveUrl', () => { const config: GerritIntegrationConfig = { host: 'gerrit.com', + baseUrl: 'https://gerrit.com', gitilesBaseUrl: 'https://gerrit.com/gitiles', }; const configWithPath: GerritIntegrationConfig = { host: 'gerrit.com', - gitilesBaseUrl: 'https://gerrit.com/path/gitiles', + baseUrl: 'https://gerrit.com/gerrit', + gitilesBaseUrl: 'https://gerrit.com/gerrit/plugins/gitiles', }; - const configWithPathSlash: GerritIntegrationConfig = { + const configWithDedicatedGitiles: GerritIntegrationConfig = { host: 'gerrit.com', - gitilesBaseUrl: 'https://gerrit.com/path1/path2/gitiles/', + gitilesBaseUrl: 'https://dedicated-gitiles-server.com/gerrit/gitiles', }; it('can create an archive url for a branch', () => { expect(buildGerritGitilesArchiveUrl(config, 'repo', 'dev', '')).toEqual( @@ -84,19 +86,19 @@ describe('gerrit core', () => { expect( buildGerritGitilesArchiveUrl(authConfig, 'repo', 'dev', 'docs'), ).toEqual( - 'https://gerrit.com/path/a/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz', + 'https://gerrit.com/gerrit/a/plugins/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, + ...configWithDedicatedGitiles, 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', + 'https://dedicated-gitiles-server.com/gerrit/gitiles/repo/+archive/refs/heads/dev/docs.tar.gz', ); }); }); diff --git a/packages/integration/src/gerrit/core.ts b/packages/integration/src/gerrit/core.ts index e104ffc7fb..b69aca429d 100644 --- a/packages/integration/src/gerrit/core.ts +++ b/packages/integration/src/gerrit/core.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { logger } from '@azure/identity'; import { trimStart } from 'lodash'; import { GerritIntegrationConfig } from './config'; @@ -156,14 +157,18 @@ export function getAuthenticationPrefix( export function getGitilesAuthenticationUrl( config: GerritIntegrationConfig, ): string { - const parsedUrl = new URL(config.gitilesBaseUrl!); - parsedUrl.pathname = parsedUrl.pathname.replace(/\/?$/, ''); - parsedUrl.pathname = parsedUrl.pathname.replace( - /\/([^\/]*)$/, - `${getAuthenticationPrefix(config)}$1`, - ); - - return parsedUrl.toString(); + if (config.gitilesBaseUrl!.startsWith(config.baseUrl!)) { + return config.gitilesBaseUrl!.replace( + config.baseUrl!.concat('/'), + config.baseUrl!.concat(getAuthenticationPrefix(config)), + ); + } + if (config.password) { + logger.warning( + 'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.', + ); + } + return config.gitilesBaseUrl!; } /**