Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Handle empty results and non-multiple sarifs better (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers authored Jan 23, 2024
1 parent c4a4b94 commit 52e3e1e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/kotlin/slack/cli/sarif/MergeSarifReports.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,14 @@ public class MergeSarifReports : CliktCommand(help = DESCRIPTION) {

private fun merge(inputs: List<Path>) {
log("Parsing ${inputs.size} sarif files")
val parsed = loadSarifs(inputs)
val mergedSarif =
loadSarifs(inputs)
.merge(levelOverride = level, removeUriPrefixes = removeUriPrefixes, log = ::log)
when (parsed.size) {
0 -> error("No sarif files parsed. Consider using --allow-empty")
1 -> parsed[0]
else ->
parsed.merge(levelOverride = level, removeUriPrefixes = removeUriPrefixes, log = ::log)
}
log("Writing merged sarif to $outputFile")
prepareOutput()
outputFile.writeText(SarifSerializer.toJson(mergedSarif))
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/slack/cli/sarif/SarifUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ internal fun List<SarifSchema210>.merge(
removeUriPrefixes: Boolean = false,
log: (String) -> Unit,
): SarifSchema210 {
check(isNotEmpty()) { "Must have at least one sarif file to merge!" }

log("Merging $size sarif files")
val sortedMergedRules =
flatMap { it.runs.single().tool.driver.rules.orEmpty() }.associateBy { it.id }.toSortedMap()
val mergedResults =
flatMap { it.runs.single().results.orEmpty() }
// Some projects produce multiple reports for different variants, so we need to
Expand All @@ -127,6 +127,14 @@ internal fun List<SarifSchema210>.merge(
.distinctBy { it.shallowHash }
.also { log("Merged ${it.size} results") }

if (mergedResults.isEmpty()) {
// Nothing to do here, just return the first
return this[0]
}

val sortedMergedRules =
flatMap { it.runs.single().tool.driver.rules.orEmpty() }.associateBy { it.id }.toSortedMap()

// Update rule.ruleIndex to match the index in rulesToAdd
val ruleIndicesById =
sortedMergedRules.entries.withIndex().associate { (index, entry) -> entry.key to index }
Expand Down

0 comments on commit 52e3e1e

Please sign in to comment.