Skip to content

Commit d5cf793

Browse files
committed
feat(reporter): Implement the FossID snippet reporter
This is an HTML report to present the snippet findings. HTML format was chosen to be able to use AsciiDoc collapsible blocks [1] for how-to-fix hints. Such blocks will be added in a future commit. [1]: https://docs.asciidoctor.org/asciidoc/latest/blocks/collapsible/ Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent 42203c4 commit d5cf793

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

cli/src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<logger name="org.ossreviewtoolkit.analyzer.managers.Yarn2" level="INFO" />
3535
<logger name="org.ossreviewtoolkit.clients.fossid.FossIdRestService" level="INFO" />
3636
<logger name="org.ossreviewtoolkit.reporter.reporters.fossid.FossIdReporter" level="INFO" />
37+
<logger name="org.ossreviewtoolkit.plugins.reporters.fossid.FossIdSnippetReporter" level="INFO" />
3738
<logger name="org.ossreviewtoolkit.scanner.scanners.fossid.FossId" level="INFO" />
3839
<logger name="org.ossreviewtoolkit.scanner.scanners.fossid.FossIdConfig" level="INFO" />
3940
<logger name="org.ossreviewtoolkit.scanner.scanners.fossid.FossIdUrlProvider" level="INFO" />

plugins/reporters/fossid-snippets/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ plugins {
2424

2525
dependencies {
2626
api(project(":reporter"))
27+
28+
implementation(project(":plugins:reporters:asciidoc-reporter"))
29+
implementation(project(":plugins:reporters:freemarker-reporter"))
30+
31+
implementation(libs.kotlinxCoroutines)
2732
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.reporters.fossidsnippets
21+
22+
import java.io.File
23+
24+
import org.apache.logging.log4j.kotlin.Logging
25+
import org.ossreviewtoolkit.plugins.reporters.asciidoc.HtmlTemplateReporter
26+
import org.ossreviewtoolkit.plugins.reporters.freemarker.FreemarkerTemplateProcessor
27+
import org.ossreviewtoolkit.reporter.Reporter
28+
import org.ossreviewtoolkit.reporter.ReporterInput
29+
30+
class FossIdSnippetReporter : Reporter {
31+
companion object : Logging {
32+
33+
private const val TEMPLATE_NAME = "fossid_snippets"
34+
35+
val delegateReporter = HtmlTemplateReporter()
36+
}
37+
38+
override val type = "FossIdSnippets"
39+
40+
override fun generateReport(input: ReporterInput, outputDir: File, options: Map<String, String>): List<File> {
41+
val extendedOptions = options + (FreemarkerTemplateProcessor.OPTION_TEMPLATE_ID to TEMPLATE_NAME)
42+
return delegateReporter.generateReport(input, outputDir, extendedOptions)
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.ossreviewtoolkit.plugins.reporters.fossid.FossIdSnippetReporter
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[#--
2+
Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
License-Filename: LICENSE
18+
--]
19+
20+
:publisher: OSS Review Toolkit
21+
[#assign now = .now]
22+
:revdate: ${now?date?iso_local}
23+
24+
:title-page:
25+
:sectnums:
26+
:toc:
27+
28+
= FossID Snippets
29+
List of all the packages with their files and snippets.
30+
[#list ortResult.scanResults as package, scanResults]
31+
32+
== Package '${package.toCoordinates()}'
33+
34+
[#list scanResults as scanResult]
35+
[#assign summary = scanResult.summary]
36+
37+
Scan start time : ${summary.startTime} +
38+
End time : ${summary.startTime} +
39+
[#if scanResult.provenance.vcsInfo??]
40+
[#assign gitRepoUrl = scanResult.provenance.vcsInfo.url]
41+
[#assign gitRevision = scanResult.provenance.vcsInfo.revision]
42+
Git repo URL: ${gitRepoUrl} +
43+
Git revision: ${gitRevision}
44+
45+
[#if gitRepoUrl?contains("github.com")]
46+
[#assign githubBaseURL = '${gitRepoUrl?remove_ending(".git")}/blob/${gitRevision}']
47+
[/#if]
48+
[/#if]
49+
50+
[#assign snippets = helper.groupSnippetsByFile(summary.snippetFindings)]
51+
52+
[#list snippets as filePath, snippetFindings]
53+
[#if gitRepoUrl?? && gitRepoUrl?contains("github.com")]
54+
[#assign localFileURL = '_${githubBaseURL}/${filePath}[source]_']
55+
[#else]
56+
[#assign localFileURL = "_source_"]
57+
[/#if]
58+
59+
[#assign licenses = helper.collectLicenses(snippetFindings)]
60+
61+
*${filePath}* +
62+
License(s):
63+
[#list licenses as license]
64+
${license}[#sep],
65+
[/#list]
66+
67+
[width=100%]
68+
[cols="1,1,3,1,3,3,1,1"]
69+
|===
70+
| ID | Match | PUrl | License | File | URL | Score | Release Date
71+
72+
[#list snippetFindings as snippetFinding ]
73+
[#assign snippet = snippetFinding.snippet]
74+
[#assign matchType = snippet.additionalData["matchType"]]
75+
[#if matchType == "PARTIAL"].2+[/#if]| ${snippet.additionalData["id"]} | ${matchType} | ${snippet.purl!""}
76+
| ${snippet.licenses!""} | ${snippet.location.path!""} | ${snippet.provenance.sourceArtifact.url!""}[URL]
77+
| ${snippet.score!""} | ${snippet.additionalData["releaseDate"]}
78+
[#if matchType == "PARTIAL"]
79+
2+^| *Matched lines* 5+| ${localFileURL}: ${snippet.additionalData["matchedLinesSource"]} /
80+
_remote_: ${snippet.additionalData["matchedLinesSnippet"]}
81+
[/#if]
82+
[/#list]
83+
|===
84+
[/#list]
85+
86+
[/#list]
87+
[/#list]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[#ftl]
2+
[#-- @implicitly included --]
3+
4+
[#-- @ftlvariable name="projects" type="kotlin.collections.Set<org.ossreviewtoolkit.reporter.utils.FreemarkerTemplateProcessor.PackageModel>" --]
5+
[#-- @ftlvariable name="packages" type="kotlin.collections.Set<org.ossreviewtoolkit.reporter.utils.FreemarkerTemplateProcessor.PackageModel>" --]
6+
[#-- @ftlvariable name="ortResult" type="org.ossreviewtoolkit.model.OrtResult" --]
7+
[#-- @ftlvariable name="licenseTextProvider" type="org.ossreviewtoolkit.reporter.LicenseTextProvider" --]
8+
[#-- @ftlvariable name="LicenseView" type="org.ossreviewtoolkit.model.licenses.LicenseView" --]
9+
[#-- @ftlvariable name="helper" type="org.ossreviewtoolkit.plugins.reporters.freemarker.FreemarkerTemplateProcessor.TemplateHelper" --]
10+
[#-- @ftlvariable name="projectsAsPackages" type="kotlin.collections.Set<org.ossreviewtoolkit.model.Identifier>" --]
11+
[#-- @ftlvariable name="vulnerabilityReference" type="org.ossreviewtoolkit.model.VulnerabilityReference" --]

plugins/reporters/freemarker/src/main/kotlin/FreemarkerTemplateProcessor.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.ossreviewtoolkit.model.OrtResult
3939
import org.ossreviewtoolkit.model.Package
4040
import org.ossreviewtoolkit.model.RuleViolation
4141
import org.ossreviewtoolkit.model.Severity
42+
import org.ossreviewtoolkit.model.SnippetFinding
4243
import org.ossreviewtoolkit.model.Vulnerability
4344
import org.ossreviewtoolkit.model.VulnerabilityReference
4445
import org.ossreviewtoolkit.model.config.RuleViolationResolution
@@ -310,6 +311,20 @@ class FreemarkerTemplateProcessor(
310311
fun filterForUnresolvedVulnerabilities(vulnerabilities: List<Vulnerability>): List<Vulnerability> =
311312
vulnerabilities.filterNot { input.resolutionProvider.isResolved(it) }
312313

314+
/**
315+
* Return a list of [SnippetFinding]s grouped by the source file being matched by those snippets.
316+
*/
317+
@Suppress("UNUSED") // This function is used in the templates.
318+
fun groupSnippetsByFile(snippetFindings: Collection<SnippetFinding>): Map<String, List<SnippetFinding>> =
319+
snippetFindings.groupBy { it.sourceLocation.path }
320+
321+
/**
322+
* Collect all the licenses present in a collection of [SnippetFinding]s.
323+
*/
324+
@Suppress("UNUSED") // This function is used in the templates.
325+
fun collectLicenses(snippetFindings: Collection<SnippetFinding>) : Set<String> =
326+
snippetFindings.map { it.snippet.licenses.toString() }.toSet()
327+
313328
/**
314329
* Return a flag indicating that issues have been encountered during the run of an advisor with the given
315330
* [capability] with at least the given [severity]. This typically means that the report is incomplete;

0 commit comments

Comments
 (0)