Skip to content

Commit

Permalink
feat: support editing default license categories
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo authored and andreimarinescu committed Apr 13, 2023
1 parent 68b7791 commit 0f473cc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {getDependencyVulnerabilities} = require('./issues/vulnerabilities');
const {getLicenseIssues, getLicenseUsage} = require('./issues/license');
const {getLicenseIssues, getLicenseUsage, getLicenseCategories} = require('./issues/license');
const {buildTree, buildTreemap} = require('./charts');
const {excludeResolved, validateResolvedIssues, allIssuesFromReport} = require('./issues/utils');
const csv = require('./charts/csv');
Expand Down Expand Up @@ -95,9 +95,11 @@ const getReport = async ({
// Get license info and issues
onProgress({type: 'start', stage: 'licenses'});
try {
const {defaultCategories, userCategories} = getLicenseCategories(licensePolicy);
licenseUsage = await getLicenseUsage({
dependencies: includeDev ? dGraph.all : dGraph.prodDependencies,
licensePolicy,
defaultCategories,
userCategories,
});
licenseIssues = await getLicenseIssues({licenseUsage, packageGraph, licensePolicy});
} catch (error) {
Expand Down
77 changes: 56 additions & 21 deletions src/issues/license.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const licenseCatalog = require('./licenses.json');
const {getFindings, makeSandwormIssueId} = require('./utils');

const LICENSE_TYPES = [
const DEFAULT_LICENSE_CATEGORIES = [
'Public Domain',
'Permissive',
'Weakly Protective',
Expand All @@ -24,7 +24,31 @@ const isSpdxExpression = (license) => {
return !!(license.match(/ or /i) || license.match(/ and /i) || license.match(/ with /i));
};

const getCategoriesForLicense = (license, licensePolicy = DEFAULT_POLICY) => {
const getLicenseCategories = (licensePolicy = DEFAULT_POLICY) => {
const defaultCategories = licenseCatalog.categories;
const userCategories = licensePolicy.categories || [];
const filteredUserCategories = [];

userCategories.forEach(({name, licenses}) => {
if (name !== 'Invalid' && DEFAULT_LICENSE_CATEGORIES.includes(name)) {
defaultCategories.forEach((dc) => {
// eslint-disable-next-line no-param-reassign
dc.licenses = dc.licenses.filter((dcl) => !licenses.includes(dcl));
});
const targetCategory = defaultCategories.find(({name: targetName}) => name === targetName);
targetCategory.licenses = [...targetCategory.licenses, ...licenses];
} else {
filteredUserCategories.push({name, licenses});
}
});

return {
defaultCategories,
userCategories: filteredUserCategories,
};
};

const getCategoriesForLicense = ({license, defaultCategories, userCategories}) => {
if (!license || license === 'N/A') {
return {defaultCategory: 'N/A', userCategories: []};
}
Expand All @@ -46,23 +70,31 @@ const getCategoriesForLicense = (license, licensePolicy = DEFAULT_POLICY) => {
}

if (expressionLicenses) {
const {defaultCategory: cat1} = getCategoriesForLicense(
expressionLicenses[0],
licensePolicy,
);
const {defaultCategory: cat2} = getCategoriesForLicense(
expressionLicenses[1],
licensePolicy,
);
const {defaultCategory: cat1} = getCategoriesForLicense({
license: expressionLicenses[0],
defaultCategories,
userCategories,
});
const {defaultCategory: cat2} = getCategoriesForLicense({
license: expressionLicenses[1],
defaultCategories,
userCategories,
});

if ([cat1, cat2].includes('Invalid')) {
defaultCategory = 'Invalid';
} else {
const aggregateExpressionType =
LICENSE_TYPES[
DEFAULT_LICENSE_CATEGORIES[
condition === 'or'
? Math.min(LICENSE_TYPES.indexOf(cat1), LICENSE_TYPES.indexOf(cat2))
: Math.max(LICENSE_TYPES.indexOf(cat1), LICENSE_TYPES.indexOf(cat2))
? Math.min(
DEFAULT_LICENSE_CATEGORIES.indexOf(cat1),
DEFAULT_LICENSE_CATEGORIES.indexOf(cat2),
)
: Math.max(
DEFAULT_LICENSE_CATEGORIES.indexOf(cat1),
DEFAULT_LICENSE_CATEGORIES.indexOf(cat2),
)
];

defaultCategory = aggregateExpressionType;
Expand All @@ -75,18 +107,16 @@ const getCategoriesForLicense = (license, licensePolicy = DEFAULT_POLICY) => {

if (!defaultCategory) {
defaultCategory =
licenseCatalog.categories.find(({licenses}) => licenses.includes(license))?.type || 'Invalid';
defaultCategories.find(({licenses}) => licenses.includes(license))?.name || 'Invalid';
}

const userCategories =
(licensePolicy.categories || [])
.filter(({licenses}) => licenses.includes(license))
.map((c) => c.type) || [];
const selectedUserCategories =
userCategories.filter(({licenses}) => licenses.includes(license)).map((c) => c.name) || [];

return {defaultCategory, userCategories};
return {defaultCategory, userCategories: selectedUserCategories};
};

const getLicenseUsage = ({dependencies = [], licensePolicy = DEFAULT_POLICY}) => {
const getLicenseUsage = ({dependencies = [], defaultCategories, userCategories}) => {
const licenseUsage = dependencies.reduce((agg, {name, version, license}) => {
const licenseString = license || 'N/A';

Expand All @@ -98,7 +128,11 @@ const getLicenseUsage = ({dependencies = [], licensePolicy = DEFAULT_POLICY}) =>
{
string: licenseString,
meta: {
categories: getCategoriesForLicense(licenseString, licensePolicy),
categories: getCategoriesForLicense({
license: licenseString,
defaultCategories,
userCategories,
}),
isSpdxExpression: isSpdxExpression(licenseString),
},
dependencies: [{name, version}],
Expand Down Expand Up @@ -243,6 +277,7 @@ const getLicenseIssues = ({licenseUsage, packageGraph, licensePolicy = DEFAULT_P
};

module.exports = {
getLicenseCategories,
getCategoriesForLicense,
getLicenseUsage,
getLicenseIssues,
Expand Down
Loading

0 comments on commit 0f473cc

Please sign in to comment.