2019-07-13 07:48:47 +00:00
|
|
|
import URL from 'url';
|
|
|
|
import addrs from 'email-addresses';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { GlobalConfig } from '../config/types';
|
2020-01-12 07:50:11 +00:00
|
|
|
import { PLATFORM_NOT_FOUND } from '../constants/error-messages';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { logger } from '../logger';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { HostRule } from '../types';
|
2020-07-03 14:47:00 +00:00
|
|
|
import { setPrivateKey } from '../util/git';
|
2020-05-01 16:03:48 +00:00
|
|
|
import * as hostRules from '../util/host-rules';
|
2021-03-02 15:57:02 +00:00
|
|
|
import platforms from './api';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { Platform } from './types';
|
2019-05-20 08:59:30 +00:00
|
|
|
|
2021-03-02 20:44:55 +00:00
|
|
|
export * from './types';
|
2019-09-10 07:50:29 +00:00
|
|
|
|
2020-04-08 07:14:32 +00:00
|
|
|
export const getPlatformList = (): string[] => Array.from(platforms.keys());
|
|
|
|
export const getPlatforms = (): Map<string, Platform> => platforms;
|
2019-09-10 07:50:29 +00:00
|
|
|
|
2020-08-10 14:18:08 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2019-09-10 07:50:29 +00:00
|
|
|
let _platform: Platform;
|
|
|
|
|
|
|
|
const handler: ProxyHandler<Platform> = {
|
|
|
|
get(_target: Platform, prop: keyof Platform) {
|
2020-03-17 11:15:22 +00:00
|
|
|
if (!_platform) {
|
|
|
|
throw new Error(PLATFORM_NOT_FOUND);
|
|
|
|
}
|
2019-09-10 07:50:29 +00:00
|
|
|
return _platform[prop];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const platform = new Proxy<Platform>({} as any, handler);
|
|
|
|
|
2020-04-08 07:14:32 +00:00
|
|
|
export function setPlatformApi(name: string): void {
|
|
|
|
if (!platforms.has(name)) {
|
2019-05-20 08:59:30 +00:00
|
|
|
throw new Error(
|
2020-04-08 07:14:32 +00:00
|
|
|
`Init: Platform "${name}" not found. Must be one of: ${getPlatformList().join(
|
2019-09-10 07:50:29 +00:00
|
|
|
', '
|
|
|
|
)}`
|
2019-05-20 08:59:30 +00:00
|
|
|
);
|
2020-03-17 11:15:22 +00:00
|
|
|
}
|
2020-04-08 07:14:32 +00:00
|
|
|
_platform = platforms.get(name);
|
2019-09-10 07:50:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 06:03:46 +00:00
|
|
|
interface GitAuthor {
|
|
|
|
name?: string;
|
|
|
|
address?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseGitAuthor(input: string): GitAuthor | null {
|
|
|
|
let result: GitAuthor = null;
|
|
|
|
if (!input) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
result = addrs.parseOneAddress(input);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (input.includes('[bot]@')) {
|
|
|
|
// invalid github app/bot addresses
|
|
|
|
const parsed = addrs.parseOneAddress(
|
|
|
|
input.replace('[bot]@', '@')
|
|
|
|
) as addrs.ParsedMailbox;
|
|
|
|
if (parsed?.address) {
|
|
|
|
result = {
|
|
|
|
name: parsed.name || input.replace(/@.*/, ''),
|
|
|
|
address: parsed.address.replace('@', '[bot]@'),
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (input.includes('<') && input.includes('>')) {
|
|
|
|
// try wrapping the name part in quotations
|
|
|
|
result = addrs.parseOneAddress('"' + input.replace(/(\s?<)/, '"$1'));
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.error({ err }, 'Unknown error parsing gitAuthor');
|
|
|
|
}
|
|
|
|
// give up
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export async function initPlatform(
|
2021-02-05 11:43:26 +00:00
|
|
|
config: GlobalConfig
|
|
|
|
): Promise<GlobalConfig> {
|
2020-05-29 10:46:21 +00:00
|
|
|
setPrivateKey(config.gitPrivateKey);
|
2020-04-08 07:14:32 +00:00
|
|
|
setPlatformApi(config.platform);
|
2019-07-13 07:48:47 +00:00
|
|
|
// TODO: types
|
2019-09-10 07:50:29 +00:00
|
|
|
const platformInfo = await platform.initPlatform(config);
|
|
|
|
const returnConfig: any = { ...config, ...platformInfo };
|
2019-08-14 04:04:09 +00:00
|
|
|
let gitAuthor: string;
|
2021-03-04 05:21:55 +00:00
|
|
|
// istanbul ignore else
|
2020-07-18 06:42:32 +00:00
|
|
|
if (config?.gitAuthor) {
|
2020-02-24 07:43:01 +00:00
|
|
|
logger.debug(`Using configured gitAuthor (${config.gitAuthor})`);
|
2019-05-24 14:34:52 +00:00
|
|
|
gitAuthor = config.gitAuthor;
|
2021-03-04 05:21:55 +00:00
|
|
|
} else if (platformInfo?.gitAuthor) {
|
|
|
|
logger.debug(`Using platform gitAuthor: ${String(platformInfo.gitAuthor)}`);
|
|
|
|
gitAuthor = platformInfo.gitAuthor;
|
|
|
|
} else {
|
2020-07-22 18:11:22 +00:00
|
|
|
logger.debug(
|
|
|
|
'Using default gitAuthor: Renovate Bot <renovate@whitesourcesoftware.com>'
|
|
|
|
);
|
|
|
|
gitAuthor = 'Renovate Bot <renovate@whitesourcesoftware.com>';
|
2019-05-24 14:34:52 +00:00
|
|
|
}
|
2020-06-27 06:03:46 +00:00
|
|
|
const gitAuthorParsed = parseGitAuthor(gitAuthor);
|
2019-05-24 14:34:52 +00:00
|
|
|
// istanbul ignore if
|
|
|
|
if (!gitAuthorParsed) {
|
|
|
|
throw new Error('Init: gitAuthor is not parsed as valid RFC5322 format');
|
|
|
|
}
|
|
|
|
global.gitAuthor = {
|
|
|
|
name: gitAuthorParsed.name,
|
|
|
|
email: gitAuthorParsed.address,
|
|
|
|
};
|
2021-03-02 20:44:55 +00:00
|
|
|
|
|
|
|
const platformRule: HostRule = {
|
2019-05-21 11:20:09 +00:00
|
|
|
hostType: returnConfig.platform,
|
2019-05-30 14:54:24 +00:00
|
|
|
hostName: URL.parse(returnConfig.endpoint).hostname,
|
2019-05-20 08:59:30 +00:00
|
|
|
};
|
2020-04-12 16:09:36 +00:00
|
|
|
['token', 'username', 'password'].forEach((field) => {
|
2019-05-25 06:13:55 +00:00
|
|
|
if (config[field]) {
|
|
|
|
platformRule[field] = config[field];
|
2019-05-24 15:40:39 +00:00
|
|
|
delete returnConfig[field];
|
|
|
|
}
|
|
|
|
});
|
2019-05-21 06:18:19 +00:00
|
|
|
returnConfig.hostRules = returnConfig.hostRules || [];
|
|
|
|
returnConfig.hostRules.push(platformRule);
|
2019-05-24 15:40:39 +00:00
|
|
|
hostRules.add(platformRule);
|
2019-05-20 08:59:30 +00:00
|
|
|
return returnConfig;
|
2017-11-07 12:31:34 +00:00
|
|
|
}
|