Skip to content

Commit

Permalink
CuratedPackage: Rename the pkg property to metadata
Browse files Browse the repository at this point in the history
This avoids the ugly `pkg.pkg` construct when accessing the `Package` of
a `CuratedPackage`.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Aug 31, 2022
1 parent c9542b8 commit 012cf82
Show file tree
Hide file tree
Showing 40 changed files with 208 additions and 197 deletions.
2 changes: 1 addition & 1 deletion advisor/src/main/kotlin/Advisor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Advisor(

val results = sortedMapOf<Identifier, List<AdvisorResult>>()

val packages = ortResult.getPackages(skipExcluded).map { it.pkg }
val packages = ortResult.getPackages(skipExcluded).map { it.metadata }
if (packages.isEmpty()) {
logger.info { "There are no packages to give advice for." }
} else {
Expand Down
2 changes: 1 addition & 1 deletion advisor/src/test/kotlin/advisors/VulnerableCodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private fun resultFile(): File = File(TEST_FILES_ROOT).resolve(TEST_RESULT_NAME)
* Return a list with [Package]s from the analyzer result file that serve as input for the [VulnerableCode] advisor.
*/
private fun inputPackages(): List<Package> =
resultFile().readValue<OrtResult>().getPackages(false).map { it.pkg }
resultFile().readValue<OrtResult>().getPackages(false).map { it.metadata }

/**
* Generate the JSON body of the request to query information about packages. It mainly consists of an array with the
Expand Down
14 changes: 8 additions & 6 deletions analyzer/src/funTest/kotlin/managers/PubFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class PubFunTest : WordSpec() {
// Reduce the analyzer result to only the Pub project and its dependencies.
val reducedAnalyzerResult = AnalyzerResult(
projects = sortedSetOf(project),
packages = analyzerResult.packages.filterTo(sortedSetOf()) { it.pkg.id in projectDependencies },
packages = analyzerResult.packages.filterTo(sortedSetOf()) {
it.metadata.id in projectDependencies
},
issues = analyzerResult.issues
)

Expand Down Expand Up @@ -176,16 +178,16 @@ class PubFunTest : WordSpec() {
val packages = analyzer?.result?.packages?.toMutableSet()
val aapt2Package =
packages?.find {
it.pkg.id.type == "Maven" &&
it.pkg.id.namespace == "com.android.tools.build" &&
it.pkg.id.name == "aapt2"
it.metadata.id.type == "Maven" &&
it.metadata.id.namespace == "com.android.tools.build" &&
it.metadata.id.name == "aapt2"
}

val patchedPackages = packages?.map { pkg ->
if (pkg == aapt2Package) {
aapt2Package?.copy(
pkg = aapt2Package.pkg.copy(
binaryArtifact = aapt2Package.pkg.binaryArtifact.copy(
metadata = aapt2Package.metadata.copy(
binaryArtifact = aapt2Package.metadata.binaryArtifact.copy(
url = "***",
hash = Hash("***", HashAlgorithm.SHA1)
)
Expand Down
2 changes: 1 addition & 1 deletion analyzer/src/main/kotlin/AnalyzerResultBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AnalyzerResultBuilder(private val curationProvider: PackageCurationProvide
private val dependencyGraphs = sortedMapOf<String, DependencyGraph>()

fun build(): AnalyzerResult {
val duplicateIds = (projects.map { it.id } + packages.map { it.pkg.id }).getDuplicates()
val duplicateIds = (projects.map { it.id } + packages.map { it.metadata.id }).getDuplicates()
require(duplicateIds.isEmpty()) {
"AnalyzerResult contains packages that are also projects. Duplicates: '$duplicateIds'."
}
Expand Down
4 changes: 2 additions & 2 deletions analyzer/src/main/kotlin/managers/SpdxDocumentFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class SpdxDocumentFile(
val artifact = getRemoteArtifact()

return CuratedPackage(
pkg = Package(
metadata = Package(
id = id,
purl = locateExternalReference(SpdxExternalReference.Type.Purl) ?: id.toPurl(),
cpe = locateCpe(),
Expand Down Expand Up @@ -529,7 +529,7 @@ class SpdxDocumentFile(
ProjectAnalyzerResult(
project = project,
// TODO: How to handle concluded licenses from an SPDX package?
packages = packages.mapTo(sortedSetOf()) { it.pkg }
packages = packages.mapTo(sortedSetOf()) { it.metadata }
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PackageManagerDependencyHandler(
private val navigator = DependencyGraphNavigator(analyzerResult.dependencyGraphs)

override fun createPackage(dependency: ResolvableDependencyNode, issues: MutableList<OrtIssue>): Package? =
analyzerResult.packages.find { it.pkg.id == dependency.id }?.pkg
analyzerResult.packages.find { it.metadata.id == dependency.id }?.metadata

override fun dependenciesFor(dependency: ResolvableDependencyNode): Collection<ResolvableDependencyNode> =
buildList {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/kotlin/commands/DownloaderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
}

if (PackageType.PACKAGE in packageTypes) {
addAll(analyzerResult.packages.map { it.pkg })
addAll(analyzerResult.packages.map { it.metadata })
}
}

Expand Down
2 changes: 1 addition & 1 deletion evaluator/src/main/kotlin/DependencyRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DependencyRule(
"(project=${project.id.toCoordinates()}, scope=$scopeName, level=$level)."

override fun issueSource() =
"$name - ${pkg.pkg.id.toCoordinates()} (dependency of ${project.id.toCoordinates()} in scope $scopeName)"
"$name - ${pkg.metadata.id.toCoordinates()} (dependency of ${project.id.toCoordinates()} in scope $scopeName)"

/**
* A [RuleMatcher] that checks if the level of the [dependency] inside the dependency tree equals [level].
Expand Down
41 changes: 22 additions & 19 deletions evaluator/src/main/kotlin/PackageRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ open class PackageRule(
@Suppress("UNUSED") // This is intended to be used by rule implementations.
val uncuratedPkg by lazy { pkg.toUncuratedPackage() }

override val description = "Evaluating rule '$name' for package '${pkg.pkg.id.toCoordinates()}'."
override val description = "Evaluating rule '$name' for package '${pkg.metadata.id.toCoordinates()}'."

override fun issueSource() = "$name - ${pkg.pkg.id.toCoordinates()}"
override fun issueSource() = "$name - ${pkg.metadata.id.toCoordinates()}"

override fun runInternal() {
licenseRules.forEach { it.evaluate() }
Expand All @@ -71,7 +71,7 @@ open class PackageRule(

override fun matches(): Boolean {
val run = ruleSet.ortResult.advisor ?: return false
return run.results.getVulnerabilities(pkg.pkg.id).isNotEmpty()
return run.results.getVulnerabilities(pkg.metadata.id).isNotEmpty()
}
}
}
Expand All @@ -86,7 +86,7 @@ open class PackageRule(

override fun matches(): Boolean {
val run = ruleSet.ortResult.advisor ?: return false
return run.results.getVulnerabilities(pkg.pkg.id)
return run.results.getVulnerabilities(pkg.metadata.id)
.filter { vulnerability -> !ruleSet.resolutionProvider.isResolved(vulnerability) }
.flatMap { it.references }
.filter { reference -> reference.scoringSystem == scoringSystem }
Expand All @@ -112,7 +112,7 @@ open class PackageRule(
object : RuleMatcher {
override val description = "isExcluded()"

override fun matches() = ruleSet.ortResult.isExcluded(pkg.pkg.id)
override fun matches() = ruleSet.ortResult.isExcluded(pkg.metadata.id)
}

/**
Expand All @@ -123,7 +123,7 @@ open class PackageRule(
object : RuleMatcher {
override val description = "isFromOrg(${names.joinToString()})"

override fun matches() = pkg.pkg.id.isFromOrg(*names)
override fun matches() = pkg.metadata.id.isFromOrg(*names)
}

/**
Expand All @@ -133,7 +133,7 @@ open class PackageRule(
object : RuleMatcher {
override val description = "isMetaDataOnly()"

override fun matches() = pkg.pkg.isMetaDataOnly
override fun matches() = pkg.metadata.isMetaDataOnly
}

/**
Expand All @@ -143,7 +143,7 @@ open class PackageRule(
object : RuleMatcher {
override val description = "isProject()"

override fun matches() = ruleSet.ortResult.isProject(pkg.pkg.id)
override fun matches() = ruleSet.ortResult.isProject(pkg.metadata.id)
}

/**
Expand All @@ -153,15 +153,15 @@ open class PackageRule(
object : RuleMatcher {
override val description = "isType($type)"

override fun matches() = pkg.pkg.id.type == type
override fun matches() = pkg.metadata.id.type == type
}

/**
* A DSL function to configure a [LicenseRule] and add it to this rule.
*/
fun licenseRule(name: String, licenseView: LicenseView, block: LicenseRule.() -> Unit) {
resolvedLicenseInfo.filter(licenseView, filterSources = true)
.applyChoices(ruleSet.ortResult.getPackageLicenseChoices(pkg.pkg.id), licenseView)
.applyChoices(ruleSet.ortResult.getPackageLicenseChoices(pkg.metadata.id), licenseView)
.applyChoices(ruleSet.ortResult.getRepositoryLicenseChoices(), licenseView).forEach { resolvedLicense ->
resolvedLicense.sources.forEach { licenseSource ->
licenseRules += LicenseRule(name, resolvedLicense, licenseSource).apply(block)
Expand All @@ -170,22 +170,22 @@ open class PackageRule(
}

fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.pkg.id, null, null, message, howToFix)
issue(severity, pkg.metadata.id, null, null, message, howToFix)

/**
* Add a [hint][Severity.HINT] to the list of [violations].
*/
fun hint(message: String, howToFix: String) = hint(pkg.pkg.id, null, null, message, howToFix)
fun hint(message: String, howToFix: String) = hint(pkg.metadata.id, null, null, message, howToFix)

/**
* Add a [warning][Severity.WARNING] to the list of [violations].
*/
fun warning(message: String, howToFix: String) = warning(pkg.pkg.id, null, null, message, howToFix)
fun warning(message: String, howToFix: String) = warning(pkg.metadata.id, null, null, message, howToFix)

/**
* Add an [error][Severity.ERROR] to the list of [violations].
*/
fun error(message: String, howToFix: String) = error(pkg.pkg.id, null, null, message, howToFix)
fun error(message: String, howToFix: String) = error(pkg.metadata.id, null, null, message, howToFix)

/**
* A [Rule] to check a single license of the [package][pkg].
Expand Down Expand Up @@ -218,7 +218,7 @@ open class PackageRule(
"'${resolvedLicense.license}'."

override fun issueSource() =
"$name - ${pkg.pkg.id.toCoordinates()} - ${resolvedLicense.license} ($licenseSource)"
"$name - ${pkg.metadata.id.toCoordinates()} - ${resolvedLicense.license} ($licenseSource)"

/**
* A [RuleMatcher] that checks if a [detected][LicenseSource.DETECTED] license is
Expand Down Expand Up @@ -247,21 +247,24 @@ open class PackageRule(
}

fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.pkg.id, license, licenseSource, message, howToFix)
issue(severity, pkg.metadata.id, license, licenseSource, message, howToFix)

/**
* Add a [hint][Severity.HINT] to the list of [violations].
*/
fun hint(message: String, howToFix: String) = hint(pkg.pkg.id, license, licenseSource, message, howToFix)
fun hint(message: String, howToFix: String) =
hint(pkg.metadata.id, license, licenseSource, message, howToFix)

/**
* Add a [warning][Severity.WARNING] to the list of [violations].
*/
fun warning(message: String, howToFix: String) = warning(pkg.pkg.id, license, licenseSource, message, howToFix)
fun warning(message: String, howToFix: String) =
warning(pkg.metadata.id, license, licenseSource, message, howToFix)

/**
* Add an [error][Severity.ERROR] to the list of [violations].
*/
fun error(message: String, howToFix: String) = error(pkg.pkg.id, license, licenseSource, message, howToFix)
fun error(message: String, howToFix: String) =
error(pkg.metadata.id, license, licenseSource, message, howToFix)
}
}
4 changes: 2 additions & 2 deletions evaluator/src/main/kotlin/RuleSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RuleSet(
}.orEmpty()

packages.forEach { curatedPackage ->
val resolvedLicenseInfo = licenseInfoResolver.resolveLicenseInfo(curatedPackage.pkg.id)
val resolvedLicenseInfo = licenseInfoResolver.resolveLicenseInfo(curatedPackage.metadata.id)
PackageRule(this, name, curatedPackage, resolvedLicenseInfo).apply {
configure()
evaluate()
Expand Down Expand Up @@ -101,7 +101,7 @@ class RuleSet(
if (curatedPackage == null) {
logger.warn { "Could not find package for dependency ${node.id.toCoordinates()}, skipping rule $name." }
} else {
val resolvedLicenseInfo = licenseInfoResolver.resolveLicenseInfo(curatedPackage.pkg.id)
val resolvedLicenseInfo = licenseInfoResolver.resolveLicenseInfo(curatedPackage.metadata.id)

DependencyRule(
this,
Expand Down
2 changes: 1 addition & 1 deletion evaluator/src/main/resources/rules/no_gpl.rules.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ val ruleSet = ruleSet(ortResult, licenseInfoResolver) {
}

error(
"The package '${pkg.pkg.id.toCoordinates()}' has the ${licenseSource.name} license '$license'.",
"The package '${pkg.metadata.id.toCoordinates()}' has the ${licenseSource.name} license '$license'.",
"Remove the dependency on this package."
)
}
Expand Down
10 changes: 5 additions & 5 deletions evaluator/src/test/kotlin/DependencyRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DependencyRuleTest : WordSpec() {
ruleSet = ruleSet,
name = "test",
pkg = pkg,
resolvedLicenseInfo = ruleSet.licenseInfoResolver.resolveLicenseInfo(pkg.pkg.id),
resolvedLicenseInfo = ruleSet.licenseInfoResolver.resolveLicenseInfo(pkg.metadata.id),
dependency = dependency,
ancestors = emptyList(),
level = 0,
Expand All @@ -44,14 +44,14 @@ class DependencyRuleTest : WordSpec() {
init {
"isAtTreeLevel()" should {
"return true if the dependency is at the expected tree level" {
val rule = createRule(packageWithoutLicense, packageWithoutLicense.pkg.toReference())
val rule = createRule(packageWithoutLicense, packageWithoutLicense.metadata.toReference())
val matcher = rule.isAtTreeLevel(0)

matcher.matches() shouldBe true
}

"return false if the dependency is not at the expected tree level" {
val rule = createRule(packageWithoutLicense, packageWithoutLicense.pkg.toReference())
val rule = createRule(packageWithoutLicense, packageWithoutLicense.metadata.toReference())
val matcher = rule.isAtTreeLevel(1)

matcher.matches() shouldBe false
Expand All @@ -60,14 +60,14 @@ class DependencyRuleTest : WordSpec() {

"isProjectFromOrg()" should {
"return true if the project is from org" {
val rule = createRule(packageWithoutLicense, packageWithoutLicense.pkg.toReference())
val rule = createRule(packageWithoutLicense, packageWithoutLicense.metadata.toReference())
val matcher = rule.isProjectFromOrg("ossreviewtoolkit")

matcher.matches() shouldBe true
}

"return false if the project is not from org" {
val rule = createRule(packageWithoutLicense, packageWithoutLicense.pkg.toReference())
val rule = createRule(packageWithoutLicense, packageWithoutLicense.metadata.toReference())
val matcher = rule.isProjectFromOrg("unknown")

matcher.matches() shouldBe false
Expand Down
2 changes: 1 addition & 1 deletion evaluator/src/test/kotlin/PackageRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PackageRuleTest : WordSpec() {
ruleSet = ruleSet,
name = "test",
pkg = pkg,
resolvedLicenseInfo = ruleSet.licenseInfoResolver.resolveLicenseInfo(pkg.pkg.id)
resolvedLicenseInfo = ruleSet.licenseInfoResolver.resolveLicenseInfo(pkg.metadata.id)
)

private fun PackageRule.createLicenseRule(license: SpdxSingleLicenseExpression, licenseSource: LicenseSource) =
Expand Down
Loading

0 comments on commit 012cf82

Please sign in to comment.