-
Notifications
You must be signed in to change notification settings - Fork 314
/
DeleteCommand.kt
175 lines (147 loc) · 6.82 KB
/
DeleteCommand.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2021 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.scanstorage
import com.github.ajalt.clikt.core.CliktCommand
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.split
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file
import org.apache.logging.log4j.kotlin.logger
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.SqlExpressionBuilder.isNotNull
import org.jetbrains.exposed.sql.SqlExpressionBuilder.regexp
import org.jetbrains.exposed.sql.compoundAnd
import org.jetbrains.exposed.sql.compoundOr
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.selectAll
import org.ossreviewtoolkit.helper.utils.ORTH_NAME
import org.ossreviewtoolkit.helper.utils.execAndMap
import org.ossreviewtoolkit.model.SourceCodeOrigin
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.PostgresStorageConfiguration
import org.ossreviewtoolkit.model.utils.DatabaseUtils
import org.ossreviewtoolkit.model.utils.DatabaseUtils.transaction
import org.ossreviewtoolkit.model.utils.rawParam
import org.ossreviewtoolkit.scanner.storages.utils.ScanResults
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 = "Removes stored scan results matching the options or all results if no options are given."
) {
private val configFile by option(
"--config",
help = "The path to the ORT configuration file that configures the scan results storage."
).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 sourceCodeOrigins by option(
"--source-code-origins",
help = "The origin of the scan results that should be deleted."
).enum<SourceCodeOrigin>().split(",").default(emptyList())
private val packageId by option(
"--package-id",
help = "A regular expression for matching the package id."
)
private val license by option(
"--license",
help = "The license expression of a detected license of a package that should be deleted."
)
private val dryRun by option(
"--dry-run",
help = "Perform a dry run without actually deleting anything."
).flag()
override fun run() {
val database = connectPostgresStorage(OrtConfiguration.load(configArguments, configFile))
val provenanceKeys = sourceCodeOrigins.map { origin ->
when (origin) {
SourceCodeOrigin.ARTIFACT -> "source_artifact"
SourceCodeOrigin.VCS -> "vcs_info"
}
}
val identifierCondition = packageId?.let { ScanResults.identifier regexp it }
val licenseCondition = license?.let {
println("Searching for stored scan results with license '$license'. This might take some time.")
val ids = database.transaction {
// language=PostgreSQL
"""
SELECT ${ScanResults.id.name}
FROM ${ScanResults.tableName}
WHERE (${ScanResults.scanResult.name} -> 'summary' -> 'licenses')::jsonb @>
'[{"license": "$license"}]'::jsonb
""".trimIndent().execAndMap {
it.getInt(ScanResults.id.name)
}
}
ScanResults.id inList ids
}
val provenanceConditions = provenanceKeys.map { key ->
rawParam("scan_result->'provenance'->>'$key'").isNotNull()
}.takeIf { it.isNotEmpty() }?.compoundOr()
val conditions = listOfNotNull(identifierCondition, licenseCondition, provenanceConditions)
if (conditions.isEmpty()) {
// Default to the safe option to not delete anything if no conditions are given.
println("Not specified what entries to delete. Not deleting anything.")
return
}
val condition = conditions.compoundAnd()
if (dryRun) {
val count = database.transaction {
ScanResults.selectAll().where { condition }.count()
}
println("Would delete $count scan result(s).")
if (logger.delegate.isDebugEnabled) {
database.transaction {
ScanResults.select(ScanResults.identifier).where { condition }
.forEach(logger::debug)
}
}
} else {
val count = database.transaction {
ScanResults.deleteWhere { condition }
}
println("Successfully deleted $count stored scan result(s).")
}
}
private fun connectPostgresStorage(config: OrtConfiguration): Database {
val storageConfig = config.scanner.storages?.get("postgresStorage") as? PostgresStorageConfiguration
?: throw IllegalArgumentException("postgresStorage not configured.")
logger.info {
"Using Postgres storage with URL ${storageConfig.connection.url} and schema " +
"'${storageConfig.connection.schema}'."
}
val dataSource = DatabaseUtils.createHikariDataSource(
config = storageConfig.connection,
applicationNameSuffix = ORTH_NAME,
maxPoolSize = 1
)
return Database.connect(dataSource.value)
}
}