Skip to content

Commit

Permalink
fix(merge-confidence): ensure URL path has trailing slashes (#27536)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Ladzaretti committed Feb 25, 2024
1 parent 5ea25f7 commit 29562a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 17 additions & 0 deletions lib/util/merge-confidence/index.spec.ts
Expand Up @@ -347,6 +347,23 @@ describe('util/merge-confidence/index', () => {
);
});

it('uses a custom base url containing path', async () => {
const renovateApi = 'https://domain.com/proxy/renovate-api';
process.env.RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL = renovateApi;
httpMock.scope(renovateApi).get(`/api/mc/availability`).reply(200);

await expect(initMergeConfidence()).toResolve();

expect(logger.trace).toHaveBeenCalledWith(
expect.anything(),
'using merge confidence API base found in environment variables',
);
expect(logger.debug).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API - successfully authenticated',
);
});

it('resolves if no token', async () => {
hostRules.clear();

Expand Down
14 changes: 11 additions & 3 deletions lib/util/merge-confidence/index.ts
Expand Up @@ -7,6 +7,7 @@ import * as packageCache from '../cache/package';
import { parseJson } from '../common';
import * as hostRules from '../host-rules';
import { Http } from '../http';
import { ensureTrailingSlash, joinUrlParts } from '../url';
import { MERGE_CONFIDENCE } from './common';
import type { MergeConfidence } from './types';

Expand Down Expand Up @@ -164,7 +165,14 @@ async function queryApi(
}

const escapedPackageName = packageName.replace('/', '%2f');
const url = `${apiBaseUrl}api/mc/json/${datasource}/${escapedPackageName}/${currentVersion}/${newVersion}`;
const url = joinUrlParts(
apiBaseUrl,
'api/mc/json',
datasource,
escapedPackageName,
currentVersion,
newVersion,
);
const cacheKey = `${token}:${url}`;
const cachedResult = await packageCache.get(hostType, cacheKey);

Expand Down Expand Up @@ -217,7 +225,7 @@ export async function initMergeConfidence(): Promise<void> {
return;
}

const url = `${apiBaseUrl}api/mc/availability`;
const url = joinUrlParts(apiBaseUrl, 'api/mc/availability');
try {
await http.get(url);
} catch (err) {
Expand Down Expand Up @@ -246,7 +254,7 @@ function getApiBaseUrl(): string {
{ baseUrl: parsedBaseUrl },
'using merge confidence API base found in environment variables',
);
return parsedBaseUrl;
return ensureTrailingSlash(parsedBaseUrl);
} catch (err) {
logger.warn(
{ err, baseFromEnv },
Expand Down

0 comments on commit 29562a1

Please sign in to comment.