Compare commits

...
2 changed files with 93 additions and 0 deletions

No files matched your search

+48
View File
@@ -390,3 +390,51 @@ describe('GH License API fallback', () => {
expect(unlicensed.length).toEqual(1)
})
})
describe('manifest/lock file license deduplication', () => {
const manifestChange: Change = {
change_type: 'added',
manifest: 'pyproject.toml',
ecosystem: 'pip',
name: 'pydantic',
version: '^ 2.11.0',
package_url: 'pkg:pypi/pydantic',
license: null,
source_repository_url: null,
scope: 'runtime',
vulnerabilities: []
}
const lockChange: Change = {
...manifestChange,
manifest: 'poetry.lock',
version: '2.11.0',
package_url: 'pkg:pypi/pydantic@2.11.0',
license: 'MIT'
}
test('does not flag an unlicensed manifest entry when a licensed lock entry exists', async () => {
const {unlicensed} = await getInvalidLicenseChanges(
[manifestChange, lockChange],
{}
)
expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled()
expect(unlicensed.length).toEqual(0)
})
test('still flags an unlicensed manifest entry when there is no licensed counterpart', async () => {
const {unlicensed} = await getInvalidLicenseChanges([manifestChange], {})
expect(unlicensed.length).toEqual(1)
})
test('does not deduplicate across differing change_types', async () => {
const {unlicensed} = await getInvalidLicenseChanges(
[manifestChange, {...lockChange, change_type: 'removed'}],
{}
)
expect(unlicensed.length).toEqual(1)
})
})
+45
View File
@@ -154,6 +154,49 @@ const setGHLicenses = async (changes: Change[]): Promise<Change[]> => {
const truncatedDGLicense = (license: string): boolean =>
license.length === 255 && !spdx.isValid(license)
// Builds a key identifying a package across manifest and lock files, so that
// duplicate entries for the same package (differing only by version specifier)
// can be matched. Changes are keyed by their parsed PURL type and full name,
// ignoring the version.
function packageKey(change: Change): string {
const purl = parsePURL(change.package_url)
const type = purl.type.toLowerCase()
const fullName =
purl.namespace && purl.name
? `${purl.namespace}/${purl.name}`
: purl.name ?? purl.namespace
if (type && fullName) {
return `${type}/${fullName.toLowerCase()}`
}
// Fall back to ecosystem + name when the PURL can't be parsed.
return `${change.ecosystem.toLowerCase()}/${change.name.toLowerCase()}`
}
// The dependency graph API can list the same package more than once: e.g. from
// a manifest file (like pyproject.toml) with a version range and a null
// license, and from a lock file (like poetry.lock) with a pinned version and a
// resolved license. This drops the null-license entries for a package when
// another entry of the same change_type resolves a license for it, so the
// package is not incorrectly reported as unlicensed.
function filterLicensedDuplicates(changes: Changes): Changes {
const licensedKeys = new Set<string>()
for (const change of changes) {
if (change.change_type !== 'removed' && change.license !== null) {
licensedKeys.add(`${change.change_type}/${packageKey(change)}`)
}
}
return changes.filter(change => {
if (change.change_type === 'removed' || change.license !== null) {
return true
}
return !licensedKeys.has(`${change.change_type}/${packageKey(change)}`)
})
}
async function groupChanges(
changes: Changes,
licenseExclusions: PackageURL[] | null = null
@@ -191,6 +234,8 @@ async function groupChanges(
})
}
candidateChanges = filterLicensedDuplicates(candidateChanges)
const ghChanges = []
for (const change of candidateChanges) {