Skip to content

Commit

Permalink
fix(composer): Support for "endpoint" and "baseUrl" hostRules (#7085)
Browse files Browse the repository at this point in the history
* fix(composer): Support for "endpoint" and "baseUrl" keys in COMPOSER_AUTH

* Fix lint
  • Loading branch information
zharinov committed Aug 24, 2020
1 parent 3437eba commit 8c45eb9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/manager/composer/__snapshots__/artifacts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Array [
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"COMPOSER_AUTH": "{\\"github-oauth\\":{\\"github.com\\":\\"github-token\\"},\\"gitlab-token\\":{\\"gitlab.com\\":\\"gitlab-token\\"},\\"http-basic\\":{\\"packagist.renovatebot.com\\":{\\"username\\":\\"some-username\\",\\"password\\":\\"some-password\\"}}}",
"COMPOSER_AUTH": "{\\"github-oauth\\":{\\"github.com\\":\\"github-token\\"},\\"gitlab-token\\":{\\"gitlab.com\\":\\"gitlab-token\\"},\\"http-basic\\":{\\"packagist.renovatebot.com\\":{\\"username\\":\\"some-username\\",\\"password\\":\\"some-password\\"},\\"artifactory.yyyyyyy.com\\":{\\"username\\":\\"some-other-username\\",\\"password\\":\\"some-other-password\\"}}}",
"COMPOSER_CACHE_DIR": "/tmp/renovate/cache/others/composer",
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
Expand Down
11 changes: 11 additions & 0 deletions lib/manager/composer/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ describe('.updateArtifacts()', () => {
username: 'some-username',
password: 'some-password',
});
hostRules.add({
hostType: datasourcePackagist.id,
endpoint: 'https://artifactory.yyyyyyy.com/artifactory/api/composer/',
username: 'some-other-username',
password: 'some-other-password',
});
hostRules.add({
hostType: datasourcePackagist.id,
username: 'some-other-username',
password: 'some-other-password',
});
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('Current composer.lock' as any);
Expand Down
36 changes: 28 additions & 8 deletions lib/manager/composer/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import url from 'url';
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import upath from 'upath';
Expand All @@ -8,6 +9,7 @@ import {
} from '../../constants/platforms';
import * as datasourcePackagist from '../../datasource/packagist';
import { logger } from '../../logger';
import { HostRule } from '../../types';
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
Expand All @@ -32,6 +34,25 @@ interface AuthJson {
'http-basic'?: Record<string, UserPass>;
}

function getHost({
hostName,
domainName,
endpoint,
baseUrl,
}: HostRule): string | null {
let host = hostName || domainName;
if (!host) {
try {
host = endpoint || baseUrl;
host = url.parse(host).host;
} catch (err) {
logger.warn(`Composer: can't parse ${host}`);
host = null;
}
}
return host;
}

function getAuthJson(): string | null {
const authJson: AuthJson = {};

Expand All @@ -57,15 +78,14 @@ function getAuthJson(): string | null {

hostRules
.findAll({ hostType: datasourcePackagist.id })
?.forEach(
({ username, password, hostName, domainName, endpoint, baseUrl }) => {
const host = hostName || domainName || endpoint || baseUrl;
if (host && username && password) {
authJson['http-basic'] = authJson['http-basic'] || {};
authJson['http-basic'][host] = { username, password };
}
?.forEach((hostRule) => {
const { username, password } = hostRule;
const host = getHost(hostRule);
if (host && username && password) {
authJson['http-basic'] = authJson['http-basic'] || {};
authJson['http-basic'][host] = { username, password };
}
);
});

return is.emptyObject(authJson) ? null : JSON.stringify(authJson);
}
Expand Down

0 comments on commit 8c45eb9

Please sign in to comment.