renovate/lib/manager/bazel/update.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

const crypto = require('crypto');
const got = require('got');
module.exports = {
updateDependency,
};
async function updateDependency(fileContent, upgrade) {
try {
logger.debug(`bazel.updateDependency(): ${upgrade.newValue}`);
let newDef;
if (
upgrade.depType === 'git_repository' ||
upgrade.depType === 'go_repository'
) {
newDef = upgrade.def
2019-03-04 05:05:10 +00:00
.replace(/(tag\s*=\s*)"[^"]+"/, `$1"${upgrade.newValue}"`)
.replace(/(commit\s*=\s*)"[^"]+"/, `$1"${upgrade.newDigest}"`);
2018-11-18 21:27:20 +00:00
if (upgrade.currentDigest && upgrade.updateType !== 'digest') {
newDef = newDef.replace(
2019-03-04 05:05:10 +00:00
/(commit\s*=\s*)"[^"]+".*?\n/,
`$1"${upgrade.newDigest}", # ${upgrade.newValue}\n`
2018-11-18 21:27:20 +00:00
);
}
} else if (upgrade.depType === 'http_archive') {
const [, shortRepo] = upgrade.repo.split('/');
let newUrl;
let file;
try {
newUrl = `https://github.com/${upgrade.repo}/releases/download/${
upgrade.newValue
}/${shortRepo}-${upgrade.newValue}.tar.gz`;
file = (await got(newUrl, { encoding: null })).body;
} catch (err) {
logger.debug(
'Failed to download release download - trying archive instead'
);
newUrl = `https://github.com/${upgrade.repo}/archive/${
upgrade.newValue
}.tar.gz`;
file = (await got(newUrl, { encoding: null })).body;
}
const hash = crypto
.createHash('sha256')
.update(file)
.digest('hex');
const valuePattern = new RegExp(
upgrade.currentValue.replace(/\./g, '\\.'),
'g'
);
newDef = upgrade.def.replace(valuePattern, upgrade.newValue);
2019-03-04 05:05:10 +00:00
newDef = newDef.replace(/(sha256\s*=\s*)"[^"]+"/, `$1"${hash}"`);
}
logger.debug({ oldDef: upgrade.def, newDef });
const existingDef = new RegExp(
2019-03-04 05:05:10 +00:00
`${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${
upgrade.depName
}"[^\\)]+\\)\n`
);
return fileContent.replace(existingDef, newDef);
} catch (err) {
logger.info({ err }, 'Error setting new bazel WORKSPACE version');
return null;
}
}