2017-07-07 04:25:38 +00:00
|
|
|
const schedule = require('./schedule');
|
2017-08-26 14:10:18 +00:00
|
|
|
const { getUpdatedPackageFiles } = require('./package-files');
|
|
|
|
const { getUpdatedLockFiles } = require('./lock-files');
|
|
|
|
const { commitFilesToBranch } = require('./commit');
|
|
|
|
const { getParentBranch } = require('./parent');
|
|
|
|
const { tryBranchAutomerge } = require('./automerge');
|
|
|
|
const { setUnpublishable } = require('./status-checks');
|
|
|
|
const { prAlreadyExisted } = require('./check-existing');
|
2017-06-29 05:29:41 +00:00
|
|
|
const prWorker = require('../pr');
|
2017-08-26 14:10:18 +00:00
|
|
|
|
|
|
|
const { isScheduledNow } = schedule;
|
2017-02-14 07:08:40 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2017-08-26 14:10:18 +00:00
|
|
|
processBranch,
|
2017-02-14 07:08:40 +00:00
|
|
|
};
|
|
|
|
|
2017-08-26 14:10:18 +00:00
|
|
|
async function processBranch(branchConfig) {
|
|
|
|
const config = { ...branchConfig };
|
|
|
|
const dependencies = config.upgrades
|
|
|
|
.map(upgrade => upgrade.depName)
|
|
|
|
.filter(v => v); // remove nulls (happens for lock file maintenance)
|
|
|
|
const logger = config.logger.child({
|
|
|
|
repository: config.repository,
|
|
|
|
branch: config.branchName,
|
|
|
|
dependencies,
|
|
|
|
});
|
|
|
|
config.logger = logger;
|
|
|
|
logger.trace({ config }, 'processBranch');
|
|
|
|
try {
|
|
|
|
// Check schedule
|
|
|
|
if (!isScheduledNow(config)) {
|
|
|
|
logger.info('Skipping branch as it is not scheduled');
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'not-scheduled';
|
2017-02-14 07:08:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 14:10:18 +00:00
|
|
|
logger.info(`Branch has ${dependencies.length} upgrade(s)`);
|
2017-06-29 17:50:26 +00:00
|
|
|
|
2017-08-26 14:10:18 +00:00
|
|
|
if (await prAlreadyExisted(config)) {
|
|
|
|
logger.info('Closed PR already exists. Skipping branch.');
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'already-existed';
|
2017-08-06 13:38:10 +00:00
|
|
|
}
|
2017-08-26 14:10:18 +00:00
|
|
|
config.parentBranch = await getParentBranch(config);
|
|
|
|
logger.debug(`Using parentBranch: ${config.parentBranch}`);
|
|
|
|
config.updatedPackageFiles = await getUpdatedPackageFiles(config);
|
|
|
|
if (config.updatedPackageFiles.length) {
|
|
|
|
logger.debug(
|
|
|
|
{ updatedPackageFiles: config.updatedPackageFiles },
|
|
|
|
`Updated ${config.updatedPackageFiles.length} package files`
|
2017-04-21 08:12:41 +00:00
|
|
|
);
|
2017-08-26 14:10:18 +00:00
|
|
|
} else {
|
|
|
|
logger.debug('No package files need updating');
|
2017-04-15 18:32:01 +00:00
|
|
|
}
|
2017-08-26 14:10:18 +00:00
|
|
|
Object.assign(config, await getUpdatedLockFiles(config));
|
|
|
|
if (config.lockFileError) {
|
|
|
|
throw new Error('lockFileError');
|
2017-04-15 18:32:01 +00:00
|
|
|
}
|
2017-08-26 14:10:18 +00:00
|
|
|
if (config.updatedLockFiles.length) {
|
|
|
|
logger.debug(
|
|
|
|
{ updatedLockFiles: config.updatedLockFiles },
|
|
|
|
`Updated ${config.updatedLockFiles.length} lock files`
|
2017-08-08 21:03:52 +00:00
|
|
|
);
|
2017-08-26 14:10:18 +00:00
|
|
|
} else {
|
|
|
|
logger.debug('No updated lock files in branch');
|
2017-08-08 21:03:52 +00:00
|
|
|
}
|
2017-08-26 14:10:18 +00:00
|
|
|
await commitFilesToBranch(config);
|
|
|
|
|
|
|
|
// Return now if no branch exists
|
|
|
|
if ((await config.api.branchExists(config.branchName)) === false) {
|
|
|
|
logger.debug('Branch does not exist - returning');
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'no-branch';
|
2017-06-08 04:18:21 +00:00
|
|
|
}
|
2017-06-22 07:03:36 +00:00
|
|
|
|
2017-08-26 14:10:18 +00:00
|
|
|
// Set branch statuses
|
|
|
|
await setUnpublishable(config);
|
|
|
|
|
|
|
|
// Try to automerge branch and finish if successful
|
|
|
|
logger.debug('Checking if we should automerge the branch');
|
|
|
|
const branchMerged = await tryBranchAutomerge(config);
|
|
|
|
if (branchMerged) {
|
|
|
|
logger.debug('Branch is automerged - returning');
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'automerged';
|
2017-08-26 14:10:18 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2017-08-26 14:47:21 +00:00
|
|
|
if (err.message !== 'lockFileError') {
|
|
|
|
logger.error({ err }, `Error updating branch: ${err.message}`);
|
|
|
|
} else {
|
|
|
|
logger.info('Error updating branch');
|
|
|
|
}
|
|
|
|
// Don't throw here - we don't want to stop the other renovations
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'error';
|
2017-06-29 17:50:26 +00:00
|
|
|
}
|
2017-06-22 07:03:36 +00:00
|
|
|
try {
|
2017-08-26 14:10:18 +00:00
|
|
|
logger.debug('Ensuring PR');
|
2017-08-27 11:55:41 +00:00
|
|
|
logger.debug(
|
|
|
|
`There are ${config.errors.length} errors and ${config.warnings
|
|
|
|
.length} warnings`
|
|
|
|
);
|
2017-08-26 14:10:18 +00:00
|
|
|
const pr = await prWorker.ensurePr(config);
|
|
|
|
// TODO: ensurePr should check for automerge itself
|
|
|
|
if (pr) {
|
2017-08-28 13:50:11 +00:00
|
|
|
const prAutomerged = await prWorker.checkAutoMerge(pr, config);
|
|
|
|
if (prAutomerged) {
|
|
|
|
return 'automerged';
|
|
|
|
}
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|
2017-06-28 20:33:27 +00:00
|
|
|
} catch (err) {
|
2017-08-26 14:10:18 +00:00
|
|
|
logger.error({ err }, `Error ensuring PR: ${err.message}`);
|
2017-08-28 13:50:11 +00:00
|
|
|
// Don't throw here - we don't want to stop the other renovations
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|
2017-08-28 13:50:11 +00:00
|
|
|
return 'done';
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|