Skip to content

Commit

Permalink
feat(reporter): Support grouped snippets in the Snippet Report
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Nobelis <[email protected]>
  • Loading branch information
nnobelis authored and mnonnenmacher committed Sep 21, 2023
1 parent 641f520 commit c586a9b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,49 @@ End time : ${summary.startTime} +
[/#if]
[/#if]

[#assign snippets = helper.groupSnippetsByFile(summary.snippetFindings)]
[#list helper.groupSnippetsByFile(summary.snippetFindings) as filePath, snippetFindings ]

[#list snippets as filePath, snippetFindings]
[#if gitRepoUrl?? && gitRepoUrl?contains("github.com")]
[#assign localFileURL = '_${githubBaseURL}/${filePath}[source]_']
[#assign localFileURL = '${githubBaseURL}/${filePath}[${filePath}]']
[#else]
[#assign localFileURL = "_source_"]
[#assign localFileURL = "${filePath}"]
[/#if]

[#assign licenses = helper.collectLicenses(snippetFindings)]

*${filePath}* +
*${localFileURL}* +
License(s):
[#list licenses as license]
${license}[#sep],
[/#list]

[#list helper.groupSnippetsBySourceLines(snippetFindings) as sourceLocation, snippetFinding]
[#assign snippetCount = snippetFinding.snippets?size]

[width=100%]
[cols="1,3,1,3,3,1,1"]
|===
| Match | pURL | License | File | URL | Score | Release Date
| Source Location | pURL | License | File | URL | Score | Release Date

.${snippetCount}+|
[#if helper.isFullFileLocation(sourceLocation)]
Full match
[#else]
Partial match +
${sourceLocation.startLine}-${sourceLocation.endLine}
[/#if]

[#list snippetFindings as snippetFinding ]
[#assign snippet = snippetFinding.snippet]
[#list snippetFinding.snippets as snippet ]
[#assign matchType = snippet.additionalData["matchType"]]
| ${matchType} | ${snippet.purl!""}
| ${snippet.licenses!""} | ${snippet.location.path!""} | ${snippet.provenance.sourceArtifact.url!""}[URL]
| ${snippet.score!""} | ${snippet.additionalData["releaseDate"]}
[#if matchType == "PARTIAL"]
2+^| *Matched lines* 5+| ${localFileURL}: ${snippet.additionalData["matchedLinesSource"]} /
_remote_: ${snippet.additionalData["matchedLinesSnippet"]}
[#assign snippetFilePath = snippet.location.path!""]
[#if matchType == "PARTIAL" && snippetFilePath?has_content]
[#assign snippetFilePath = "${snippetFilePath}#${snippet.additionalData['matchedLinesSnippet']}"]
[/#if]

| ${snippet.purl!""}
| ${snippet.licenses!""} | ${snippetFilePath} | ${snippet.provenance.sourceArtifact.url!""}[URL]
| ${snippet.score!""} | ${snippet.additionalData["releaseDate"]}
[/#list]
|===
[/#list]

[/#list]
[/#list]
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.RuleViolation
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.SnippetFinding
import org.ossreviewtoolkit.model.TextLocation
import org.ossreviewtoolkit.model.Vulnerability
import org.ossreviewtoolkit.model.VulnerabilityReference
import org.ossreviewtoolkit.model.config.RuleViolationResolution
Expand Down Expand Up @@ -318,12 +319,28 @@ class FreemarkerTemplateProcessor(
fun groupSnippetsByFile(snippetFindings: Collection<SnippetFinding>): Map<String, List<SnippetFinding>> =
snippetFindings.groupBy { it.sourceLocation.path }

/**
* Return a list of [SnippetFinding]s grouped by the source file location ( line ranges) being matched by those
* snippets.
*/
@Suppress("UNUSED") // This function is used in the templates.
fun groupSnippetsBySourceLines(snippetFindings: Collection<SnippetFinding>): Map<TextLocation, SnippetFinding> =
snippetFindings.associateBy { it.sourceLocation }

/**
* Return a flag if the given [sourceLocation] refers to the full source file.
*/
@Suppress("UNUSED") // This function is used in the templates.
fun isFullFileLocation(sourceLocation: TextLocation) =
sourceLocation.startLine == TextLocation.UNKNOWN_LINE && sourceLocation.endLine == TextLocation.UNKNOWN_LINE

/**
* Collect all the licenses present in a collection of [SnippetFinding]s.
*/
@Suppress("UNUSED") // This function is used in the templates.
fun collectLicenses(snippetFindings: Collection<SnippetFinding>): Set<String> =
snippetFindings.flatMapTo(mutableSetOf()) { it.snippets.map { snippet -> snippet.licenses.toString() } }
fun collectLicenses(snippetsFindings: Collection<SnippetFinding>): Set<String> =
snippetsFindings.flatMap { findings -> findings.snippets }.map { snippet -> snippet.licenses.toString() }
.toSet()

/**
* Return a flag indicating that issues have been encountered during the run of an advisor with the given
Expand Down

0 comments on commit c586a9b

Please sign in to comment.