2017-12-14 19:05:45 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const got = require('got');
|
|
|
|
|
2017-12-07 08:22:10 +00:00
|
|
|
module.exports = {
|
2018-03-30 04:05:00 +00:00
|
|
|
updateDependency,
|
2017-12-07 08:22:10 +00:00
|
|
|
};
|
|
|
|
|
2018-03-30 04:05:00 +00:00
|
|
|
async function updateDependency(fileContent, upgrade) {
|
2017-12-07 08:22:10 +00:00
|
|
|
try {
|
2018-06-04 03:48:20 +00:00
|
|
|
logger.debug(`bazel.updateDependency(): ${upgrade.newValue}`);
|
2017-12-14 19:05:45 +00:00
|
|
|
let newDef;
|
2018-11-10 20:50:17 +00:00
|
|
|
if (
|
|
|
|
upgrade.depType === 'git_repository' ||
|
|
|
|
upgrade.depType === 'go_repository'
|
|
|
|
) {
|
2018-11-14 12:11:35 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
2017-12-14 19:05:45 +00:00
|
|
|
} else if (upgrade.depType === 'http_archive') {
|
|
|
|
const [, shortRepo] = upgrade.repo.split('/');
|
2019-01-26 05:41:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-12-14 19:05:45 +00:00
|
|
|
const hash = crypto
|
|
|
|
.createHash('sha256')
|
|
|
|
.update(file)
|
|
|
|
.digest('hex');
|
2018-10-29 16:18:07 +00:00
|
|
|
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}"`);
|
2017-12-14 19:05:45 +00:00
|
|
|
}
|
2017-12-07 08:22:10 +00:00
|
|
|
logger.debug({ oldDef: upgrade.def, newDef });
|
2017-12-14 19:05:45 +00:00
|
|
|
const existingDef = new RegExp(
|
2019-03-04 05:05:10 +00:00
|
|
|
`${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${
|
|
|
|
upgrade.depName
|
|
|
|
}"[^\\)]+\\)\n`
|
2017-12-14 19:05:45 +00:00
|
|
|
);
|
2018-03-30 04:05:00 +00:00
|
|
|
return fileContent.replace(existingDef, newDef);
|
2017-12-07 08:22:10 +00:00
|
|
|
} catch (err) {
|
|
|
|
logger.info({ err }, 'Error setting new bazel WORKSPACE version');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|