Skip to content

Commit

Permalink
refactor(opossum reporter): adjust file structure
Browse files Browse the repository at this point in the history
Put public method at the top, put caller above callees
etc.

Signed-off-by: alexzurbonsen <[email protected]>
  • Loading branch information
alexzurbonsen committed Nov 22, 2024
1 parent 5c8382f commit 91b1609
Showing 1 changed file with 150 additions and 142 deletions.
292 changes: 150 additions & 142 deletions plugins/reporters/opossum/src/main/kotlin/OpossumReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,142 +104,35 @@ class OpossumReporter(
override val descriptor: PluginDescriptor = OpossumReporterFactory.descriptor,
private val config: OpossumReporterConfig
) : Reporter {
@Serializable
internal data class OpossumSignal(
@Transient
val uuid: UUID = UUID.randomUUID(),
val source: OpossumSignalSource,
val attributionConfidence: Int = 80,
val packageType: String?,
val packageNamespace: String?,
val packageName: String?,
val packageVersion: String?,
val copyright: String?,
val licenseName: String?,
val url: String?,
val preSelected: Boolean,
val followUp: OpossumFollowUp?,
val excludeFromNotice: Boolean,
val comment: String?
) {
companion object {
fun create(
source: String,
id: Identifier? = null,
url: String? = null,
license: SpdxExpression? = null,
copyright: String? = null,
comment: String? = null,
preSelected: Boolean = false,
followUp: Boolean = false,
excludeFromNotice: Boolean = false
): OpossumSignal {
return OpossumSignal(
source = OpossumSignalSource(name = source),
packageType = id?.getPurlType(),
packageNamespace = id?.namespace,
packageName = id?.name,
packageVersion = id?.version,
copyright = copyright,
licenseName = license?.toString(),
url = url,
preSelected = preSelected,
followUp = OpossumFollowUp.FOLLOW_UP.takeIf { followUp },
excludeFromNotice = excludeFromNotice,
comment = comment,
)
override fun generateReport(input: ReporterInput, outputDir: File): List<Result<File>> {
val reportFileResult = runCatching {
val opossumInput = createOpossumInput(input, config.maxDepth)

outputDir.resolve("report.opossum").also {
writeReport(it, opossumInput)
}
}

fun matches(other: OpossumSignal): Boolean =
source == other.source
&& packageType == other.packageType
&& packageNamespace == other.packageNamespace
&& packageName == other.packageName
&& packageVersion == other.packageVersion
&& copyright == other.copyright
&& licenseName == other.licenseName
&& url == other.url
&& preSelected == other.preSelected
&& comment == other.comment
return listOf(reportFileResult)
}

@Serializable
internal data class OpossumSignalSource(
val name: String,
val documentConfidence: Int = 80,
)

@Serializable
internal enum class OpossumFollowUp {
FOLLOW_UP,
internal fun createOpossumInput(input: ReporterInput, maxDepth: Int = Int.MAX_VALUE): OpossumInput {

Check notice on line 119 in plugins/reporters/opossum/src/main/kotlin/OpossumReporter.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Class member can have 'private' visibility

Function 'createOpossumInput' could be private
return OpossumInputCreator().create(input, maxDepth)
}

@Serializable(with = OpossumResourcesSerializer::class)
internal data class OpossumResources(
val tree: MutableMap<String, OpossumResources> = mutableMapOf()
) {
private fun addResource(pathPieces: List<String>) {
if (pathPieces.isEmpty()) return

val head = pathPieces.first()
val tail = pathPieces.drop(1)

if (head !in tree) tree[head] = OpossumResources()
tree.getValue(head).addResource(tail)
}

fun addResource(path: String) {
val pathPieces = path.split("/").filter { it.isNotEmpty() }

addResource(pathPieces)
}

fun isFile() = tree.isEmpty()

fun isPathAFile(path: String): Boolean {
val pathPieces = path.split("/").filter { it.isNotEmpty() }

return isPathAFile(pathPieces)
}

private fun isPathAFile(pathPieces: List<String>): Boolean {
if (pathPieces.isEmpty()) return isFile()

val head = pathPieces.first()
val tail = pathPieces.drop(1)

return head !in tree || tree.getValue(head).isPathAFile(tail)
private fun writeReport(outputFile: File, opossumInput: OpossumInput) {
val jsonFile = createOrtTempDir().resolve("input.json")
val json = Json {
explicitNulls = false
encodeDefaults = true
}

fun toFileList(): Set<String> =
tree.flatMapTo(mutableSetOf()) { (key, value) ->
value.toFileList().map { resolvePath(key, it, isDirectory = false) }
}.plus("/")
}
jsonFile.writeText(json.encodeToString(OpossumInput.serializer(), opossumInput))

@Serializable
internal data class OpossumFrequentLicense(
val shortName: String,
val fullName: String?,
val defaultText: String?
) : Comparable<OpossumFrequentLicense> {
override fun compareTo(other: OpossumFrequentLicense) =
compareValuesBy(
this,
other,
{ it.shortName },
{ it.fullName },
{ it.defaultText }
)
jsonFile.packZip(outputFile)
jsonFile.delete()
}

@Serializable
internal data class OpossumExternalAttributionSource(
val name: String,
val priority: Int
)

@Serializable
internal data class OpossumInput(
val metadata: OpossumInputMetadata = OpossumInputMetadata(),
Expand Down Expand Up @@ -579,33 +472,148 @@ class OpossumReporter(
val fileCreationDate: String = LocalDateTime.now().toString()
)

private fun writeReport(outputFile: File, opossumInput: OpossumInput) {
val jsonFile = createOrtTempDir().resolve("input.json")
val json = Json {
explicitNulls = false
encodeDefaults = true
@Serializable(with = OpossumResourcesSerializer::class)
internal data class OpossumResources(
val tree: MutableMap<String, OpossumResources> = mutableMapOf()
) {
private fun addResource(pathPieces: List<String>) {
if (pathPieces.isEmpty()) {
return
}

val head = pathPieces.first()
val tail = pathPieces.drop(1)

if (head !in tree) {
tree[head] = OpossumResources()
}

tree.getValue(head).addResource(tail)
}
jsonFile.writeText(json.encodeToString(OpossumInput.serializer(), opossumInput))

jsonFile.packZip(outputFile)
jsonFile.delete()
}
fun addResource(path: String) {
val pathPieces = path.split("/").filter { it.isNotEmpty() }

internal fun createOpossumInput(input: ReporterInput, maxDepth: Int = Int.MAX_VALUE): OpossumInput {
return OpossumInputCreator().create(input, maxDepth)
}
addResource(pathPieces)
}

override fun generateReport(input: ReporterInput, outputDir: File): List<Result<File>> {
val reportFileResult = runCatching {
val opossumInput = createOpossumInput(input, config.maxDepth)
fun isFile() = tree.isEmpty()

outputDir.resolve("report.opossum").also {
writeReport(it, opossumInput)
fun isPathAFile(path: String): Boolean {
val pathPieces = path.split("/").filter { it.isNotEmpty() }

return isPathAFile(pathPieces)
}

private fun isPathAFile(pathPieces: List<String>): Boolean {
if (pathPieces.isEmpty()) {
return isFile()
}

val head = pathPieces.first()
val tail = pathPieces.drop(1)

return head !in tree || tree.getValue(head).isPathAFile(tail)
}

return listOf(reportFileResult)
fun toFileList(): Set<String> =
tree.flatMapTo(mutableSetOf()) { (key, value) ->
value.toFileList().map { resolvePath(key, it, isDirectory = false) }
}.plus("/")
}

@Serializable
internal data class OpossumSignal(
@Transient
val uuid: UUID = UUID.randomUUID(),
val source: OpossumSignalSource,
val attributionConfidence: Int = 80,
val packageType: String?,
val packageNamespace: String?,
val packageName: String?,
val packageVersion: String?,
val copyright: String?,
val licenseName: String?,
val url: String?,
val preSelected: Boolean,
val followUp: OpossumFollowUp?,
val excludeFromNotice: Boolean,
val comment: String?
) {
companion object {
fun create(
source: String,
id: Identifier? = null,
url: String? = null,
license: SpdxExpression? = null,
copyright: String? = null,
comment: String? = null,
preSelected: Boolean = false,
followUp: Boolean = false,
excludeFromNotice: Boolean = false
): OpossumSignal {
return OpossumSignal(
source = OpossumSignalSource(name = source),
packageType = id?.getPurlType(),
packageNamespace = id?.namespace,
packageName = id?.name,
packageVersion = id?.version,
copyright = copyright,
licenseName = license?.toString(),
url = url,
preSelected = preSelected,
followUp = OpossumFollowUp.FOLLOW_UP.takeIf { followUp },
excludeFromNotice = excludeFromNotice,
comment = comment
)
}
}

fun matches(other: OpossumSignal): Boolean =
source == other.source
&& packageType == other.packageType
&& packageNamespace == other.packageNamespace
&& packageName == other.packageName
&& packageVersion == other.packageVersion
&& copyright == other.copyright
&& licenseName == other.licenseName
&& url == other.url
&& preSelected == other.preSelected
&& comment == other.comment
}

@Serializable
internal data class OpossumSignalSource(
val name: String,
val documentConfidence: Int = 80
)

@Serializable
internal enum class OpossumFollowUp {
FOLLOW_UP
}

Check notice

Code scanning / QDJVMC

Class member can have 'private' visibility Note

Function 'createOpossumInput' could be private

@Serializable
internal data class OpossumFrequentLicense(
val shortName: String,
val fullName: String?,
val defaultText: String?
) : Comparable<OpossumFrequentLicense> {
override fun compareTo(other: OpossumFrequentLicense) =
compareValuesBy(
this,
other,
{ it.shortName },
{ it.fullName },
{ it.defaultText }
)
}

@Serializable
internal data class OpossumExternalAttributionSource(
val name: String,
val priority: Int
)
}

private fun DependencyNode.getDependencies(): List<DependencyNode> =
Expand Down

0 comments on commit 91b1609

Please sign in to comment.