renovate/lib/workers/branch/get-updated.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

const is = require('@sindresorhus/is');
const { get } = require('../../manager');
module.exports = {
getUpdatedPackageFiles,
};
async function getUpdatedPackageFiles(config) {
logger.debug('manager.getUpdatedPackageFiles()');
logger.trace({ config });
2018-07-18 18:18:40 +00:00
const updatedFileContents = {};
const packageFileManagers = {};
const packageFileUpdatedDeps = {};
for (const upgrade of config.upgrades) {
const { manager, packageFile, depName } = upgrade;
packageFileManagers[packageFile] = manager;
packageFileUpdatedDeps[packageFile] =
packageFileUpdatedDeps[packageFile] || [];
packageFileUpdatedDeps[packageFile].push(depName);
if (upgrade.updateType !== 'lockFileMaintenance') {
const existingContent =
2018-07-18 18:18:40 +00:00
updatedFileContents[packageFile] ||
(await platform.getFile(packageFile, config.parentBranch));
let newContent = existingContent;
const updateDependency = get(manager, 'updateDependency');
newContent = await updateDependency(existingContent, upgrade);
if (!newContent) {
if (config.parentBranch) {
logger.info('Rebasing branch after error updating content');
return getUpdatedPackageFiles({
...config,
parentBranch: undefined,
});
}
2018-10-17 04:04:39 +00:00
logger.debug(
{ existingContent, config: upgrade },
'Error updating file'
);
throw new Error('update-failure');
}
if (newContent !== existingContent) {
if (config.parentBranch) {
2019-01-06 13:47:42 +00:00
// This ensure it's always 1 commit from the bot
logger.info('Need to update package file so will rebase first');
return getUpdatedPackageFiles({
...config,
parentBranch: undefined,
});
}
logger.debug('Updating packageFile content');
2018-07-18 18:18:40 +00:00
updatedFileContents[packageFile] = newContent;
}
}
}
2018-07-18 18:18:40 +00:00
const updatedPackageFiles = Object.keys(updatedFileContents).map(name => ({
name,
contents: updatedFileContents[name],
}));
const updatedArtifacts = [];
const artifactErrors = [];
for (const packageFile of updatedPackageFiles) {
const manager = packageFileManagers[packageFile.name];
const updatedDeps = packageFileUpdatedDeps[packageFile.name];
2018-09-30 08:10:42 +00:00
const getArtifacts = get(manager, 'getArtifacts');
if (getArtifacts) {
const results = await getArtifacts(
packageFile.name,
updatedDeps,
packageFile.contents,
config
);
if (is.nonEmptyArray(results)) {
for (const res of results) {
const { file, artifactError } = res;
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
return {
parentBranch: config.parentBranch, // Need to overwrite original config
2018-07-18 18:18:40 +00:00
updatedPackageFiles,
updatedArtifacts,
artifactErrors,
};
}