refactor: simplify global init (#6452)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Rhys Arkins 2020-06-22 17:00:46 +02:00 committed by GitHub
parent 3ba1c93960
commit f77b7e8781
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 40 deletions

View file

@ -1,41 +1,18 @@
import os from 'os';
import path from 'path'; import path from 'path';
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import fs from 'fs-extra'; import fs from 'fs-extra';
import * as configParser from '../../config'; import * as configParser from '../../config';
import { getErrors, logger, setMeta } from '../../logger'; import { getErrors, logger, setMeta } from '../../logger';
import { initPlatform } from '../../platform';
import { setUtilConfig } from '../../util'; import { setUtilConfig } from '../../util';
import * as globalCache from '../../util/cache/global';
import { setEmojiConfig } from '../../util/emoji';
import * as hostRules from '../../util/host-rules'; import * as hostRules from '../../util/host-rules';
import * as repositoryWorker from '../repository'; import * as repositoryWorker from '../repository';
import { autodiscoverRepositories } from './autodiscover'; import { autodiscoverRepositories } from './autodiscover';
import { globalFinalize, globalInitialize } from './initialize';
import * as limits from './limits'; import * as limits from './limits';
type RenovateConfig = configParser.RenovateConfig; type RenovateConfig = configParser.RenovateConfig;
type RenovateRepository = configParser.RenovateRepository; type RenovateRepository = configParser.RenovateRepository;
async function setDirectories(input: RenovateConfig): Promise<RenovateConfig> {
const config: RenovateConfig = { ...input };
process.env.TMPDIR = process.env.RENOVATE_TMPDIR || os.tmpdir();
if (config.baseDir) {
logger.debug('Using configured baseDir: ' + config.baseDir);
} else {
config.baseDir = path.join(process.env.TMPDIR, 'renovate');
logger.debug('Using baseDir: ' + config.baseDir);
}
await fs.ensureDir(config.baseDir);
if (config.cacheDir) {
logger.debug('Using configured cacheDir: ' + config.cacheDir);
} else {
config.cacheDir = path.join(config.baseDir, 'cache');
logger.debug('Using cacheDir: ' + config.cacheDir);
}
await fs.ensureDir(config.cacheDir);
return config;
}
export async function getRepositoryConfig( export async function getRepositoryConfig(
globalConfig: RenovateConfig, globalConfig: RenovateConfig,
repository: RenovateRepository repository: RenovateRepository
@ -57,22 +34,26 @@ function getGlobalConfig(): Promise<RenovateConfig> {
return configParser.parseConfigs(process.env, process.argv); return configParser.parseConfigs(process.env, process.argv);
} }
export async function start(): Promise<0 | 1> { function haveReachedLimits(): boolean {
try { if (limits.getLimitRemaining('prCommitsPerRunLimit') <= 0) {
let config = await getGlobalConfig(); logger.info('Max commits created for this run.');
config = await initPlatform(config); return true;
config = await setDirectories(config); }
globalCache.init(config); return false;
config = await autodiscoverRepositories(config); }
limits.init(config); export async function start(): Promise<0 | 1> {
setEmojiConfig(config); let config: RenovateConfig;
try {
// read global config from file, env and cli args
config = await getGlobalConfig();
// initialize all submodules
config = await globalInitialize(config);
// autodiscover repositories (needs to come after platform initialization)
config = await autodiscoverRepositories(config);
// Iterate through repositories sequentially // Iterate through repositories sequentially
for (const repository of config.repositories) { for (const repository of config.repositories) {
if (limits.getLimitRemaining('prCommitsPerRunLimit') <= 0) { if (haveReachedLimits()) {
logger.debug(
'Max commits created for this run. Skipping all remaining repositories.'
);
break; break;
} }
const repoConfig = await getRepositoryConfig(config, repository); const repoConfig = await getRepositoryConfig(config, repository);
@ -83,16 +64,17 @@ export async function start(): Promise<0 | 1> {
repoConfig.hostRules = []; repoConfig.hostRules = [];
} }
await repositoryWorker.renovateRepository(repoConfig); await repositoryWorker.renovateRepository(repoConfig);
}
setMeta({}); setMeta({});
globalCache.cleanup(config); }
logger.debug(`Renovate exiting successfully`);
} catch (err) /* istanbul ignore next */ { } catch (err) /* istanbul ignore next */ {
if (err.message.startsWith('Init: ')) { if (err.message.startsWith('Init: ')) {
logger.fatal(err.message.substring(6)); logger.fatal(err.message.substring(6));
} else { } else {
logger.fatal({ err }, `Fatal error: ${err.message}`); logger.fatal({ err }, `Fatal error: ${err.message}`);
} }
} finally {
globalFinalize(config);
logger.debug(`Renovate exiting`);
} }
const loggerErrors = getErrors(); const loggerErrors = getErrors();
/* istanbul ignore if */ /* istanbul ignore if */

View file

@ -0,0 +1,45 @@
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import { RenovateConfig } from '../../config/common';
import { logger } from '../../logger';
import { initPlatform } from '../../platform';
import * as globalCache from '../../util/cache/global';
import { setEmojiConfig } from '../../util/emoji';
import * as limits from './limits';
async function setDirectories(input: RenovateConfig): Promise<RenovateConfig> {
const config: RenovateConfig = { ...input };
process.env.TMPDIR = process.env.RENOVATE_TMPDIR || os.tmpdir();
if (config.baseDir) {
logger.debug('Using configured baseDir: ' + config.baseDir);
} else {
config.baseDir = path.join(process.env.TMPDIR, 'renovate');
logger.debug('Using baseDir: ' + config.baseDir);
}
await fs.ensureDir(config.baseDir);
if (config.cacheDir) {
logger.debug('Using configured cacheDir: ' + config.cacheDir);
} else {
config.cacheDir = path.join(config.baseDir, 'cache');
logger.debug('Using cacheDir: ' + config.cacheDir);
}
await fs.ensureDir(config.cacheDir);
return config;
}
export async function globalInitialize(
config_: RenovateConfig
): Promise<RenovateConfig> {
let config = config_;
config = await initPlatform(config);
config = await setDirectories(config);
globalCache.init(config);
limits.init(config);
setEmojiConfig(config);
return config;
}
export function globalFinalize(config: RenovateConfig): void {
globalCache.cleanup(config);
}