Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to allow a license for a particular package #135336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe('tasks/lib/licenses', () => {
}).toThrow(PACKAGE.name);
});

it('throw an error when the packages license is invalid and not overriden', () => {
expect(() => {
assertLicensesValid({
packages: [PACKAGE],
validLicenses: [`not ${PACKAGE.licenses[0]}`],
perPackageOverrides: {
[`${PACKAGE.name}-${PACKAGE.version}`]: [`also not ${PACKAGE.licenses[0]}`],
},
});
}).toThrow(PACKAGE.name);
});

it('should return undefined if the package license is invalid but has an ovveride', () => {
expect(
assertLicensesValid({
packages: [PACKAGE],
validLicenses: [`not ${PACKAGE.licenses[0]}`],
perPackageOverrides: {
[`${PACKAGE.name}-${PACKAGE.version}`]: PACKAGE.licenses,
},
})
).toEqual(undefined);
});

it('throws an error when the package has no licenses', () => {
expect(() => {
assertLicensesValid({
Expand Down
15 changes: 13 additions & 2 deletions src/dev/license_checker/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ interface Options {
licenses: string[];
}>;
validLicenses: string[];
perPackageOverrides?: Record<string, string[]>;
}

/**
* When given a list of packages and the valid license
* options, either throws an error with details about
* violations or returns undefined.
*/
export function assertLicensesValid({ packages, validLicenses }: Options) {
export function assertLicensesValid({
packages,
validLicenses,
perPackageOverrides = {},
}: Options) {
const invalidMsgs = packages.reduce((acc, pkg) => {
const invalidLicenses = pkg.licenses.filter((license) => !validLicenses.includes(license));
const isValidLicense = (license: string) => validLicenses.includes(license);
const isValidLicenseForPackage = (license: string) =>
(perPackageOverrides[`${pkg.name}-${pkg.version}`] || []).includes(license);

const invalidLicenses = pkg.licenses.filter(
(license) => !isValidLicense(license) && !isValidLicenseForPackage(license)
);

if (pkg.licenses.length && !invalidLicenses.length) {
return acc;
Expand Down