Skip to content

Commit

Permalink
refactor(ClearlyDefinedStorage): Reduce packageCoordinates() arguments
Browse files Browse the repository at this point in the history
Make `packageCoordinates()` take a `Package` from which all other
arguments can be deduced.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Jan 24, 2023
1 parent 81669df commit c65da80
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,16 @@ private fun SourceLocation.toCoordinates(): Coordinates = Coordinates(type, prov
private fun VcsInfo.toVcsInfoCurationData(): VcsInfoCurationData = VcsInfoCurationData(type, url, revision, path)

/**
* Generate the coordinates for ClearlyDefined based on the [id], the [vcs], and a [sourceArtifact].
* If information about a Git repository in GitHub is available, this is used. Otherwise, the coordinates
* Generate the coordinates for ClearlyDefined based on the [package][pkg]'s [Package.vcs] [Package.sourceArtifact]
* properties. If information about a Git repository in GitHub is available, this is used. Otherwise, the coordinates
* are derived from the identifier. Throws [IllegalArgumentException] if generating coordinates is not possible.
*/
private fun packageCoordinates(
id: Identifier,
vcs: VcsInfo?,
sourceArtifact: RemoteArtifact?
): Coordinates {
val sourceLocation = id.toClearlyDefinedSourceLocation(vcs?.toVcsInfoCurationData(), sourceArtifact)
return sourceLocation?.toCoordinates() ?: id.toClearlyDefinedCoordinates()
?: throw IllegalArgumentException("Unable to create ClearlyDefined coordinates for '${id.toCoordinates()}'.")
private fun packageCoordinates(pkg: Package): Coordinates {
val vcs = pkg.vcs.takeUnless { it.url.isEmpty() }
val sourceArtifact = pkg.sourceArtifact.takeUnless { it.url.isEmpty() }
val sourceLocation = pkg.id.toClearlyDefinedSourceLocation(vcs?.toVcsInfoCurationData(), sourceArtifact)
return sourceLocation?.toCoordinates() ?: pkg.id.toClearlyDefinedCoordinates()
?: throw IllegalArgumentException("Unable to create ClearlyDefined coordinates for $pkg.")
}

/**
Expand Down Expand Up @@ -110,13 +108,10 @@ class ClearlyDefinedStorage(
}

override fun readInternal(id: Identifier): Result<List<ScanResult>> =
runBlocking(Dispatchers.IO) { readFromClearlyDefined(packageCoordinates(id, null, null)) }
runBlocking(Dispatchers.IO) { readFromClearlyDefined(packageCoordinates(Package.EMPTY.copy(id = id))) }

override fun readInternal(pkg: Package, scannerCriteria: ScannerCriteria): Result<List<ScanResult>> =
runBlocking(Dispatchers.IO) {
val sourceArtifact = pkg.sourceArtifact.takeIf { it.url.isNotEmpty() }
readFromClearlyDefined(packageCoordinates(pkg.id, pkg.vcs, sourceArtifact))
}
runBlocking(Dispatchers.IO) { readFromClearlyDefined(packageCoordinates(pkg)) }

override fun addInternal(id: Identifier, scanResult: ScanResult): Result<Unit> =
Result.failure(ScanStorageException("Adding scan results directly to ClearlyDefined is not supported."))
Expand Down

0 comments on commit c65da80

Please sign in to comment.