diff options
| author | Olivier Gonzalez <ogonzalez@gitlab.com> | 2018-06-06 15:21:06 +0000 |
|---|---|---|
| committer | Olivier Gonzalez <ogonzalez@gitlab.com> | 2018-06-06 15:21:06 +0000 |
| commit | 09bb36aa761c60e081418da232bf440dbf0341c7 (patch) | |
| tree | b950fc5b6e4f636208d56280dc03fa5b283ed8b4 /html2json.js | |
| parent | d706bac7750c57bf9092c9d2c9ef177ff5b8503f (diff) | |
| parent | 4d9048a99a9824ab7cb719771699a92c582b5bf2 (diff) | |
Merge branch 'first_version' into 'master'
First version
See merge request gitlab-org/security-products/license-management!2
Diffstat (limited to 'html2json.js')
| -rw-r--r-- | html2json.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/html2json.js b/html2json.js new file mode 100644 index 0000000..841149a --- /dev/null +++ b/html2json.js @@ -0,0 +1,80 @@ +var cheerio = require('cheerio') +var path = require('path') + +// Read the HTML report +fs = require('fs') +var htmlContent; +try { + htmlContent = fs.readFileSync(process.argv[2], 'utf8'); +} catch(e) { + console.log('Error:', e.stack); +} + +// Get the directory containing the results to make pathes relative to it later. +report_directory = path.dirname(process.argv[2]) + +const $ = cheerio.load(htmlContent) + +// Extract licenses and the number of occurences. +var licenses = []; +$('div.summary div.row').children().first().find('ul li').each(function(i, doc) { + tmp = $(this).text(); + matches = tmp.match(/^([0-9]+) ((\s|\S)*)/m) + licenses.push({ + count: parseInt(matches[1], 10), + name: matches[2] + }) +}) + +// Extract dependencies info. +var dependencies = [] +$('div.dependencies div').each(function(i, doc) { + // Get license name. + license = $(this).find('blockquote p').text().trim(); + license = license.split("\n")[0]; + + // Get URL. + license_url = $(this).find('blockquote p a[href]').attr('href'); + + // Get dependency name. + dependency_name = $(this).find('h2').text().trim(); + dependency_name = dependency_name.split("\n")[0]; + + // Get dependency URL. + dependency_url = $(this).find('h2 a[href]').attr('href'); + + // Get dependency description. + dependency_description = $(this).find('dl').first().next().text().trim(); + + // Get dependency location relative to the project root path + dependency_pathes = [] + $(this).find('dl').first().find('dd').each(function(i, doc) { + dependency_path = path.relative(report_directory, $(this).text().trim()); + + // Whitespace path means current directory + if (!dependency_path) { + dependency_path = "."; + } + + dependency_pathes.push(dependency_path); + }) + dependencies.push({ + license: { + name: license, + url: license_url + }, + dependency: { + name: dependency_name, + url: dependency_url, + description: dependency_description, + pathes: dependency_pathes + } + }) +}) + +console.log(JSON.stringify({ + licenses: licenses, + dependencies: dependencies}, null, 4)) + + + |
