Makes License Issues a single table per manifest

This commit is contained in:
David Losert
2023-03-02 07:43:23 +00:00
committed by GitHub
parent 9f0792541a
commit b7a25f4e9b
4 changed files with 71 additions and 33 deletions
+25 -7
View File
@@ -219,7 +219,7 @@ test('addLicensesToSummary() - does not include entire section if no license iss
expect(text).toEqual('')
})
test('addLicensesToSummary() - includes all license issues', () => {
test('addLicensesToSummary() - includes all license issues in table', () => {
const licenseIssues = {
forbidden: [createTestChange()],
unresolved: [createTestChange(), createTestChange()],
@@ -230,9 +230,27 @@ test('addLicensesToSummary() - includes all license issues', () => {
const text = core.summary.stringify()
expect(text).toContain('<h2>License Issues</h2>')
expect(text).toContain('<h3>Incompatible Licenses</h3>')
expect(text).toContain('<h3>Unknown Licenses</h3>')
expect(text).toContain('<h3>Invalid SPDX License Definitions</h3>')
expect(text).toContain('<td>Incompatible License</td>')
expect(text).toContain('<td>Invalid SPDX License</td>')
expect(text).toContain('<td>Unknown License</td>')
})
test('addLicenseToSummary() - adds one table per manifest', () => {
const licenseIssues = {
forbidden: [
createTestChange({manifest: 'package.json'}),
createTestChange({manifest: '.github/workflows/test.yml'})
],
unresolved: [],
unlicensed: []
}
summary.addLicensesToSummary(licenseIssues, defaultConfig)
const text = core.summary.stringify()
expect(text).toContain('<h4><em>package.json</em></h4>')
expect(text).toContain('<h4><em>.github/workflows/test.yml</em></h4>')
})
test('addLicensesToSummary() - does not include specific license type sub-section if nothing is found', () => {
@@ -245,9 +263,9 @@ test('addLicensesToSummary() - does not include specific license type sub-sectio
summary.addLicensesToSummary(licenseIssues, defaultConfig)
const text = core.summary.stringify()
expect(text).not.toContain('<h3>Incompatible Licenses</h3>')
expect(text).not.toContain('<h3>Unknown Licenses</h3>')
expect(text).toContain('<h3>Invalid SPDX License Definitions</h3>')
expect(text).not.toContain('<td>Incompatible License</td>')
expect(text).not.toContain('<td>Unknown License</td>')
expect(text).toContain('<td>Invalid SPDX License</td>')
})
test('addLicensesToSummary() - includes list of configured allowed licenses', () => {
+13 -1
View File
@@ -57,12 +57,24 @@ const createFullSummary = async (): Promise<void> => {
createTestChange({
name: 'octoinvader',
license: 'Non SPDX License'
}),
createTestChange({
name: 'owner/action-1',
license: 'XYZ-License',
version: 'v1.2.2',
manifest: '.github/workflows/action.yml'
})
],
unlicensed: [
createTestChange({
name: 'my-other-dependency',
license: 'No License'
license: null
}),
createTestChange({
name: 'owner/action-2',
version: 'main',
license: null,
manifest: '.github/workflows/action.yml'
})
]
}
+4 -1
View File
@@ -14,7 +14,10 @@ import {isSPDXValid, octokitClient} from './utils'
* @param { { allow?: string[], deny?: string[]}} licenses An object with `allow`/`deny` keys, each containing a list of licenses.
* @returns {Promise<{Object.<string, Array.<Change>>}} A promise to a Record Object. The keys are strings, unlicensed, unresolved and forbidden. The values are a list of changes
*/
type InvalidLicenseChangeTypes = 'unlicensed' | 'unresolved' | 'forbidden'
export type InvalidLicenseChangeTypes =
| 'unlicensed'
| 'unresolved'
| 'forbidden'
export type InvalidLicenseChanges = Record<InvalidLicenseChangeTypes, Changes>
export async function getInvalidLicenseChanges(
changes: Change[],
+29 -24
View File
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import {ConfigurationOptions, Changes} from './schemas'
import {SummaryTableRow} from '@actions/core/lib/summary'
import {InvalidLicenseChanges} from './licenses'
import {InvalidLicenseChanges, InvalidLicenseChangeTypes} from './licenses'
import {groupDependenciesByManifest, getManifestsSet, renderUrl} from './utils'
const icons = {
@@ -131,6 +131,7 @@ export function addLicensesToSummary(
}
core.summary.addHeading('License Issues', 2)
printLicenseViolations(invalidLicenseChanges)
if (config.allow_licenses && config.allow_licenses.length > 0) {
core.summary.addQuote(
@@ -151,40 +152,44 @@ export function addLicensesToSummary(
`${invalidLicenseChanges.unresolved.length} licenses could not be validated`
)
printLicenseViolation(
`Incompatible Licenses`,
invalidLicenseChanges.forbidden
)
printLicenseViolation(
`Invalid SPDX License Definitions`,
invalidLicenseChanges.unresolved
)
printLicenseViolation(`Unknown Licenses`, invalidLicenseChanges.unlicensed)
core.summary.addSeparator()
}
function printLicenseViolation(heading: string, changes: Changes): void {
if (changes.length === 0) {
return
}
core.summary.addSeparator()
core.summary.addHeading(heading, 3)
const licenseIssueTypes: InvalidLicenseChangeTypes[] = [
'forbidden',
'unresolved',
'unlicensed'
]
const rows: SummaryTableRow[] = []
const manifests = getManifestsSet(changes)
const issueTypeNames: Record<InvalidLicenseChangeTypes, string> = {
forbidden: 'Incompatible License',
unresolved: 'Invalid SPDX License',
unlicensed: 'Unknown License'
}
for (const manifest of manifests) {
core.summary.addHeading(`<em>${manifest}</em>`, 4)
function printLicenseViolations(changes: InvalidLicenseChanges): void {
const rowsGroupedByManifest: Record<string, SummaryTableRow[]> = {}
for (const change of changes.filter(pkg => pkg.manifest === manifest)) {
rows.push([
for (const issueType of licenseIssueTypes) {
for (const change of changes[issueType]) {
if (!rowsGroupedByManifest[change.manifest]) {
rowsGroupedByManifest[change.manifest] = []
}
rowsGroupedByManifest[change.manifest].push([
renderUrl(change.source_repository_url, change.name),
change.version,
formatLicense(change.license)
formatLicense(change.license),
issueTypeNames[issueType]
])
}
}
core.summary.addTable([['Package', 'Version', 'License'], ...rows])
for (const [manifest, rows] of Object.entries(rowsGroupedByManifest)) {
core.summary.addHeading(`<em>${manifest}</em>`, 4)
core.summary.addTable([
['Package', 'Version', 'License', 'Issue Type'],
...rows
])
}
}