Skip to content

Commit

Permalink
refactor(cyclonedx): Continue with remaining formats even if one failed
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jul 16, 2024
1 parent 78a5735 commit 73de3e5
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions plugins/reporters/cyclonedx/src/main/kotlin/CycloneDxReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import java.util.Date
import java.util.SortedSet
import java.util.UUID

import org.apache.logging.log4j.kotlin.logger

import org.cyclonedx.Version
import org.cyclonedx.generators.BomGeneratorFactory
import org.cyclonedx.model.AttachmentText
Expand Down Expand Up @@ -368,37 +370,41 @@ class CycloneDxReporter : Reporter {
outputFileExtensions.forEach { fileExtension ->
val outputFile = outputDir.resolve("$outputName.$fileExtension")

val bomGenerator = when (fileExtension) {
"xml" -> BomGeneratorFactory.createXml(schemaVersion, bom).toXmlString()
"json" -> {
// JSON output cannot handle extensible types (see [1]), so simply remove them. As JSON output is
// guaranteed to be the last format serialized, it is okay to modify the BOM here without doing a
// deep copy first.
//
// [1] https://github.com/CycloneDX/cyclonedx-core-java/issues/99.
val bomWithoutExtensibleTypes = bom.apply {
components.forEach { component ->
// Clear the "dependencyType".
component.extensibleTypes = null

component.licenses.licenses.forEach { license ->
// Clear the "origin".
license.extensibleTypes = null
runCatching {
val bomGenerator = when (fileExtension) {
"xml" -> BomGeneratorFactory.createXml(schemaVersion, bom).toXmlString()
"json" -> {
// JSON output cannot handle extensible types (see [1]), so simply remove them. As JSON output
// is guaranteed to be the last format serialized, it is okay to modify the BOM here without
// doing a deep copy first.
//
// [1] https://github.com/CycloneDX/cyclonedx-core-java/issues/99.
val bomWithoutExtensibleTypes = bom.apply {
components.forEach { component ->
// Clear the "dependencyType".
component.extensibleTypes = null

component.licenses.licenses.forEach { license ->
// Clear the "origin".
license.extensibleTypes = null
}

// Remove duplicates that may occur due to clearing the distinguishing extensive type.
component.licenses.licenses = component.licenses.licenses.distinct()
}

// Remove duplicates that may occur due to clearing the distinguishing extensive type.
component.licenses.licenses = component.licenses.licenses.distinct()
}

BomGeneratorFactory.createJson(schemaVersion, bomWithoutExtensibleTypes).toJsonString()
}

BomGeneratorFactory.createJson(schemaVersion, bomWithoutExtensibleTypes).toJsonString()
else -> throw IllegalArgumentException("Unsupported CycloneDX file extension '$fileExtension'.")
}

else -> throw IllegalArgumentException("Unsupported CycloneDX file extension '$fileExtension'.")
outputFile.bufferedWriter().use { it.write(bomGenerator) }
writtenFiles += outputFile
}.onFailure {
logger.error("Unable to create CycloneDX report: ", it)
}

outputFile.bufferedWriter().use { it.write(bomGenerator) }
writtenFiles += outputFile
}

return writtenFiles
Expand Down

0 comments on commit 73de3e5

Please sign in to comment.