From 97b0618b6a8953343a97036862df3ed977167d72 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 19 Aug 2019 14:55:16 -0600 Subject: Apply changes from https://gitlab.com/gitlab-org/security-products/license-management/merge_requests/50 --- lib/license/management.rb | 3 +++ lib/license/management/json_report.rb | 41 +++++++++++++++++++---------------- lib/license/management/repository.rb | 7 ++++-- 3 files changed, 30 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/license/management.rb b/lib/license/management.rb index 3b41843..2fdb6b5 100644 --- a/lib/license/management.rb +++ b/lib/license/management.rb @@ -11,6 +11,9 @@ require 'license/management/versions/v1' require 'license/management/json_report' require 'license/management/version' +# This applies a monkey patch to the JsonReport found in the `license_finder` gem. +LicenseFinder::JsonReport.prepend(License::Management::JsonReport) + module License module Management def self.root diff --git a/lib/license/management/json_report.rb b/lib/license/management/json_report.rb index 19a44c4..fde30ef 100644 --- a/lib/license/management/json_report.rb +++ b/lib/license/management/json_report.rb @@ -1,28 +1,31 @@ # frozen_string_literal: true -# This is a monkey patch of the JsonReport found in `license_finder` -module LicenseFinder - class JsonReport < CsvReport - DEFAULT_VERSION = '1' - VERSIONS = { - nil => ::License::Management::Versions::V1, - '' => ::License::Management::Versions::V1, - '1' => ::License::Management::Versions::V1, - '1.0' => ::License::Management::Versions::V1 - }.freeze +module License + module Management + module JsonReport + DEFAULT_VERSION = '1' + VERSIONS = { + nil => ::License::Management::Versions::V1, + '' => ::License::Management::Versions::V1, + '1' => ::License::Management::Versions::V1, + '1.0' => ::License::Management::Versions::V1 + }.freeze - def to_s - JSON.pretty_generate(version_for(report_version).to_h) + "\n" - end + # This method overrides the method defined in `License::Management::JsonReport` to + # allow us to generate a custom json report. + def to_s + JSON.pretty_generate(version_for(report_version).to_h) + "\n" + end - private + private - def report_version - ENV.fetch('LM_REPORT_VERSION', DEFAULT_VERSION) - end + def report_version + ENV.fetch('LM_REPORT_VERSION', DEFAULT_VERSION) + end - def version_for(version) - VERSIONS.fetch(version.to_s).new(dependencies) + def version_for(version) + VERSIONS.fetch(version.to_s).new(dependencies) + end end end end diff --git a/lib/license/management/repository.rb b/lib/license/management/repository.rb index 707d9f0..2eee1b6 100644 --- a/lib/license/management/repository.rb +++ b/lib/license/management/repository.rb @@ -7,14 +7,14 @@ module License include Verifiable def initialize( - compatibility_path: License::Management.root.join('licenses.yml') + compatibility_path: License::Management.root.join('normalized-licenses.yml') ) @compatibility_data = YAML.safe_load(IO.read(compatibility_path)) end def item_for(license) id = id_for(license) - item = id ? compatibility_data['items'][id] : nil + item = id ? compatibility_data['licenses'][id] : nil item ? { 'id' => id }.merge(item) : generate_item_for(license) end @@ -27,6 +27,9 @@ module License ids[license.send(:short_name)] || ids[license.url] end + # When `license_finder` is unable to determine the license it will use the full + # content of the file as the name of the license. This method shrinks that name + # down to just take the first line of the file. def take_first_line_from(content) return '' if blank?(content) -- cgit v1.2.3