2018-06-01 13:38:20 +00:00
|
|
|
const parse = require('github-url-from-git');
|
2018-10-29 16:18:07 +00:00
|
|
|
const URL = require('url');
|
2018-06-01 13:38:20 +00:00
|
|
|
|
2017-12-07 08:22:10 +00:00
|
|
|
module.exports = {
|
|
|
|
extractDependencies,
|
|
|
|
};
|
|
|
|
|
2018-10-29 16:18:07 +00:00
|
|
|
function parseUrl(urlString) {
|
|
|
|
// istanbul ignore if
|
|
|
|
if (!urlString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const url = URL.parse(urlString);
|
|
|
|
if (url.host !== 'github.com') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const path = url.path.split('/').slice(1);
|
|
|
|
const repo = path[0] + '/' + path[1];
|
|
|
|
let currentValue = null;
|
|
|
|
if (path[2] === 'releases' && path[3] === 'download') {
|
|
|
|
currentValue = path[4];
|
|
|
|
}
|
|
|
|
if (path[2] === 'archive') {
|
|
|
|
currentValue = path[3].replace(/\.tar\.gz$/, '');
|
|
|
|
}
|
|
|
|
if (currentValue) {
|
|
|
|
return { repo, currentValue };
|
|
|
|
}
|
|
|
|
// istanbul ignore next
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-03 16:09:18 +00:00
|
|
|
function extractDependencies(content) {
|
|
|
|
const definitions = content.match(
|
2017-12-14 19:05:45 +00:00
|
|
|
/(git_repository|http_archive)\(([\s\S]*?)\n\)\n?/g
|
|
|
|
);
|
|
|
|
if (!definitions) {
|
|
|
|
logger.debug('No matching WORKSPACE definitions found');
|
2018-05-03 16:09:18 +00:00
|
|
|
return null;
|
2017-12-07 08:22:10 +00:00
|
|
|
}
|
2017-12-14 19:05:45 +00:00
|
|
|
logger.debug({ definitions }, `Found ${definitions.length} definitions`);
|
|
|
|
const deps = [];
|
|
|
|
definitions.forEach(def => {
|
|
|
|
logger.debug({ def }, 'Checking bazel definition');
|
2018-10-29 16:18:07 +00:00
|
|
|
|
2018-06-03 17:13:39 +00:00
|
|
|
const dep = { def, versionScheme: 'semver' };
|
2017-12-14 19:05:45 +00:00
|
|
|
let depName;
|
|
|
|
let remote;
|
2018-06-04 03:48:20 +00:00
|
|
|
let currentValue;
|
2017-12-14 19:05:45 +00:00
|
|
|
let url;
|
|
|
|
let sha256;
|
|
|
|
let match = def.match(/name = "([^"]+)"/);
|
|
|
|
if (match) {
|
|
|
|
[, depName] = match;
|
|
|
|
}
|
|
|
|
match = def.match(/remote = "([^"]+)"/);
|
|
|
|
if (match) {
|
|
|
|
[, remote] = match;
|
|
|
|
}
|
|
|
|
match = def.match(/tag = "([^"]+)"/);
|
|
|
|
if (match) {
|
2018-06-04 03:48:20 +00:00
|
|
|
[, currentValue] = match;
|
2017-12-14 19:05:45 +00:00
|
|
|
}
|
|
|
|
match = def.match(/url = "([^"]+)"/);
|
|
|
|
if (match) {
|
|
|
|
[, url] = match;
|
|
|
|
}
|
2018-10-29 16:18:07 +00:00
|
|
|
match = def.match(/urls = \[\s*"([^\]]+)",?\s*\]/);
|
|
|
|
if (match) {
|
|
|
|
const urls = match[1].replace(/\s/g, '').split('","');
|
|
|
|
url = urls.find(parseUrl);
|
|
|
|
}
|
2017-12-14 19:05:45 +00:00
|
|
|
match = def.match(/sha256 = "([^"]+)"/);
|
|
|
|
if (match) {
|
|
|
|
[, sha256] = match;
|
|
|
|
}
|
2018-09-20 10:13:18 +00:00
|
|
|
logger.debug({ dependency: depName, remote, currentValue });
|
2018-06-04 03:48:20 +00:00
|
|
|
if (def.startsWith('git_repository') && depName && remote && currentValue) {
|
2017-12-14 19:05:45 +00:00
|
|
|
dep.depType = 'git_repository';
|
|
|
|
dep.depName = depName;
|
|
|
|
dep.remote = remote;
|
2018-06-04 03:48:20 +00:00
|
|
|
dep.currentValue = currentValue;
|
2018-06-01 13:38:20 +00:00
|
|
|
const repo = parse(remote).substring('https://github.com/'.length);
|
|
|
|
dep.purl = 'pkg:github/' + repo;
|
2017-12-14 19:05:45 +00:00
|
|
|
deps.push(dep);
|
|
|
|
} else if (
|
|
|
|
def.startsWith('http_archive') &&
|
|
|
|
depName &&
|
2018-10-29 16:18:07 +00:00
|
|
|
parseUrl(url) &&
|
|
|
|
sha256
|
2017-12-14 19:05:45 +00:00
|
|
|
) {
|
2018-10-29 16:18:07 +00:00
|
|
|
const parsedUrl = parseUrl(url);
|
2017-12-14 19:05:45 +00:00
|
|
|
dep.depType = 'http_archive';
|
|
|
|
dep.depName = depName;
|
2018-10-29 16:18:07 +00:00
|
|
|
dep.repo = parsedUrl.repo;
|
|
|
|
dep.currentValue = parsedUrl.currentValue;
|
2018-06-04 04:34:48 +00:00
|
|
|
dep.purl = 'pkg:github/' + dep.repo + '?ref=release';
|
2017-12-14 19:05:45 +00:00
|
|
|
deps.push(dep);
|
|
|
|
} else {
|
|
|
|
logger.info(
|
|
|
|
{ def },
|
|
|
|
'Failed to find dependency in bazel WORKSPACE definition'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2018-05-03 16:09:18 +00:00
|
|
|
if (!deps.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return { deps };
|
2017-12-07 08:22:10 +00:00
|
|
|
}
|