diff options
| author | mo khan <mo.khan@gmail.com> | 2019-10-22 16:24:18 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2019-10-23 11:07:42 -0600 |
| commit | 9038552abe7c4b0ebca9795121f9d48564492b3c (patch) | |
| tree | 65c855c8803b8ccc13446ddf47f2cd444ffca5be | |
| parent | ef5e5ec0b635d3968a6699b8d6711dd75f568b7d (diff) | |
Parse spdx id from known sources
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | lib/license/management/repository.rb | 20 | ||||
| -rw-r--r-- | normalized-licenses.yml | 5 | ||||
| -rw-r--r-- | spec/license/management/repository_spec.rb | 27 |
4 files changed, 46 insertions, 8 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 9f6acfe..4459fda 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - license-management (1.7.0) + license-management (1.7.3) license_finder (= 5.6.2) GEM diff --git a/lib/license/management/repository.rb b/lib/license/management/repository.rb index 0c428dd..fdff30d 100644 --- a/lib/license/management/repository.rb +++ b/lib/license/management/repository.rb @@ -25,6 +25,8 @@ module License attr_reader :spdx_data, :compatibility_data def spdx_data_for(id) + return if blank?(id) + data = spdx_data[id] if data { @@ -40,7 +42,7 @@ module License def id_for(license) ids = compatibility_data['ids'] - ids[license.send(:short_name)] || ids[license.url] + ids[license.send(:short_name)] || ids[license.url] || known_sources(license.url) end # When `license_finder` is unable to determine the license it will use the full @@ -54,9 +56,10 @@ module License def generate_item_for(license) log_info("detected unknown license named `#{license.send(:short_name)}`") + name = take_first_line_from(license.name) { - 'id' => 'unknown', - 'name' => take_first_line_from(license.name), + 'id' => name.downcase, + 'name' => name, 'url' => present?(license.url) ? license.url : '' } end @@ -71,6 +74,17 @@ module License memo end end + + def known_sources(url) + return unless url =~ /\A#{::URI::DEFAULT_PARSER.make_regexp(['http', 'https'])}\z/ + + uri = URI.parse(url) + return unless ['opensource.org', 'licenses.nuget.org'].include?(uri.host) + + uri.path.split('/')[-1] + rescue + nil + end end end end diff --git a/normalized-licenses.yml b/normalized-licenses.yml index 8b7640f..3fbef8d 100644 --- a/normalized-licenses.yml +++ b/normalized-licenses.yml @@ -7,14 +7,11 @@ ids: BSD: BSD-4-Clause CC0 1.0 Universal: CC0-1.0 CC01: CC0-1.0 + Common Public License Version 1.0: CPL-1.0 EPL1: EPL-1.0 GPLv2: GPL-2.0 GPLv3: GPL-3.0 - http://opensource.org/licenses/Apache-2.0: Apache-2.0 - http://opensource.org/licenses/LGPL-2.1: LGPL-2.1 - https://licenses.nuget.org/MIT: MIT http://www.apache.org/licenses/LICENSE-2.0: Apache-2.0 - http://www.opensource.org/licenses/mit-license.php: MIT ISC: ISC LGPL 2.1: LGPL-2.1 LGPL2_1: LGPL-2.1 diff --git a/spec/license/management/repository_spec.rb b/spec/license/management/repository_spec.rb new file mode 100644 index 0000000..dbc0a22 --- /dev/null +++ b/spec/license/management/repository_spec.rb @@ -0,0 +1,27 @@ +RSpec.describe License::Management::Repository do + describe "#item_for" do + let(:spdx_licenses) { JSON.parse(IO.read('spdx-licenses.json'))['licenses'] } + + context "when mapping a license that refers to opensource.org" do + it 'parses the SPDX id from the url' do + spdx_licenses.each do |license| + spdx_id = license['licenseId'] + url = "https://opensource.org/licenses/#{spdx_id}" + license = LicenseFinder::License.new(short_name: url, matcher: LicenseFinder::License::NoneMatcher.new, url: url) + expect(subject.item_for(license)['id']).to eql(spdx_id) + end + end + end + + context "when mapping a license that refers to nuget.org" do + it 'parses the SPDX id from the url' do + spdx_licenses.each do |license| + spdx_id = license['licenseId'] + url = "https://licenses.nuget.org/#{spdx_id}" + license = LicenseFinder::License.new(short_name: url, matcher: LicenseFinder::License::NoneMatcher.new, url: url) + expect(subject.item_for(license)['id']).to eql(spdx_id) + end + end + end + end +end |
