Skip to content

Commit

Permalink
Do not fail hard if an ORT result contains no analyzer result
Browse files Browse the repository at this point in the history
Pragmatically, when being asked to operate on empty input, then doing
nothing is the right thing to do, esp. in library code where the caller
should implement custom error handling.

However, as it is rather unusual to not even have an analyzer result in a
ORT result, still emit a warning in that case.

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
  • Loading branch information
sschuberth committed Apr 19, 2021
1 parent 5f6990c commit ab9d20c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
9 changes: 7 additions & 2 deletions advisor/src/main/kotlin/Advisor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ class Advisor(
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
}

requireNotNull(ortResult.analyzer) {
"The provided ORT result file '${ortFile.canonicalPath}' does not contain an analyzer result."
if (ortResult.analyzer == null) {
log.warn {
"Cannot run the advisor as the provided ORT result file '${ortFile.canonicalPath}' does not contain " +
"an analyzer result. No result will be added."
}

return ortResult
}

val providers = providerFactories.map { it.create(config) }
Expand Down
13 changes: 10 additions & 3 deletions cli/src/main/kotlin/commands/DownloaderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,22 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
when (input) {
is FileType -> {
val ortFile = (input as FileType).file
val (analyzerResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>().analyzer?.result }
val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in " +
"${duration.inMilliseconds}ms."
}

requireNotNull(analyzerResult) {
"The provided ORT result file '${ortFile.canonicalPath}' does not contain an analyzer result."
val analyzerResult = ortResult.analyzer?.result

if (analyzerResult == null) {
log.warn {
"Cannot run the downloader as the provided ORT result file '${ortFile.canonicalPath}' does " +
"not contain an analyzer result. Nothing will be downloaded."
}

throw ProgramResult(0)
}

val packages = mutableListOf<Package>().apply {
Expand Down
15 changes: 5 additions & 10 deletions reporter/src/main/kotlin/utils/ReportTableModelMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,12 @@ class ReportTableModelMapper(
val issueSummaryRows = mutableMapOf<Identifier, IssueRow>()
val summaryRows = mutableMapOf<Identifier, SummaryRow>()

requireNotNull(ortResult.analyzer?.result) {
"The provided ORT result does not contain an analyzer result."
}

val analyzerResult = ortResult.analyzer!!.result
val excludes = ortResult.getExcludes()

val scanRecord = ortResult.scanner?.results
val analyzerResult = ortResult.analyzer?.result
val analyzerIssuesForPackages = ortResult.getPackages().associateBy({ it.pkg.id }, { it.pkg.collectIssues() })
val scanRecord = ortResult.scanner?.results
val excludes = ortResult.getExcludes()

val projectTables = analyzerResult.projects.associateWith { project ->
val projectTables = analyzerResult?.projects?.associateWith { project ->
val scopesForDependencies = project.getScopesForDependencies(excludes)
val pathExcludes = excludes.findPathExcludes(project, ortResult)

Expand Down Expand Up @@ -224,7 +219,7 @@ class ReportTableModelMapper(
ortResult.getDefinitionFilePathRelativeToAnalyzerRoot(project),
pathExcludes
)
}.toSortedMap()
}.orEmpty().toSortedMap()

val issueSummaryTable = IssueTable(issueSummaryRows.values.toList().sortedBy { it.id })

Expand Down
9 changes: 7 additions & 2 deletions scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ abstract class Scanner(
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
}

requireNotNull(ortResult.analyzer) {
"The provided ORT result file '${ortFile.canonicalPath}' does not contain an analyzer result."
if (ortResult.analyzer == null) {
log.warn {
"Cannot run the scanner as the provided ORT result file '${ortFile.canonicalPath}' does not contain " +
"an analyzer result. No result will be added."
}

return ortResult
}

// Add the projects as packages to scan.
Expand Down

0 comments on commit ab9d20c

Please sign in to comment.