Commit d031fdaf authored by mo khan's avatar mo khan Committed by Douglas Barbosa Alexandre
parent 4178123f
...@@ -87,41 +87,33 @@ export const parseLicenseReportMetrics = (headMetrics, baseMetrics, managedLicen ...@@ -87,41 +87,33 @@ export const parseLicenseReportMetrics = (headMetrics, baseMetrics, managedLicen
const baseLicenses = baseMetrics.licenses || []; const baseLicenses = baseMetrics.licenses || [];
const managedLicenseList = managedLicenses || []; const managedLicenseList = managedLicenses || [];
if (headLicenses.length > 0 && headDependencies.length > 0) { if (!headLicenses.length && !headDependencies.length) return [];
const report = [];
const knownLicenses = baseLicenses.map(license => license.name); const knownLicenses = baseLicenses.map(license => license.name.toLowerCase());
const identityMap = license => knownLicenses.includes(license.name.toLowerCase());
headLicenses.forEach(license => { const mapper = license => {
const { name, count } = license; const { name, count } = license;
const { id, approvalStatus } = getLicenseStatusByName(managedLicenseList, name);
if (!knownLicenses.includes(name)) { const dependencies = getDependenciesByLicenseName(headDependencies, name);
const { id, approvalStatus } = getLicenseStatusByName(managedLicenseList, name); const url =
(dependencies && dependencies[0] && dependencies[0].license && dependencies[0].license.url) ||
const dependencies = getDependenciesByLicenseName(headDependencies, name); '';
const url = return {
(dependencies && name,
dependencies[0] && count,
dependencies[0].license && url,
dependencies[0].license.url) || packages: dependencies.map(dependencyItem => dependencyItem.dependency),
''; status: getIssueStatusFromLicenseStatus(approvalStatus),
approvalStatus,
report.push({ id,
name, };
count, };
url,
packages: dependencies.map(dependencyItem => dependencyItem.dependency),
status: getIssueStatusFromLicenseStatus(approvalStatus),
approvalStatus,
id,
});
}
});
return report.sort(byLicenseNameComparator);
}
return []; return headLicenses
.filter(license => !identityMap(license))
.map(mapper)
.sort(byLicenseNameComparator);
}; };
export const getPackagesString = (packages, truncate, maxPackages) => { export const getPackagesString = (packages, truncate, maxPackages) => {
......
---
title: Perform case insensitive diff on license names
merge_request: 16335
author:
type: fixed
...@@ -28,6 +28,28 @@ module Gitlab ...@@ -28,6 +28,28 @@ module Gitlab
def violates?(software_license_policies) def violates?(software_license_policies)
software_license_policies.blacklisted.with_license_by_name(license_names).exists? software_license_policies.blacklisted.with_license_by_name(license_names).exists?
end end
def diff_with(other_report)
base = self.license_names.map { |name| canonicalize(name) }
head = other_report.license_names.map { |name| canonicalize(name) }
{
added: other_report.find_by_names(head - base),
unchanged: find_by_names(base & head),
removed: find_by_names(base - head)
}
end
def find_by_names(names)
names = names.map { |name| canonicalize(name) }
licenses.select { |license| names.include?(canonicalize(license.name)) }
end
private
def canonicalize(name)
name.downcase
end
end end
end end
end end
......
...@@ -15,23 +15,22 @@ module Gitlab ...@@ -15,23 +15,22 @@ module Gitlab
end end
def new_licenses def new_licenses
strong_memoize(:new_licenses) do diff[:added]
names = @head_report.license_names - @base_report.license_names
@head_report.licenses.select { |license| names.include?(license.name) }
end
end end
def existing_licenses def existing_licenses
strong_memoize(:existing_licenses) do diff[:unchanged]
names = @base_report.license_names & @head_report.license_names
@head_report.licenses.select { |license| names.include?(license.name) }
end
end end
def removed_licenses def removed_licenses
strong_memoize(:removed_licenses) do diff[:removed]
names = @base_report.license_names - @head_report.license_names end
@base_report.licenses.select { |license| names.include?(license.name) }
private
def diff
strong_memoize(:diff) do
base_report.diff_with(head_report)
end end
end end
end end
......
...@@ -61,6 +61,14 @@ describe('utils', () => { ...@@ -61,6 +61,14 @@ describe('utils', () => {
expect(result[1].approvalStatus).toBe(blacklistedLicense.approvalStatus); expect(result[1].approvalStatus).toBe(blacklistedLicense.approvalStatus);
expect(result[1].id).toBe(blacklistedLicense.id); expect(result[1].id).toBe(blacklistedLicense.id);
}); });
it('matches using a case insensitive match on license name', () => {
const headReport = { licenses: [{ count: 1, name: 'BSD' }], dependencies: [] };
const baseReport = { licenses: [{ count: 1, name: 'bsd' }], dependencies: [] };
const result = parseLicenseReportMetrics(headReport, baseReport, []);
expect(result.length).toBe(0);
});
}); });
describe('byLicenseNameComparator', () => { describe('byLicenseNameComparator', () => {
......
...@@ -41,4 +41,23 @@ describe Gitlab::Ci::Reports::LicenseManagement::Report do ...@@ -41,4 +41,23 @@ describe Gitlab::Ci::Reports::LicenseManagement::Report do
specify { expect(subject.violates?(project.software_license_policies)).to be(false) } specify { expect(subject.violates?(project.software_license_policies)).to be(false) }
end end
end end
describe "#diff_with" do
let(:report_1) { build(:ci_reports_license_management_report, :report_1) }
let(:report_2) { build(:ci_reports_license_management_report, :report_2) }
subject { report_1.diff_with(report_2) }
before do
report_1.add_dependency('BSD', 1, 'https://opensource.org/licenses/0BSD', 'Library1')
report_2.add_dependency('bsd', 1, 'https://opensource.org/licenses/0BSD', 'Library1')
end
def names_from(licenses)
licenses.map(&:name)
end
it { expect(names_from(subject[:added])).to contain_exactly('Apache 2.0') }
it { expect(names_from(subject[:unchanged])).to contain_exactly('MIT', 'BSD') }
it { expect(names_from(subject[:removed])).to contain_exactly('WTFPL') }
end
end end
...@@ -7,30 +7,30 @@ describe Gitlab::Ci::Reports::LicenseManagement::ReportsComparer do ...@@ -7,30 +7,30 @@ describe Gitlab::Ci::Reports::LicenseManagement::ReportsComparer do
let(:report_2) { build :ci_reports_license_management_report, :report_2 } let(:report_2) { build :ci_reports_license_management_report, :report_2 }
let(:report_comparer) { described_class.new(report_1, report_2) } let(:report_comparer) { described_class.new(report_1, report_2) }
before do
report_1.add_dependency('BSD', 1, 'https://opensource.org/licenses/0BSD', 'Library1')
report_2.add_dependency('bsd', 1, 'https://opensource.org/licenses/0BSD', 'Library1')
end
def names_from(licenses)
licenses.map(&:name)
end
describe '#new_licenses' do describe '#new_licenses' do
subject { report_comparer.new_licenses } subject { report_comparer.new_licenses }
it 'reports new licenses' do it { expect(names_from(subject)).to contain_exactly('Apache 2.0') }
expect(subject.count).to eq 1
expect(subject[0].name).to eq 'Apache 2.0'
end
end end
describe '#existing_licenses' do describe '#existing_licenses' do
subject { report_comparer.existing_licenses } subject { report_comparer.existing_licenses }
it 'reports existing licenses' do it { expect(names_from(subject)).to contain_exactly('MIT', 'BSD') }
expect(subject.count).to eq 1
expect(subject[0].name).to eq 'MIT'
end
end end
describe '#removed_licenses' do describe '#removed_licenses' do
subject { report_comparer.removed_licenses } subject { report_comparer.removed_licenses }
it 'reports removed licenses' do it { expect(names_from(subject)).to contain_exactly('WTFPL') }
expect(subject.count).to eq 1
expect(subject[0].name).to eq 'WTFPL'
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment