Gerrit: Gitiles auth-links could still be broken.

This change includes a much more robust way to insert the /a/-
prefix in the correct place.

We now check that gitiles-files are actually served from the
Gerrit-server and if so, the authentication prefix is inserted after
Gerrit's baseUrl.

Signed-off-by: Gustaf Lundh <gustaf.lundh@axis.com>
This commit is contained in:
Gustaf Lundh
2023-08-21 18:26:58 +02:00
parent cb2e19d82d
commit 2d2fc9d20e
3 changed files with 26 additions and 14 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/integration': patch
---
Gerrit: Gitiles auth-links could still be broken
+8 -6
View File
@@ -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',
);
});
});
+13 -8
View File
@@ -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!;
}
/**