Skip to content

Commit

Permalink
fix(AdvisorRecord): Merge all properties of vulnerabilities
Browse files Browse the repository at this point in the history
Do not leave the `summary` and `description` at their `null` default
values but take them from the first vulnerability which has either set.
Update the documentation accordingly and also generally improve the
wording while at it.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Oct 30, 2023
1 parent 37d2925 commit 7a2b4aa
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions model/src/main/kotlin/AdvisorRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,21 @@ data class AdvisorRecord(
}

/**
* Merge this list of [Vulnerability] objects by combining vulnerabilities with the same ID and merging their
* references.
* Merge this collection of [Vulnerability] objects by combining vulnerabilities with the same ID and merging their
* references. Other [Vulnerability] properties are taken from the first object which has any such property set.
*/
private fun Collection<Vulnerability>.mergeVulnerabilities(): List<Vulnerability> {
val vulnerabilitiesById = groupByTo(sortedMapOf()) { it.id }
return vulnerabilitiesById.map { it.value.mergeReferences() }
}

/**
* Merge this (non-empty) list of [Vulnerability] objects (which are expected to have the same ID) by to a single
* [Vulnerability] that contains all the references from the source vulnerabilities (with duplicates removed).
* Merge this (non-empty) collection of [Vulnerability] objects (which are expected to have the same ID) to a single
* [Vulnerability] that contains all the references from the original vulnerabilities (with duplicates removed). Other
* [Vulnerability] properties are taken from the first object which has any such property set.
*/
private fun Collection<Vulnerability>.mergeReferences(): Vulnerability {
val references = flatMapTo(mutableSetOf()) { it.references }
return Vulnerability(id = first().id, references = references.toList())
val entry = find { it.summary != null || it.description != null } ?: first()
return entry.copy(references = references.toList())
}

0 comments on commit 7a2b4aa

Please sign in to comment.