Refactoring on PR feedback.

This commit is contained in:
Federico Builes
2022-06-08 17:45:42 +02:00
parent 25271922eb
commit 4ac3d318ab
3 changed files with 47 additions and 38 deletions
+5 -5
View File
@@ -1,6 +1,6 @@
import {expect, test} from '@jest/globals' import {expect, test} from '@jest/globals'
import {Change, Changes} from '../src/schemas' import {Change, Changes} from '../src/schemas'
import {hasInvalidLicenses} from '../src/licenses' import {getDeniedLicenseChanges} from '../src/licenses'
let npmChange: Change = { let npmChange: Change = {
manifest: 'package.json', manifest: 'package.json',
@@ -46,14 +46,14 @@ let rubyChange: Change = {
] ]
} }
test('hasInvalidLicenses fails a licenses outside the allow list is found', async () => { test('it fails if a license outside the allow list is found', async () => {
const changes: Changes = [npmChange, rubyChange] const changes: Changes = [npmChange, rubyChange]
const invalidChanges = hasInvalidLicenses(changes, {allow: ['BSD']}) const invalidChanges = getDeniedLicenseChanges(changes, {allow: ['BSD']})
expect(invalidChanges[0]).toBe(npmChange) expect(invalidChanges[0]).toBe(npmChange)
}) })
test('hasInvalidLicenses fails if a denied license is found', async () => { test('it fails if a license inside the deny list is found', async () => {
const changes: Changes = [npmChange, rubyChange] const changes: Changes = [npmChange, rubyChange]
const invalidChanges = hasInvalidLicenses(changes, {deny: ['BSD']}) const invalidChanges = getDeniedLicenseChanges(changes, {deny: ['BSD']})
expect(invalidChanges[0]).toBe(rubyChange) expect(invalidChanges[0]).toBe(rubyChange)
}) })
+18 -14
View File
@@ -1,18 +1,22 @@
import {Change, ChangeSchema} from './schemas' import {Change, ChangeSchema} from './schemas'
export function hasInvalidLicenses( /**
* Loops through a list of changes, filtering and returning the
* ones that don't conform to the licenses allow/deny lists.
* @param {Change[]} changes The list of changes to filter.
* @param { { allow?: string[], deny?: string[]}} licenses An object with `allow`/`deny` keys, each containing a list of licenses.
* @returns {Array<Change} The list of denied changes.
*/
export function getDeniedLicenseChanges(
changes: Array<Change>, changes: Array<Change>,
allowLicenses: Array<string> | undefined, licenses: {
failLicenses: Array<string> | undefined allow?: Array<string>
deny?: Array<string>
}
): Array<Change> { ): Array<Change> {
let disallowed: Change[] = [] let {allow = [], deny = []} = licenses
if (allowLicenses === undefined) { let disallowed: Change[] = []
allowLicenses = []
}
if (failLicenses === undefined) {
failLicenses = []
}
for (const change of changes) { for (const change of changes) {
let license = change.license let license = change.license
@@ -20,12 +24,12 @@ export function hasInvalidLicenses(
if (license === null) { if (license === null) {
continue continue
} }
if (allowLicenses.length > 0) { if (allow.length > 0) {
if (!allowLicenses.includes(license)) { if (!allow.includes(license)) {
disallowed.push(change) disallowed.push(change)
} }
} else if (failLicenses.length > 0) { } else if (deny.length > 0) {
if (failLicenses.includes(license)) { if (deny.includes(license)) {
disallowed.push(change) disallowed.push(change)
} }
} }
+24 -19
View File
@@ -6,7 +6,7 @@ import {RequestError} from '@octokit/request-error'
import {Change, PullRequestSchema, Severity} from './schemas' import {Change, PullRequestSchema, Severity} from './schemas'
import {readConfigFile} from '../src/config' import {readConfigFile} from '../src/config'
import {filterChangesBySeverity} from '../src/filter' import {filterChangesBySeverity} from '../src/filter'
import {hasInvalidLicenses} from './licenses' import {getDeniedLicenseChanges} from './licenses'
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
@@ -31,18 +31,15 @@ async function run(): Promise<void> {
let minSeverity = config.fail_on_severity let minSeverity = config.fail_on_severity
let failed = false let failed = false
let licenseErrors = hasInvalidLicenses( let licenses = {
changes, allow: config.allow_licenses,
config.allow_licenses, deny: config.deny_licenses
config.deny_licenses }
)
let licenseErrors = getDeniedLicenseChanges(changes, licenses)
if (licenseErrors.length > 0) { if (licenseErrors.length > 0) {
printLicensesError( printLicensesError(licenseErrors, licenses)
licenseErrors,
config.allow_licenses,
config.deny_licenses
)
core.setFailed('Dependency review detected incompatible licenses.') core.setFailed('Dependency review detected incompatible licenses.')
return return
} }
@@ -118,17 +115,25 @@ function renderSeverity(
function printLicensesError( function printLicensesError(
changes: Array<Change>, changes: Array<Change>,
allowLicenses: Array<string> | undefined, licenses: {
denyLicenses: Array<string> | undefined allow?: Array<string>
deny?: Array<string>
}
): void { ): void {
core.info('Dependency review detected incompatible licenses.') if (changes.length == 0) {
return
if (allowLicenses !== undefined) {
core.info('\nAllowed licenses: ' + allowLicenses.join(', ') + '\n')
} }
if (denyLicenses !== undefined) { let {allow = [], deny = []} = licenses
core.info('\nDenied licenses: ' + denyLicenses.join(', ') + '\n')
core.info('Dependency review detected incompatible licenses.')
if (allow.length > 0) {
core.info('\nAllowed licenses: ' + allow.join(', ') + '\n')
}
if (deny.length > 0) {
core.info('\nDenied licenses: ' + deny.join(', ') + '\n')
} }
core.info('The following dependencies have incompatible licenses:\n') core.info('The following dependencies have incompatible licenses:\n')