Skip to content

Commit 150b157

Browse files
committed
refactor: Remove RemoteProvenance casts
Casts were removed from `FileListResolver`, `ProvenanceBasedFileStorage`, `ProvenanceDownloader`, and `Scanner`. In order to avoid casting `KnownProvenance` to `RemoteProvenance`, a lot of parameters need to be changed to `RemoteProvenance`. With the exception of `ProvenanceBasedFileStorage`, which now uses `UnknownProvenance` exceptions just like `ProvenanceBasedPostgresStorage`. Signed-off-by: Jens Keim <[email protected]>
1 parent 4e5dc09 commit 150b157

File tree

10 files changed

+34
-28
lines changed

10 files changed

+34
-28
lines changed

scanner/src/funTest/kotlin/provenance/AbstractNestedProvenanceStorageFunTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import io.kotest.core.listeners.TestListener
2323
import io.kotest.core.spec.style.WordSpec
2424
import io.kotest.matchers.shouldBe
2525

26-
import org.ossreviewtoolkit.model.KnownProvenance
26+
import org.ossreviewtoolkit.model.RemoteProvenance
2727
import org.ossreviewtoolkit.model.RepositoryProvenance
2828
import org.ossreviewtoolkit.model.VcsInfo
2929
import org.ossreviewtoolkit.model.VcsType
@@ -78,6 +78,6 @@ private fun createRepositoryProvenance(
7878
) = RepositoryProvenance(vcsInfo, resolvedRevision)
7979

8080
private fun createNestedProvenance(
81-
root: KnownProvenance,
81+
root: RemoteProvenance,
8282
subRepositories: Map<String, RepositoryProvenance> = emptyMap()
8383
) = NestedProvenance(root, subRepositories)

