Skip to content

Commit

Permalink
feat(model): Enable Issue.affectedPath also for older scan results
Browse files Browse the repository at this point in the history
Set the affected path during deserialization. Scan results which have
been created before `affectedPath` got introduced now still have the
value set properly.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Dec 7, 2023
1 parent 5839604 commit 4d532d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions model/src/main/kotlin/ScanSummary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.util.StdConverter

import java.time.Instant

Expand Down Expand Up @@ -79,6 +81,7 @@ data class ScanSummary(
* the size of the result file.
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonDeserialize(converter = IssueListConverter::class)
val issues: List<Issue> = emptyList()
) {
companion object {
Expand Down Expand Up @@ -144,3 +147,19 @@ data class ScanSummary(
)
}
}

/**
* Set the `affectedPath` for scan timeout errors if otherwise it would have been unset. This way scan results which
* have been created before `affectedPath` was introduced will still have that property set.
*/

internal class IssueListConverter : StdConverter<List<Issue>, List<Issue>>() {
override fun convert(issues: List<Issue>): List<Issue> =
issues.map { issue ->
if (issue.affectedPath != null) return@map issue
val match = TIMEOUT_ERROR_REGEX.matchEntire(issue.message) ?: return@map issue
issue.copy(affectedPath = match.groups["file"]!!.value)
}
}

private val TIMEOUT_ERROR_REGEX = Regex("ERROR: Timeout after (\\d+) seconds while scanning file '(?<file>.+)'.")
22 changes: 22 additions & 0 deletions model/src/test/kotlin/ScanSummaryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.ossreviewtoolkit.model
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.containExactly
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe

import org.ossreviewtoolkit.utils.spdx.SpdxExpression

Expand Down Expand Up @@ -55,6 +56,18 @@ class ScanSummaryTest : WordSpec({
containExactly("a/file.txt", "b/c/file.txt")
}
}

"deserializing an old scan result()" should {
"set the affected path of issues which represent a scan timeout error" {
val summary = createSummaryWithIssue(
"ERROR: Timeout after 300 seconds while scanning file 'some-path/some-file.txt'."
)

val deserializedSummary = summary.toYaml().fromYaml<ScanSummary>()

deserializedSummary.issues.single().affectedPath shouldBe "some-path/some-file.txt"
}
}
})

private fun createSummaryWithFindingAndIssuePaths(vararg paths: String): ScanSummary {
Expand Down Expand Up @@ -88,3 +101,12 @@ private fun createSummaryWithFindingAndIssuePaths(vararg paths: String): ScanSum
}
)
}

private fun createSummaryWithIssue(message: String): ScanSummary {
val issue = Issue(
source = "some source",
message = message
)

return ScanSummary.EMPTY.copy(issues = listOf(issue))
}

0 comments on commit 4d532d8

Please sign in to comment.