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'
|
|
|
|
) {
|
2017-12-14 19:05:45 +00:00
|
|
|
newDef = upgrade.def.replace(
|
|
|
|
/tag = "[^"]+"/,
|
2018-06-04 03:48:20 +00:00
|
|
|
`tag = "${upgrade.newValue}"`
|
2017-12-14 19:05:45 +00:00
|
|
|
);
|
|
|
|
} else if (upgrade.depType === 'http_archive') {
|
|
|
|
const [, shortRepo] = upgrade.repo.split('/');
|
|
|
|
const newUrl = `https://github.com/${upgrade.repo}/releases/download/${
|
2018-06-04 03:48:20 +00:00
|
|
|
upgrade.newValue
|
|
|
|
}/${shortRepo}-${upgrade.newValue}.tar.gz`;
|
2017-12-14 19:05:45 +00:00
|
|
|
const file = (await got(newUrl, { encoding: null })).body;
|
|
|
|
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);
|
2017-12-14 19:05:45 +00:00
|
|
|
newDef = newDef.replace(/sha256 = "[^"]+"/, `sha256 = "${hash}"`);
|
|
|
|
}
|
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(
|
2017-12-14 19:31:21 +00:00
|
|
|
`${upgrade.depType}\\([^\\)]+name = "${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;
|
|
|
|
}
|
|
|
|
}
|