scanner/src/main/kotlin/ScanController.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal class ScanController(
7474
/**
7575
* A map of [KnownProvenance]s to their resolved [NestedProvenance]s.
7676
*/
77-
private val nestedProvenances = mutableMapOf<KnownProvenance, NestedProvenance>()
77+
private val nestedProvenances = mutableMapOf<RemoteProvenance, NestedProvenance>()
7878

7979
/**
8080
* A map of package [Identifier]s to their resolved [KnownProvenance]s. These provenances are used to filter the
@@ -86,7 +86,7 @@ internal class ScanController(
8686
* A map of package [Identifier]s to their resolved [KnownProvenance]s with the VCS path removed. These provenances
8787
* are used during scanning to make sure that always the full repositories are scanned.
8888
*/
89-
private val packageProvenancesWithoutVcsPath = mutableMapOf<Identifier, KnownProvenance>()
89+
private val packageProvenancesWithoutVcsPath = mutableMapOf<Identifier, RemoteProvenance>()
9090

9191
/**
9292
* The [ScanResult]s for each [KnownProvenance] and [ScannerWrapper].
@@ -143,7 +143,7 @@ internal class ScanController(
143143
* Set the [nestedProvenance] corresponding to the given [package provenance][root], overwriting any existing
144144
* values.
145145
*/
146-
fun putNestedProvenance(root: KnownProvenance, nestedProvenance: NestedProvenance) {
146+
fun putNestedProvenance(root: RemoteProvenance, nestedProvenance: NestedProvenance) {
147147
nestedProvenances[root] = nestedProvenance
148148
}
149149

@@ -166,7 +166,7 @@ internal class ScanController(
166166
/**
167167
* Return all [KnownProvenance]s contained in [nestedProvenances].
168168
*/
169-
fun getAllProvenances(): Set<KnownProvenance> =
169+
fun getAllProvenances(): Set<RemoteProvenance> =
170170
nestedProvenances.values.flatMapTo(mutableSetOf()) { it.allProvenances }
171171

172172
/**
@@ -178,7 +178,7 @@ internal class ScanController(
178178
/**
179179
* Return all provenances including sub-repositories associated with the identifiers of the packages they belong to.
180180
*/
181-
fun getIdsByProvenance(): Map<KnownProvenance, Set<Identifier>> =
181+
fun getIdsByProvenance(): Map<RemoteProvenance, Set<Identifier>> =
182182
buildMap<_, MutableSet<Identifier>> {
183183
getNestedProvenancesByPackage().forEach { (pkg, nestedProvenance) ->
184184
nestedProvenance.allProvenances.forEach { provenance ->
@@ -273,7 +273,7 @@ internal class ScanController(
273273
/**
274274
* Return all [KnownProvenance]s for the [packages] with the VCS path removed.
275275
*/
276-
fun getPackageProvenancesWithoutVcsPath(): Set<KnownProvenance> = packageProvenancesWithoutVcsPath.values.toSet()
276+
fun getPackageProvenancesWithoutVcsPath(): Set<RemoteProvenance> = packageProvenancesWithoutVcsPath.values.toSet()
277277

278278
/**
279279
* Return all [PackageScannerWrapper]s.

scanner/src/main/kotlin/Scanner.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class Scanner(
258258
}.awaitAll()
259259
}.forEach { (pkg, result) ->
260260
result.onSuccess { provenance ->
261-
controller.putPackageProvenance(pkg.id, provenance as RemoteProvenance)
261+
controller.putPackageProvenance(pkg.id, provenance)
262262
}.onFailure {
263263
controller.putPackageProvenanceResolutionIssue(
264264
pkg.id,
@@ -279,7 +279,7 @@ class Scanner(
279279
controller.getPackageProvenancesWithoutVcsPath().map { provenance ->
280280
async {
281281
provenance to runCatching {
282-
nestedProvenanceResolver.resolveNestedProvenance(provenance as RemoteProvenance)
282+
nestedProvenanceResolver.resolveNestedProvenance(provenance)
283283
}
284284
}
285285
}.awaitAll()
@@ -569,12 +569,12 @@ class Scanner(
569569
}
570570

571571
private fun scanPath(
572-
provenance: KnownProvenance,
572+
provenance: RemoteProvenance,
573573
scanners: List<PathScannerWrapper>,
574574
context: ScanContext
575575
): Map<PathScannerWrapper, ScanResult> {
576576
val downloadDir = try {
577-
provenanceDownloader.download(provenance as RemoteProvenance)
577+
provenanceDownloader.download(provenance)
578578
} catch (e: DownloadException) {
579579
val issue = createAndLogIssue(
580580
"Downloader", "Could not download provenance $provenance: ${e.collectMessages()}"

scanner/src/main/kotlin/provenance/NestedProvenance.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore
2323

2424
import org.ossreviewtoolkit.model.ArtifactProvenance
2525
import org.ossreviewtoolkit.model.KnownProvenance
26+
import org.ossreviewtoolkit.model.RemoteProvenance
2627
import org.ossreviewtoolkit.model.RepositoryProvenance
2728

2829
/**
@@ -32,7 +33,7 @@ data class NestedProvenance(
3233
/**
3334
* The root provenance that contains the [nested provenances][subRepositories].
3435
*/
35-
val root: KnownProvenance,
36+
val root: RemoteProvenance,
3637

3738
/**
3839
* If [root] is a [RepositoryProvenance] this map contains all paths which contain nested repositories associated
@@ -45,7 +46,7 @@ data class NestedProvenance(
4546
* The set of all contained [KnownProvenance]s.
4647
*/
4748
@JsonIgnore
48-
val allProvenances: Set<KnownProvenance> =
49+
val allProvenances: Set<RemoteProvenance> =
4950
buildSet {
5051
add(root)
5152
addAll(subRepositories.values)

scanner/src/main/kotlin/provenance/NestedProvenanceScanResult.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package org.ossreviewtoolkit.scanner.provenance
2121

2222
import org.ossreviewtoolkit.model.KnownProvenance
2323
import org.ossreviewtoolkit.model.OrtResult
24+
import org.ossreviewtoolkit.model.RemoteProvenance
2425
import org.ossreviewtoolkit.model.RepositoryProvenance
2526
import org.ossreviewtoolkit.model.ScanResult
2627
import org.ossreviewtoolkit.model.ScanSummary
@@ -38,7 +39,7 @@ data class NestedProvenanceScanResult(
3839
/**
3940
* A map of [KnownProvenance]s from [nestedProvenance] associated with lists of [ScanResult]s.
4041
*/
41-
val scanResults: Map<KnownProvenance, List<ScanResult>>
42+
val scanResults: Map<RemoteProvenance, List<ScanResult>>
4243
) {
4344
/**
4445
* Return true if [scanResults] contains at least one scan result for each of the [KnownProvenance]s contained in
@@ -107,7 +108,7 @@ data class NestedProvenanceScanResult(
107108
}
108109
}
109110

110-
fun KnownProvenance.withVcsPath() =
111+
fun RemoteProvenance.withVcsPath() =
111112
when (this) {
112113
is RepositoryProvenance -> {
113114
val pathWithinProvenance = pathsWithinProvenances.getValue(this)

scanner/src/main/kotlin/provenance/PackageProvenanceResolver.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import org.apache.logging.log4j.kotlin.logger
2828

2929
import org.ossreviewtoolkit.downloader.WorkingTreeCache
3030
import org.ossreviewtoolkit.model.ArtifactProvenance
31-
import org.ossreviewtoolkit.model.KnownProvenance
3231
import org.ossreviewtoolkit.model.Package
3332
import org.ossreviewtoolkit.model.Provenance
3433
import org.ossreviewtoolkit.model.RemoteArtifact
34+
import org.ossreviewtoolkit.model.RemoteProvenance
3535
import org.ossreviewtoolkit.model.RepositoryProvenance
3636
import org.ossreviewtoolkit.model.SourceCodeOrigin
3737
import org.ossreviewtoolkit.model.VcsInfo
@@ -50,7 +50,7 @@ interface PackageProvenanceResolver {
5050
*
5151
* Throws an [IOException] if the provenance cannot be resolved.
5252
*/
53-
suspend fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): KnownProvenance
53+
suspend fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): RemoteProvenance
5454
}
5555

5656
/**
@@ -70,7 +70,7 @@ class DefaultPackageProvenanceResolver(
7070
override suspend fun resolveProvenance(
7171
pkg: Package,
7272
defaultSourceCodeOrigins: List<SourceCodeOrigin>
73-
): KnownProvenance {
73+
): RemoteProvenance {
7474
val errors = mutableMapOf<SourceCodeOrigin, Throwable>()
7575
val sourceCodeOrigins = pkg.sourceCodeOrigins ?: defaultSourceCodeOrigins
7676

scanner/src/main/kotlin/provenance/ProvenanceDownloader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fun interface ProvenanceDownloader {
6161
// Use the provenanceDownloader to download each provenance from nestedProvenance separately, because they are
6262
// likely already cached if a path scanner wrapper is used.
6363

64-
val root = download(nestedProvenance.root as RemoteProvenance)
64+
val root = download(nestedProvenance.root)
6565

6666
nestedProvenance.subRepositories.forEach { (path, provenance) ->
6767
val tempDir = download(provenance)

scanner/src/main/kotlin/storages/ProvenanceBasedFileStorage.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ class ProvenanceBasedFileStorage(private val backend: FileStorage) : ProvenanceB
4646
override fun read(provenance: KnownProvenance, scannerMatcher: ScannerMatcher?): List<ScanResult> {
4747
requireEmptyVcsPath(provenance)
4848

49-
val path = storagePath(provenance as RemoteProvenance)
49+
if (provenance !is RemoteProvenance) {
50+
throw ScanStorageException("Scan result must have a known provenance, but it is $provenance.")
51+
}
52+
53+
val path = storagePath(provenance)
5054

5155
return runCatching {
5256
backend.read(path).use { input ->
@@ -81,7 +85,7 @@ class ProvenanceBasedFileStorage(private val backend: FileStorage) : ProvenanceB
8185

8286
requireEmptyVcsPath(provenance)
8387

84-
if (provenance !is KnownProvenance) {
88+
if (provenance !is RemoteProvenance) {
8589
throw ScanStorageException("Scan result must have a known provenance, but it is $provenance.")
8690
}
8791

@@ -95,7 +99,7 @@ class ProvenanceBasedFileStorage(private val backend: FileStorage) : ProvenanceB
9599

96100
val scanResults = existingScanResults + scanResult
97101

98-
val path = storagePath(provenance as RemoteProvenance)
102+
val path = storagePath(provenance)
99103
val yamlBytes = yamlMapper.writeValueAsBytes(scanResults)
100104
val input = ByteArrayInputStream(yamlBytes)
101105

scanner/src/main/kotlin/utils/FileListResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ class FileListResolver(
5656
* Get the [FileList] associated with the provided [provenance]. If it is not available in the [storage], download
5757
* the provenance and create the [FileList] from it.
5858
*/
59-
fun resolve(provenance: KnownProvenance): FileList {
59+
fun resolve(provenance: RemoteProvenance): FileList {
6060
storage.getFileList(provenance)?.let { return it }
6161

62-
val dir = provenanceDownloader.download(provenance as RemoteProvenance)
62+
val dir = provenanceDownloader.download(provenance)
6363

6464
return createFileList(dir).also {
6565
try {

scanner/src/test/kotlin/ScannerTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ private class FakePackageProvenanceResolver : PackageProvenanceResolver {
988988
override suspend fun resolveProvenance(
989989
pkg: Package,
990990
defaultSourceCodeOrigins: List<SourceCodeOrigin>
991-
): KnownProvenance {
991+
): RemoteProvenance {
992992
defaultSourceCodeOrigins.forEach { sourceCodeOrigin ->
993993
when (sourceCodeOrigin) {
994994
SourceCodeOrigin.ARTIFACT -> {
@@ -1103,7 +1103,7 @@ private fun createScanResult(
11031103
)
11041104

11051105
private fun createNestedScanResult(
1106-
provenance: KnownProvenance,
1106+
provenance: RemoteProvenance,
11071107
scannerDetails: ScannerDetails,
11081108
subRepositories: Map<String, RepositoryProvenance> = emptyMap()
11091109
) = NestedProvenanceScanResult(
@@ -1125,7 +1125,7 @@ private fun createStoredScanResult(provenance: Provenance, scannerDetails: Scann
11251125
)
11261126

11271127
private fun createStoredNestedScanResult(
1128-
provenance: KnownProvenance,
1128+
provenance: RemoteProvenance,
11291129
scannerDetails: ScannerDetails,
11301130
subRepositories: Map<String, RepositoryProvenance> = emptyMap()
11311131
) = NestedProvenanceScanResult(

0 commit comments

Comments
 (0)