|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.eclipse.apoapsis.ortserver.dao.tables |
| 21 | + |
| 22 | +import org.eclipse.apoapsis.ortserver.dao.repositories.repository.RepositoriesTable |
| 23 | +import org.eclipse.apoapsis.ortserver.dao.utils.jsonb |
| 24 | +import org.eclipse.apoapsis.ortserver.model.ChangeEventEntityType |
| 25 | +import org.eclipse.apoapsis.ortserver.model.RepositoryId |
| 26 | +import org.eclipse.apoapsis.ortserver.model.VulnerabilityResolutionDefinition |
| 27 | +import org.eclipse.apoapsis.ortserver.model.VulnerabilityResolutionReason |
| 28 | +import org.eclipse.apoapsis.ortserver.model.util.OptionalValue |
| 29 | + |
| 30 | +import org.jetbrains.exposed.dao.id.LongIdTable |
| 31 | +import org.jetbrains.exposed.sql.ResultRow |
| 32 | +import org.jetbrains.exposed.sql.insertAndGetId |
| 33 | +import org.jetbrains.exposed.sql.update |
| 34 | + |
| 35 | +/** |
| 36 | + * A table to store definitions of vulnerability resolutions. |
| 37 | + */ |
| 38 | +object VulnerabilityResolutionDefinitionsTable : LongIdTable("vulnerability_resolution_definitions") { |
| 39 | + val repositoryId = reference("repository_id", RepositoriesTable) |
| 40 | + |
| 41 | + val contextRunId = long("context_run_id") |
| 42 | + val idMatchers = jsonb<List<String>>("id_matchers") |
| 43 | + val reason = text("reason") |
| 44 | + val comment = text("comment") |
| 45 | + val archived = bool("archived").default(false) |
| 46 | + |
| 47 | + fun insert( |
| 48 | + hierarchyId: RepositoryId, |
| 49 | + runIdInput: Long, |
| 50 | + idMatchersInput: List<String>, |
| 51 | + reasonInput: VulnerabilityResolutionReason, |
| 52 | + commentInput: String |
| 53 | + ): Long { |
| 54 | + return insertAndGetId { |
| 55 | + it[repositoryId] = hierarchyId.value |
| 56 | + it[contextRunId] = runIdInput |
| 57 | + it[idMatchers] = idMatchersInput |
| 58 | + it[reason] = reasonInput.name |
| 59 | + it[comment] = commentInput |
| 60 | + }.value |
| 61 | + } |
| 62 | + |
| 63 | + fun get(definitionId: Long): VulnerabilityResolutionDefinition { |
| 64 | + return select(columns) |
| 65 | + .where { id eq definitionId } |
| 66 | + .single() |
| 67 | + .toVulnerabilityResolutionDefinition() |
| 68 | + } |
| 69 | + |
| 70 | + fun getOrNull(definitionId: Long): VulnerabilityResolutionDefinition? { |
| 71 | + val row = select(columns) |
| 72 | + .where { id eq definitionId } |
| 73 | + .singleOrNull() ?: return null |
| 74 | + |
| 75 | + return row.toVulnerabilityResolutionDefinition() |
| 76 | + } |
| 77 | + |
| 78 | + fun updateDefinition( |
| 79 | + definitionId: Long, |
| 80 | + idMatchersInput: OptionalValue<List<String>> = OptionalValue.Absent, |
| 81 | + reasonInput: OptionalValue<VulnerabilityResolutionReason> = OptionalValue.Absent, |
| 82 | + commentInput: OptionalValue<String> = OptionalValue.Absent, |
| 83 | + archivedInput: OptionalValue<Boolean> = OptionalValue.Absent |
| 84 | + ) { |
| 85 | + update({ id eq definitionId }) { stmt -> |
| 86 | + idMatchersInput.ifPresent { stmt[idMatchers] = it } |
| 87 | + reasonInput.ifPresent { stmt[reason] = it.name } |
| 88 | + commentInput.ifPresent { stmt[comment] = it } |
| 89 | + archivedInput.ifPresent { stmt[archived] = it } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + fun ResultRow.toVulnerabilityResolutionDefinition() = VulnerabilityResolutionDefinition( |
| 94 | + this[id].value, |
| 95 | + RepositoryId(this[repositoryId].value), |
| 96 | + this[idMatchers], |
| 97 | + VulnerabilityResolutionReason.valueOf(this[reason]), |
| 98 | + this[comment], |
| 99 | + this[archived], |
| 100 | + ChangeLogTable.getAllByEntityTypeAndId( |
| 101 | + ChangeEventEntityType.VULNERABILITY_RESOLUTION_DEFINITION, |
| 102 | + this[id].value.toString() |
| 103 | + ) |
| 104 | + ) |
| 105 | +} |
0 commit comments