Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper command to delete stored provenance by package id #8327

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions helper-cli/src/main/kotlin/HelperMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.ossreviewtoolkit.helper.commands.classifications.LicenseClassificatio
import org.ossreviewtoolkit.helper.commands.dev.DevCommand
import org.ossreviewtoolkit.helper.commands.packageconfig.PackageConfigurationCommand
import org.ossreviewtoolkit.helper.commands.packagecuration.PackageCurationsCommand
import org.ossreviewtoolkit.helper.commands.provenancestorage.ProvenanceStorageCommand
import org.ossreviewtoolkit.helper.commands.repoconfig.RepositoryConfigurationCommand
import org.ossreviewtoolkit.helper.commands.scanstorage.ScanStorageCommand
import org.ossreviewtoolkit.helper.utils.ORTH_NAME
Expand Down Expand Up @@ -97,6 +98,7 @@ internal class HelperMain : CliktCommand(
MergeRepositoryConfigurationsCommand(),
PackageConfigurationCommand(),
PackageCurationsCommand(),
ProvenanceStorageCommand(),
RepositoryConfigurationCommand(),
ScanStorageCommand(),
SetDependencyRepresentationCommand(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.helper.commands.provenancestorage

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.terminal
import com.github.ajalt.clikt.parameters.options.associate
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.mordant.rendering.Theme
import com.github.ajalt.mordant.terminal.YesNoPrompt

import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.scanner.ScanStorages
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

internal class DeleteCommand : CliktCommand(
help = "Deletes stored provenance results matching the options."
) {
private val configFile by option(
"--config",
help = "The path to the ORT configuration file that configures the scan storages."
).convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }
.default(ortConfigDirectory.resolve(ORT_CONFIG_FILENAME))

private val configArguments by option(
"-P",
help = "Override a key-value pair in the configuration file. For example: " +
"-P ort.scanner.storages.postgres.connection.schema=testSchema"
).associate()

private val packageId by option(
"--package-id",
help = "Coordinates of the package ID to delete."
).convert { Identifier(it) }
.required()

private val forceYes by option(
"--yes", "-y",
help = "Force yes on all prompts."
).flag()

override fun run() {
val config = OrtConfiguration.load(configArguments, configFile)
val scanStorages = ScanStorages.createFromConfig(config.scanner)

Check warning on line 71 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L70-L71

Added lines #L70 - L71 were not covered by tests

val provenances = scanStorages.packageProvenanceStorage.readProvenances(packageId)

Check warning on line 73 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L73

Added line #L73 was not covered by tests
if (provenances.isEmpty()) {
val pkgCoords = Theme.Default.success(packageId.toCoordinates())
echo(Theme.Default.info("No stored provenance found for '$pkgCoords'."))
return

Check warning on line 77 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L75-L77

Added lines #L75 - L77 were not covered by tests
}

val count = Theme.Default.warning(provenances.size.toString())

Check warning on line 80 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L80

Added line #L80 was not covered by tests

echo(Theme.Default.danger("About to delete the following $count provenance(s):"))
provenances.forEach(::echo)

Check warning on line 83 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L82-L83

Added lines #L82 - L83 were not covered by tests

if (forceYes || YesNoPrompt("Continue?", terminal).ask() == true) {
scanStorages.packageProvenanceStorage.deleteProvenances(packageId)

Check warning on line 86 in helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt#L86

Added line #L86 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.helper.commands.provenancestorage

import com.github.ajalt.clikt.core.NoOpCliktCommand
import com.github.ajalt.clikt.core.subcommands

internal class ProvenanceStorageCommand : NoOpCliktCommand(
help = "Commands for working with provenance storages."
) {
init {
subcommands(
DeleteCommand()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,6 @@ internal class DummyProvenanceStorage : PackageProvenanceStorage {
sourceArtifact: RemoteArtifact,
result: PackageProvenanceResolutionResult
) { /* no-op */ }

override fun deleteProvenances(id: Identifier) { /* no-op */ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@
}
}
}

override fun deleteProvenances(id: Identifier) {
val path = storagePath(id)

Check warning on line 117 in scanner/src/main/kotlin/provenance/FileBasedPackageProvenanceStorage.kt

View check run for this annotation

Codecov / codecov/patch

scanner/src/main/kotlin/provenance/FileBasedPackageProvenanceStorage.kt#L117

Added line #L117 was not covered by tests
if (!backend.delete(path)) {
logger.warn { "Could not delete resolved provenances for '${id.toCoordinates()}' at path '$path'." }

Check warning on line 119 in scanner/src/main/kotlin/provenance/FileBasedPackageProvenanceStorage.kt

View check run for this annotation

Codecov / codecov/patch

scanner/src/main/kotlin/provenance/FileBasedPackageProvenanceStorage.kt#L119

Added line #L119 was not covered by tests
}
}
}

private const val FILE_NAME = "resolved_provenance.yml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ interface PackageProvenanceStorage {
* for [id] and [vcs] it is overwritten.
*/
fun writeProvenance(id: Identifier, vcs: VcsInfo, result: PackageProvenanceResolutionResult)

/**
* Delete all [PackageProvenanceResolutionResult]s for the [id].
*/
fun deleteProvenances(id: Identifier)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@
}
}
}

override fun deleteProvenances(id: Identifier) {
database.transaction {
table.deleteWhere {
table.identifier eq id.toCoordinates()

Check warning on line 141 in scanner/src/main/kotlin/provenance/PostgresPackageProvenanceStorage.kt

View check run for this annotation

Codecov / codecov/patch

scanner/src/main/kotlin/provenance/PostgresPackageProvenanceStorage.kt#L139-L141

Added lines #L139 - L141 were not covered by tests
}
}
}
}

