Skip to content

Commit

Permalink
Fix licenses not being resolved by name
Browse files Browse the repository at this point in the history
The name was already considered for resolution, but the matching was only performed on license IDs.

Fixes #3781

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed May 30, 2024
1 parent c64055a commit 27e8a5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,10 @@ private static void resolveAndApplyLicense(final QueryManager qm,
}
}

private static License resolveLicense(final QueryManager qm, final String licenseId) {
private static License resolveLicense(final QueryManager qm, final String licenseIdOrName) {
final Query<License> query = qm.getPersistenceManager().newQuery(License.class);
query.setFilter("licenseId == :licenseId");
query.setParameters(licenseId);
query.setFilter("licenseId == :licenseIdOrName || name == :licenseIdOrName");
query.setNamedParameters(Map.of("licenseIdOrName", licenseIdOrName));
try {
final License license = query.executeUnique();
return license != null ? license : License.UNRESOLVED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,49 @@ public void informWithExistingComponentPropertiesAndBomWithComponentProperties()
});
}

@Test
public void informWithLicenseResolutionByNameTest() {
final var license = new License();
license.setLicenseId("MIT");
license.setName("MIT License");
qm.persist(license);

final var project = new Project();
project.setName("acme-license-app");
qm.persist(project);

final byte[] bomBytes = """
{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b80",
"version": 1,
"components": [
{
"type": "library",
"name": "acme-lib-x",
"licenses": [
{
"license": {
"name": "MIT License"
}
}
]
}
]
}
""".getBytes(StandardCharsets.UTF_8);

final var bomUploadEvent = new BomUploadEvent(qm.detach(Project.class, project.getId()), bomBytes);
new BomUploadProcessingTask().inform(bomUploadEvent);
awaitBomProcessedNotification(bomUploadEvent);

assertThat(qm.getAllComponents(project)).satisfiesExactly(component -> {
assertThat(component.getResolvedLicense()).isNotNull();
assertThat(component.getResolvedLicense().getLicenseId()).isEqualTo("MIT");
});
}

@Test // https://github.com/DependencyTrack/dependency-track/issues/1905
public void informIssue1905Test() throws Exception {
final var project = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false);
Expand Down

0 comments on commit 27e8a5c

Please sign in to comment.