Skip to content

Commit

Permalink
feat(helper-cli): Add a command to delete stored provenance by packag…
Browse files Browse the repository at this point in the history
…e id

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Feb 21, 2024
1 parent a9e006e commit e483e25
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
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,93 @@
/*
* 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 dryRun by option(
"--dry-run",
help = "Perform a dry run without actually deleting anything."
).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

if (dryRun) {
echo(Theme.Default.info("Would delete the following $count provenance(s):"))
provenances.forEach(::echo)

Check warning on line 84 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#L83-L84

Added lines #L83 - L84 were not covered by tests
} else {
echo(Theme.Default.danger("About to delete the following $count provenance(s):"))
provenances.forEach(::echo)

Check warning on line 87 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-L87

Added lines #L86 - L87 were not covered by tests
if (YesNoPrompt("Continue?", terminal).ask() == true) {
scanStorages.packageProvenanceStorage.deleteProvenances(packageId)

Check warning on line 89 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#L89

Added line #L89 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()
)
}
}

0 comments on commit e483e25

Please sign in to comment.