private class PackageProvenances(tableName: String) : IntIdTable(tableName) {
Expand Down
5 changes: 5 additions & 0 deletions utils/ort/src/main/kotlin/storage/FileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ interface FileStorage {
* provided [inputStream] is closed after writing it to the file.
*/
fun write(path: String, inputStream: InputStream)

/**
* Delete the file at the given [path] and return whether the operation was successful.
*/
fun delete(path: String): Boolean
}
12 changes: 12 additions & 0 deletions utils/ort/src/main/kotlin/storage/HttpFileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,16 @@
}

private fun urlForPath(path: String) = "$url/$path$query"

override fun delete(path: String): Boolean {
val request = requestBuilder()
.delete()
.url(urlForPath(path))
.build()

Check warning on line 144 in utils/ort/src/main/kotlin/storage/HttpFileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/HttpFileStorage.kt#L141-L144

Added lines #L141 - L144 were not covered by tests

logger.debug { "Deleting file from storage: ${request.url}" }

Check warning on line 146 in utils/ort/src/main/kotlin/storage/HttpFileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/HttpFileStorage.kt#L146

Added line #L146 was not covered by tests

val response = httpClient.execute(request)
return response.isSuccessful

Check warning on line 149 in utils/ort/src/main/kotlin/storage/HttpFileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/HttpFileStorage.kt#L148-L149

Added lines #L148 - L149 were not covered by tests
}
}
3 changes: 3 additions & 0 deletions utils/ort/src/main/kotlin/storage/LocalFileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@
inputStream.use { it.copyTo(outputStream) }
}
}

@Synchronized
override fun delete(path: String): Boolean = directory.resolve(path).delete()

Check warning on line 81 in utils/ort/src/main/kotlin/storage/LocalFileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/LocalFileStorage.kt#L81

Added line #L81 was not covered by tests
}
11 changes: 11 additions & 0 deletions utils/ort/src/main/kotlin/storage/S3FileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest
import software.amazon.awssdk.services.s3.model.GetObjectRequest
import software.amazon.awssdk.services.s3.model.HeadObjectRequest
import software.amazon.awssdk.services.s3.model.NoSuchKeyException
Expand Down Expand Up @@ -136,4 +137,14 @@
if (exception is S3Exception) logger.warn { "Can not write '$path' to S3 bucket '$bucketName'." }
}
}

override fun delete(path: String): Boolean {
val request = DeleteObjectRequest.builder()
.key(path)
.bucket(bucketName)
.build()

Check warning on line 145 in utils/ort/src/main/kotlin/storage/S3FileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/S3FileStorage.kt#L142-L145

Added lines #L142 - L145 were not covered by tests

val response = s3Client.deleteObject(request)
return response.sdkHttpResponse().isSuccessful

Check warning on line 148 in utils/ort/src/main/kotlin/storage/S3FileStorage.kt

View check run for this annotation

Codecov / codecov/patch

utils/ort/src/main/kotlin/storage/S3FileStorage.kt#L147-L148

Added lines #L147 - L148 were not covered by tests
}
}
Loading