Skip to content

Commit 7fe4fb7

Browse files
committed
feat: Add table for saving user-performed change events
Add table for storing events regarding items such as resolutions. Signed-off-by: Johanna Lamppu <[email protected]>
1 parent 2911e87 commit 7fe4fb7

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.userDisplayName.UserDisplayNameDao
23+
import org.eclipse.apoapsis.ortserver.model.ChangeEvent
24+
import org.eclipse.apoapsis.ortserver.model.ChangeEventAction
25+
import org.eclipse.apoapsis.ortserver.model.ChangeEventEntityType
26+
import org.eclipse.apoapsis.ortserver.model.UserDisplayName
27+
28+
import org.jetbrains.exposed.sql.SortOrder
29+
import org.jetbrains.exposed.sql.Table
30+
import org.jetbrains.exposed.sql.and
31+
import org.jetbrains.exposed.sql.insert
32+
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp
33+
34+
/*
35+
* A table to store change log entries representing user-performed change events.
36+
*/
37+
object ChangeLogTable : Table("change_log") {
38+
val entityType = text("entity_type")
39+
val entityId = text("entity_id")
40+
val userId = text("user_id")
41+
val occurredAt = timestamp("occurred_at")
42+
val action = text("action")
43+
44+
fun insert(
45+
entityTypeInput: ChangeEventEntityType,
46+
entityIdInput: String,
47+
userIdInput: String,
48+
actionInput: ChangeEventAction
49+
) {
50+
insert {
51+
it[entityType] = entityTypeInput.name
52+
it[entityId] = entityIdInput
53+
it[userId] = userIdInput
54+
it[action] = actionInput.name
55+
}
56+
}
57+
58+
fun getAllByEntityTypeAndId(
59+
entityTypeSearch: ChangeEventEntityType,
60+
entityIdSearch: String
61+
): List<ChangeEvent> {
62+
return select(columns)
63+
.where { (entityType eq entityTypeSearch.name) and (entityId eq entityIdSearch) }
64+
.orderBy(occurredAt, SortOrder.ASC)
65+
.map { row ->
66+
ChangeEvent(
67+
user = UserDisplayNameDao.findById(row[userId])?.mapToModel()
68+
?: UserDisplayName(row[userId], "Unknown"),
69+
occurredAt = row[occurredAt],
70+
action = ChangeEventAction.valueOf(row[action])
71+
)
72+
}
73+
}
74+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE change_log (
2+
entity_type text NOT NULL,
3+
entity_id text NOT NULL,
4+
user_id varchar(40) NOT NULL,
5+
occurred_at timestamp DEFAULT NOW() NOT NULL,
6+
action text NOT NULL
7+
);
8+
9+
CREATE INDEX IF NOT EXISTS idx_change_log_entity_type ON change_log(entity_type);
10+
11+
CREATE INDEX IF NOT EXISTS idx_change_log_entity_id ON change_log(entity_id);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.model
21+
22+
import kotlinx.datetime.Instant
23+
24+
/**
25+
* A data class representing a change event performed by a user.
26+
*/
27+
data class ChangeEvent(
28+
/** The user who performed the change. */
29+
val user: UserDisplayName,
30+
31+
/** The time the change occurred. */
32+
val occurredAt: Instant,
33+
34+
/** The action performed. */
35+
val action: ChangeEventAction
36+
)
37+
38+
/**
39+
* An enumeration of the entity types that can be affected by a [ChangeEvent].
40+
*/
41+
enum class ChangeEventEntityType {
42+
VULNERABILITY_RESOLUTION_DEFINITION
43+
}
44+
45+
/**
46+
* An enumeration of the actions that can be performed, resulting in a [ChangeEvent].
47+
*/
48+
enum class ChangeEventAction {
49+
CREATE,
50+
UPDATE,
51+
ARCHIVE,
52+
RESTORE
53+
}

0 commit comments

Comments
 (0)