Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reporter: Prepare ReportTabelModelMapper for some clean-ups #7927

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.reporter
package org.ossreviewtoolkit.plugins.reporters.statichtml

import java.util.SortedMap

Expand All @@ -36,9 +36,9 @@ import org.ossreviewtoolkit.model.licenses.ResolvedLicense
import org.ossreviewtoolkit.utils.common.zipWithCollections
import org.ossreviewtoolkit.utils.spdx.SpdxExpression

fun Collection<ReportTableModel.ResolvableIssue>.containsUnresolved() = any { !it.isResolved }
internal fun Collection<ReportTableModel.ResolvableIssue>.containsUnresolved() = any { !it.isResolved }

data class ReportTableModel(
internal data class ReportTableModel(
/**
* The [VcsInfo] for the scanned project.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.reporter
package org.ossreviewtoolkit.plugins.reporters.statichtml

import org.ossreviewtoolkit.model.DependencyNavigator
import org.ossreviewtoolkit.model.Identifier
Expand All @@ -28,44 +28,40 @@ import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.RuleViolation
import org.ossreviewtoolkit.model.config.Excludes
import org.ossreviewtoolkit.model.config.ScopeExclude
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.model.utils.ResolutionProvider
import org.ossreviewtoolkit.reporter.ReportTableModel.DependencyRow
import org.ossreviewtoolkit.reporter.ReportTableModel.IssueRow
import org.ossreviewtoolkit.reporter.ReportTableModel.IssueTable
import org.ossreviewtoolkit.reporter.ReportTableModel.ProjectTable
import org.ossreviewtoolkit.reporter.ReportTableModel.ResolvableIssue
import org.ossreviewtoolkit.reporter.ReportTableModel.ResolvableViolation
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.DependencyRow
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.IssueRow
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.IssueTable
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.ProjectTable
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.ResolvableIssue
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.ResolvableViolation
import org.ossreviewtoolkit.reporter.HowToFixTextProvider
import org.ossreviewtoolkit.reporter.ReporterInput

/**
* A mapper which converts an [OrtResult] to a [ReportTableModel].
*/
object ReportTableModelMapper {
fun map(
ortResult: OrtResult,
licenseInfoResolver: LicenseInfoResolver,
resolutionProvider: ResolutionProvider,
howToFixTextProvider: HowToFixTextProvider
): ReportTableModel {
internal object ReportTableModelMapper {
fun map(input: ReporterInput): ReportTableModel {
val issueSummaryRows = mutableMapOf<Identifier, IssueRow>()

val analyzerResult = ortResult.analyzer?.result
val excludes = ortResult.getExcludes()
val analyzerResult = input.ortResult.analyzer?.result
val excludes = input.ortResult.getExcludes()

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

val allIds = sortedSetOf(project.id)
allIds += ortResult.dependencyNavigator.projectDependencies(project)
allIds += input.ortResult.dependencyNavigator.projectDependencies(project)

val projectIssues = ortResult.dependencyNavigator.projectIssues(project)
val projectIssues = input.ortResult.dependencyNavigator.projectIssues(project)
val tableRows = allIds.map { id ->
val scanResults = ortResult.getScanResultsForId(id)
val scanResults = input.ortResult.getScanResultsForId(id)

val resolvedLicenseInfo = licenseInfoResolver.resolveLicenseInfo(id)
val resolvedLicenseInfo = input.licenseInfoResolver.resolveLicenseInfo(id)

val concludedLicense = resolvedLicenseInfo.licenseInfo.concludedLicenseInfo.concludedLicense
val declaredLicenses = resolvedLicenseInfo.filter { LicenseSource.DECLARED in it.sources }
Expand All @@ -79,28 +75,30 @@ object ReportTableModelMapper {
it.summary.issues
}

val packageForId = ortResult.getPackage(id)?.metadata ?: ortResult.getProject(id)?.toPackage()
val pkg = input.ortResult.getPackageOrProject(id)?.metadata

val row = DependencyRow(
id = id,
sourceArtifact = packageForId?.sourceArtifact.orEmpty(),
vcsInfo = packageForId?.vcsProcessed.orEmpty(),
sourceArtifact = pkg?.sourceArtifact.orEmpty(),
vcsInfo = pkg?.vcsProcessed.orEmpty(),
scopes = scopesForDependencies[id].orEmpty().toSortedMap(),
concludedLicense = concludedLicense,
declaredLicenses = declaredLicenses,
detectedLicenses = detectedLicenses,
effectiveLicense = resolvedLicenseInfo.filterExcluded().effectiveLicense(
LicenseView.CONCLUDED_OR_DECLARED_AND_DETECTED,
ortResult.getPackageLicenseChoices(id),
ortResult.getRepositoryLicenseChoices()
input.ortResult.getPackageLicenseChoices(id),
input.ortResult.getRepositoryLicenseChoices()
)?.sorted(),
analyzerIssues = analyzerIssues.map {
it.toResolvableIssue(resolutionProvider, howToFixTextProvider)
it.toResolvableIssue(input.resolutionProvider, input.howToFixTextProvider)
},
scanIssues = scanIssues.map { it.toResolvableIssue(resolutionProvider, howToFixTextProvider) }
scanIssues = scanIssues.map {
it.toResolvableIssue(input.resolutionProvider, input.howToFixTextProvider)
}
)

val isRowExcluded = ortResult.isExcluded(row.id)
val isRowExcluded = input.ortResult.isExcluded(row.id)
val unresolvedAnalyzerIssues = row.analyzerIssues.filterUnresolved()
val unresolvedScanIssues = row.scanIssues.filterUnresolved()

Expand Down Expand Up @@ -129,7 +127,7 @@ object ReportTableModelMapper {

ProjectTable(
tableRows,
ortResult.getDefinitionFilePathRelativeToAnalyzerRoot(project),
input.ortResult.getDefinitionFilePathRelativeToAnalyzerRoot(project),
pathExcludes
)
}.orEmpty().toSortedMap(compareBy { it.id })
Expand All @@ -138,15 +136,15 @@ object ReportTableModelMapper {

// TODO: Use the prefixes up until the first '.' (which below get discarded) for some visual grouping in the
// report.
val labels = ortResult.labels.mapKeys { it.key.substringAfter(".") }
val labels = input.ortResult.labels.mapKeys { it.key.substringAfter(".") }

val ruleViolations = ortResult.getRuleViolations()
.map { it.toResolvableViolation(resolutionProvider) }
val ruleViolations = input.ortResult.getRuleViolations()
.map { it.toResolvableViolation(input.resolutionProvider) }
.sortedWith(VIOLATION_COMPARATOR)

return ReportTableModel(
ortResult.repository.vcsProcessed,
ortResult.repository.config,
input.ortResult.repository.vcsProcessed,
input.ortResult.repository.config,
ruleViolations,
issueSummaryTable,
projectTables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.ScopeExclude
import org.ossreviewtoolkit.model.licenses.ResolvedLicenseLocation
import org.ossreviewtoolkit.model.yamlMapper
import org.ossreviewtoolkit.reporter.ReportTableModel
import org.ossreviewtoolkit.reporter.ReportTableModel.IssueTable
import org.ossreviewtoolkit.reporter.ReportTableModel.ProjectTable
import org.ossreviewtoolkit.reporter.ReportTableModel.ResolvableIssue
import org.ossreviewtoolkit.reporter.ReportTableModelMapper
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.IssueTable
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.ProjectTable
import org.ossreviewtoolkit.plugins.reporters.statichtml.ReportTableModel.ResolvableIssue
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.reporter.containsUnresolved
import org.ossreviewtoolkit.utils.common.isValidUri
import org.ossreviewtoolkit.utils.common.joinNonBlank
import org.ossreviewtoolkit.utils.common.normalizeLineBreaks
Expand All @@ -76,12 +73,7 @@ class StaticHtmlReporter : Reporter {
private val licensesSha1 = mutableMapOf<String, String>()

override fun generateReport(input: ReporterInput, outputDir: File, config: PluginConfiguration): List<File> {
val reportTableModel = ReportTableModelMapper.map(
input.ortResult,
input.licenseInfoResolver,
input.resolutionProvider,
input.howToFixTextProvider
)
val reportTableModel = ReportTableModelMapper.map(input)

val html = renderHtml(reportTableModel)
val outputFile = outputDir.resolve(reportFilename)
Expand Down