Skip to content

Commit

Permalink
Make the mapping of authors to copyrights configurable
Browse files Browse the repository at this point in the history
Disable the feature by default as that seems to be the common
expectation.

Resolves #4314.

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
  • Loading branch information
sschuberth committed Sep 15, 2021
1 parent a696629 commit e239072
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 32 deletions.
1 change: 1 addition & 0 deletions cli/src/main/kotlin/commands/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
val licenseInfoResolver = LicenseInfoResolver(
provider = DefaultLicenseInfoProvider(ortResultInput, packageConfigurationProvider),
copyrightGarbage = copyrightGarbage,
addAuthorsToCopyrights = config.addAuthorsToCopyrights,
archiver = config.scanner.archive.createFileArchiver(),
licenseFilenamePatterns = LicenseFilenamePatterns.getInstance()
)
Expand Down
4 changes: 3 additions & 1 deletion cli/src/main/kotlin/commands/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ class ReporterCommand : CliktCommand(

val packageConfigurationProvider = packageConfigurationOption.createProvider()

val config = globalOptionsForSubcommands.config
val licenseInfoResolver = LicenseInfoResolver(
provider = DefaultLicenseInfoProvider(ortResult, packageConfigurationProvider),
copyrightGarbage = copyrightGarbage,
archiver = globalOptionsForSubcommands.config.scanner.archive.createFileArchiver(),
addAuthorsToCopyrights = config.addAuthorsToCopyrights,
archiver = config.scanner.archive.createFileArchiver(),
licenseFilenamePatterns = LicenseFilenamePatterns.getInstance()
)

Expand Down
4 changes: 3 additions & 1 deletion helper-cli/src/main/kotlin/common/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ internal data class ProcessedCopyrightStatement(
internal fun OrtResult.processAllCopyrightStatements(
omitExcluded: Boolean = true,
copyrightGarbage: Set<String> = emptySet(),
addAuthorsToCopyrights: Boolean = false,
packageConfigurationProvider: PackageConfigurationProvider = SimplePackageConfigurationProvider.EMPTY
): List<ProcessedCopyrightStatement> {
val result = mutableListOf<ProcessedCopyrightStatement>()
Expand All @@ -236,7 +237,8 @@ internal fun OrtResult.processAllCopyrightStatements(

val licenseInfoResolver = createLicenseInfoResolver(
packageConfigurationProvider = packageConfigurationProvider,
copyrightGarbage = CopyrightGarbage(copyrightGarbage.toSortedSet())
copyrightGarbage = CopyrightGarbage(copyrightGarbage.toSortedSet()),
addAuthorsToCopyrights = addAuthorsToCopyrights
)

getProjectAndPackageIds().forEach { id ->
Expand Down
5 changes: 5 additions & 0 deletions model/src/main/kotlin/config/OrtConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ data class OrtConfiguration(
*/
val licenseFilePatterns: LicenseFilenamePatterns = LicenseFilenamePatterns.DEFAULT,

/**
* A flag to indicate whether authors should be considered as copyright holders.
*/
val addAuthorsToCopyrights: Boolean = false,

/**
* The threshold from which on issues count as severe.
*/
Expand Down
39 changes: 21 additions & 18 deletions model/src/main/kotlin/licenses/LicenseInfoResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import org.ossreviewtoolkit.utils.ORT_NAME
class LicenseInfoResolver(
private val provider: LicenseInfoProvider,
private val copyrightGarbage: CopyrightGarbage,
val addAuthorsToCopyrights: Boolean,
val archiver: FileArchiver?,
val licenseFilenamePatterns: LicenseFilenamePatterns = LicenseFilenamePatterns.DEFAULT
) {
Expand Down Expand Up @@ -98,24 +99,26 @@ class LicenseInfoResolver(
licenseInfo.declaredLicenseInfo.processed.mapped.filterValues { it == license }.keys
)

licenseInfo.declaredLicenseInfo.authors.takeIf { it.isNotEmpty() }?.let {
locations.add(ResolvedLicenseLocation(
provenance = UnknownProvenance,
location = UNDEFINED_TEXT_LOCATION,
appliedCuration = null,
matchingPathExcludes = emptyList(),
copyrights = licenseInfo.declaredLicenseInfo.authors.mapTo(mutableSetOf()) { author ->
val statement = "Copyright (C) $author".takeUnless {
author.contains("Copyright", ignoreCase = true)
} ?: author

ResolvedCopyrightFinding(
statement = statement,
location = UNDEFINED_TEXT_LOCATION,
matchingPathExcludes = emptyList()
)
}
))
if (addAuthorsToCopyrights) {
licenseInfo.declaredLicenseInfo.authors.takeIf { it.isNotEmpty() }?.let {
locations.add(ResolvedLicenseLocation(
provenance = UnknownProvenance,
location = UNDEFINED_TEXT_LOCATION,
appliedCuration = null,
matchingPathExcludes = emptyList(),
copyrights = licenseInfo.declaredLicenseInfo.authors.mapTo(mutableSetOf()) { author ->
val statement = "Copyright (C) $author".takeUnless {
author.contains("Copyright", ignoreCase = true)
} ?: author

ResolvedCopyrightFinding(
statement = statement,
location = UNDEFINED_TEXT_LOCATION,
matchingPathExcludes = emptyList()
)
}
))
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions model/src/main/kotlin/utils/OrtResultExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ fun OrtResult.collectDeclaredLicenses(omitExcluded: Boolean = false): Map<Identi
fun OrtResult.createLicenseInfoResolver(
packageConfigurationProvider: PackageConfigurationProvider = SimplePackageConfigurationProvider.EMPTY,
copyrightGarbage: CopyrightGarbage = CopyrightGarbage(),
addAuthorsToCopyrights: Boolean = false,
archiver: FileArchiver? = null
): LicenseInfoResolver {
val licenseInfoProvider = DefaultLicenseInfoProvider(this, packageConfigurationProvider)
return LicenseInfoResolver(licenseInfoProvider, copyrightGarbage, archiver, LicenseFilenamePatterns.getInstance())
}
) = LicenseInfoResolver(
DefaultLicenseInfoProvider(this, packageConfigurationProvider),
copyrightGarbage,
addAuthorsToCopyrights,
archiver,
LicenseFilenamePatterns.getInstance()
)

/**
* Copy this [OrtResult] and add all [labels] to the existing labels, overwriting existing labels on conflict.
Expand Down
27 changes: 22 additions & 5 deletions model/src/test/kotlin/licenses/LicenseInfoResolverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ class LicenseInfoResolverTest : WordSpec() {
result should containNumberOfLocationsForLicense(bsdLicense, 4)
}

"resolve copyrights from authors" {
"resolve copyrights from authors if enabled" {
val licenseInfos = listOf(
createLicenseInfo(
id = pkgId,
authors = authors,
declaredLicenses = declaredLicenses
)
)
val resolver = createResolver(licenseInfos)
val resolver = createResolver(licenseInfos, addAuthorsToCopyrights = true)

val result = resolver.resolveLicenseInfo(pkgId)
result should containCopyrightStatementsForLicenseExactly(
Expand All @@ -542,6 +542,21 @@ class LicenseInfoResolverTest : WordSpec() {
"Copyright (C) The Author", "Copyright (C) The Other Author"
)
}

"not resolve copyrights from authors if disabled" {
val licenseInfos = listOf(
createLicenseInfo(
id = pkgId,
authors = authors,
declaredLicenses = declaredLicenses
)
)
val resolver = createResolver(licenseInfos, addAuthorsToCopyrights = false)

val result = resolver.resolveLicenseInfo(pkgId)
result should containCopyrightStatementsForLicenseExactly("LicenseRef-a")
result should containCopyrightStatementsForLicenseExactly("LicenseRef-b")
}
}

"resolveLicenseFiles()" should {
Expand Down Expand Up @@ -612,11 +627,13 @@ class LicenseInfoResolverTest : WordSpec() {
private fun createResolver(
data: List<LicenseInfo>,
copyrightGarbage: Set<String> = emptySet(),
addAuthorsToCopyrights: Boolean = false,
archiver: FileArchiver = FileArchiver.createDefault()
) = LicenseInfoResolver(
SimpleLicenseInfoProvider(data),
CopyrightGarbage(copyrightGarbage.toSortedSet()),
archiver
provider = SimpleLicenseInfoProvider(data),
copyrightGarbage = CopyrightGarbage(copyrightGarbage.toSortedSet()),
addAuthorsToCopyrights = addAuthorsToCopyrights,
archiver = archiver
)

private fun createLicenseInfo(
Expand Down
7 changes: 4 additions & 3 deletions model/src/test/kotlin/licenses/LicenseViewTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ import org.ossreviewtoolkit.utils.test.createDefault

class LicenseViewTest : WordSpec() {
private val licenseInfoResolver = LicenseInfoResolver(
DefaultLicenseInfoProvider(ortResult, SimplePackageConfigurationProvider.EMPTY),
CopyrightGarbage(),
FileArchiver.createDefault()
provider = DefaultLicenseInfoProvider(ortResult, SimplePackageConfigurationProvider.EMPTY),
copyrightGarbage = CopyrightGarbage(),
addAuthorsToCopyrights = false,
archiver = FileArchiver.createDefault()
)

private fun LicenseView.getLicensesWithSources(
Expand Down
1 change: 1 addition & 0 deletions reporter/src/main/kotlin/ReporterInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data class ReporterInput(
val licenseInfoResolver: LicenseInfoResolver = LicenseInfoResolver(
provider = DefaultLicenseInfoProvider(ortResult, packageConfigurationProvider),
copyrightGarbage = copyrightGarbage,
addAuthorsToCopyrights = ortConfig.addAuthorsToCopyrights,
archiver = ortConfig.scanner.archive.createFileArchiver(),
licenseFilenamePatterns = ortConfig.licenseFilePatterns
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ private fun ReporterInput.replaceOrtResult(ortResult: OrtResult): ReporterInput
licenseInfoResolver = LicenseInfoResolver(
provider = DefaultLicenseInfoProvider(ortResult, packageConfigurationProvider),
copyrightGarbage = copyrightGarbage,
addAuthorsToCopyrights = licenseInfoResolver.addAuthorsToCopyrights,
archiver = licenseInfoResolver.archiver,
licenseFilenamePatterns = licenseInfoResolver.licenseFilenamePatterns
)
Expand Down

0 comments on commit e239072

Please sign in to comment.