Skip to content

Commit 542bcff

Browse files
mnonnenmachernnobelis
authored andcommitted
refactor: Use the Severity enum in issues
Change the API and backend model `Issue` class to use the `Severity` enums as type for the `severity` property. This ensures that only valid values are used. A database migration is not required because outside of test code only valid values were used before. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent 3258f14 commit 542bcff

File tree

21 files changed

+79
-40
lines changed

21 files changed

+79
-40
lines changed

api/v1/mapping/src/commonMain/kotlin/Mappings.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import org.eclipse.apoapsis.ortserver.api.v1.model.RepositoryType as ApiReposito
6464
import org.eclipse.apoapsis.ortserver.api.v1.model.ScannerJob as ApiScannerJob
6565
import org.eclipse.apoapsis.ortserver.api.v1.model.ScannerJobConfiguration as ApiScannerJobConfiguration
6666
import org.eclipse.apoapsis.ortserver.api.v1.model.Secret as ApiSecret
67+
import org.eclipse.apoapsis.ortserver.api.v1.model.Severity as ApiSeverity
6768
import org.eclipse.apoapsis.ortserver.api.v1.model.SortDirection as ApiSortDirection
6869
import org.eclipse.apoapsis.ortserver.api.v1.model.SortProperty as ApiSortProperty
6970
import org.eclipse.apoapsis.ortserver.api.v1.model.SourceCodeOrigin as ApiSourceCodeOrigin
@@ -101,6 +102,7 @@ import org.eclipse.apoapsis.ortserver.model.RepositoryType
101102
import org.eclipse.apoapsis.ortserver.model.ScannerJob
102103
import org.eclipse.apoapsis.ortserver.model.ScannerJobConfiguration
103104
import org.eclipse.apoapsis.ortserver.model.Secret
105+
import org.eclipse.apoapsis.ortserver.model.Severity
104106
import org.eclipse.apoapsis.ortserver.model.SourceCodeOrigin
105107
import org.eclipse.apoapsis.ortserver.model.runs.Issue
106108
import org.eclipse.apoapsis.ortserver.model.runs.PackageManagerConfiguration
@@ -215,7 +217,13 @@ fun ApiEvaluatorJobConfiguration.mapToModel() =
215217
ruleSet
216218
)
217219

218-
fun Issue.mapToApi() = ApiIssue(timestamp, source, message, severity)
220+
fun Issue.mapToApi() = ApiIssue(timestamp, source, message, severity.mapToApi())
221+
222+
fun Severity.mapToApi() = when (this) {
223+
Severity.ERROR -> ApiSeverity.ERROR
224+
Severity.WARNING -> ApiSeverity.WARNING
225+
Severity.HINT -> ApiSeverity.HINT
226+
}
219227

220228
fun JobStatus.mapToApi() = ApiJobStatus.valueOf(name)
221229

api/v1/model/src/commonMain/kotlin/Issue.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ data class Issue(
3636
/** A message describing the issue. */
3737
val message: String,
3838

39-
/** The severity, such as ERROR, HINT, etc. */
40-
val severity: String
39+
/** The [Severity] of the issue. */
40+
val severity: Severity
4141
)

dao/src/main/kotlin/repositories/DaoScannerRunRepository.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.eclipse.apoapsis.ortserver.dao.tables.runs.scanner.ScannerRunsScanner
3838
import org.eclipse.apoapsis.ortserver.dao.tables.runs.scanner.ScannerRunsScannersTable
3939
import org.eclipse.apoapsis.ortserver.dao.tables.runs.scanner.ScannerRunsTable
4040
import org.eclipse.apoapsis.ortserver.dao.tables.runs.shared.EnvironmentDao
41+
import org.eclipse.apoapsis.ortserver.model.Severity
4142
import org.eclipse.apoapsis.ortserver.model.repositories.ScannerRunRepository
4243
import org.eclipse.apoapsis.ortserver.model.runs.Environment
4344
import org.eclipse.apoapsis.ortserver.model.runs.Identifier
@@ -126,7 +127,7 @@ class DaoScannerRunRepository(private val db: Database) : ScannerRunRepository {
126127
source = "scanner",
127128
message = "Could not resolve provenance for package '$identifier': " +
128129
"${packageProvenanceDao.errorMessage}",
129-
severity = "ERROR"
130+
severity = Severity.ERROR
130131
)
131132
)
132133
add(result)
@@ -143,7 +144,7 @@ class DaoScannerRunRepository(private val db: Database) : ScannerRunRepository {
143144
timestamp = Clock.System.now(),
144145
source = "scanner",
145146
message = "Could not resolve nested provenance for provenance '$packageProvenance'.",
146-
severity = "ERROR"
147+
severity = Severity.ERROR
147148
)
148149
)
149150
add(result)

dao/src/main/kotlin/tables/runs/shared/IssuesTable.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.eclipse.apoapsis.ortserver.dao.tables.runs.shared
2121

2222
import org.eclipse.apoapsis.ortserver.dao.utils.toDatabasePrecision
23+
import org.eclipse.apoapsis.ortserver.model.Severity
2324
import org.eclipse.apoapsis.ortserver.model.runs.Issue
2425

2526
import org.jetbrains.exposed.dao.LongEntity
@@ -35,7 +36,7 @@ object IssuesTable : LongIdTable("issues") {
3536
val timestamp = timestamp("timestamp")
3637
val issueSource = text("source")
3738
val message = text("message")
38-
val severity = text("severity")
39+
val severity = enumerationByName<Severity>("severity", 128)
3940
}
4041

4142
class IssueDao(id: EntityID<Long>) : LongEntity(id) {

dao/src/test/kotlin/repositories/DaoAdvisorRunRepositoryTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.eclipse.apoapsis.ortserver.dao.test.DatabaseTestExtension
3030
import org.eclipse.apoapsis.ortserver.dao.test.Fixtures
3131
import org.eclipse.apoapsis.ortserver.dao.utils.toDatabasePrecision
3232
import org.eclipse.apoapsis.ortserver.model.PluginConfiguration
33+
import org.eclipse.apoapsis.ortserver.model.Severity
3334
import org.eclipse.apoapsis.ortserver.model.runs.Environment
3435
import org.eclipse.apoapsis.ortserver.model.runs.Identifier
3536
import org.eclipse.apoapsis.ortserver.model.runs.Issue
@@ -174,7 +175,7 @@ val issue = Issue(
174175
timestamp = Clock.System.now().toDatabasePrecision(),
175176
source = "source",
176177
message = "message",
177-
severity = "ERROR"
178+
severity = Severity.ERROR
178179
)
179180

180181
val defect = Defect(

dao/src/test/kotlin/repositories/DaoOrtRunRepositoryTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.eclipse.apoapsis.ortserver.model.AnalyzerJobConfiguration
3434
import org.eclipse.apoapsis.ortserver.model.JobConfigurations
3535
import org.eclipse.apoapsis.ortserver.model.OrtRun
3636
import org.eclipse.apoapsis.ortserver.model.OrtRunStatus
37+
import org.eclipse.apoapsis.ortserver.model.Severity
3738
import org.eclipse.apoapsis.ortserver.model.runs.Issue
3839
import org.eclipse.apoapsis.ortserver.model.util.ListQueryParameters
3940
import org.eclipse.apoapsis.ortserver.model.util.ListQueryResult
@@ -235,9 +236,9 @@ class DaoOrtRunRepositoryTest : StringSpec({
235236
}
236237

237238
"update should update an entry in the database" {
238-
val issue1 = Issue(Instant.parse("2023-08-02T06:16:10Z"), "existing", "An initial issue", "WARNING")
239-
val issue2 = Issue(Instant.parse("2023-08-02T06:17:16Z"), "new1", "An new issue", "HINT")
240-
val issue3 = Issue(Instant.parse("2023-08-02T06:17:36Z"), "new2", "Another new issue", "ERROR")
239+
val issue1 = Issue(Instant.parse("2023-08-02T06:16:10Z"), "existing", "An initial issue", Severity.WARNING)
240+
val issue2 = Issue(Instant.parse("2023-08-02T06:17:16Z"), "new1", "An new issue", Severity.HINT)
241+
val issue3 = Issue(Instant.parse("2023-08-02T06:17:36Z"), "new2", "Another new issue", Severity.ERROR)
241242

242243
val label2Value = "new value for label2"
243244
val label3Value = "a newly added label"

model/src/commonMain/kotlin/runs/Issue.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ package org.eclipse.apoapsis.ortserver.model.runs
2222
import kotlinx.datetime.Instant
2323
import kotlinx.serialization.Serializable
2424

25+
import org.eclipse.apoapsis.ortserver.model.Severity
26+
2527
@Serializable
2628
data class Issue(
2729
val timestamp: Instant,
2830
val source: String,
2931
val message: String,
30-
val severity: String
32+
val severity: Severity
3133
)

orchestrator/src/main/kotlin/Orchestrator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.eclipse.apoapsis.ortserver.model.JobConfigurations
2828
import org.eclipse.apoapsis.ortserver.model.JobStatus
2929
import org.eclipse.apoapsis.ortserver.model.OrtRun
3030
import org.eclipse.apoapsis.ortserver.model.OrtRunStatus
31+
import org.eclipse.apoapsis.ortserver.model.Severity
3132
import org.eclipse.apoapsis.ortserver.model.WorkerJob
3233
import org.eclipse.apoapsis.ortserver.model.orchestrator.AdvisorWorkerError
3334
import org.eclipse.apoapsis.ortserver.model.orchestrator.AdvisorWorkerResult
@@ -417,5 +418,5 @@ fun <T : Any> Endpoint<T>.createErrorIssue(): Issue = Issue(
417418
timestamp = Clock.System.now(),
418419
source = configPrefix,
419420
message = "The $configPrefix worker failed due to an unexpected error.",
420-
severity = "ERROR"
421+
severity = Severity.ERROR
421422
)

orchestrator/src/test/kotlin/OrchestratorTest.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import org.eclipse.apoapsis.ortserver.model.Repository
5656
import org.eclipse.apoapsis.ortserver.model.RepositoryType
5757
import org.eclipse.apoapsis.ortserver.model.ScannerJob
5858
import org.eclipse.apoapsis.ortserver.model.ScannerJobConfiguration
59+
import org.eclipse.apoapsis.ortserver.model.Severity
5960
import org.eclipse.apoapsis.ortserver.model.WorkerJob
6061
import org.eclipse.apoapsis.ortserver.model.orchestrator.AdvisorWorkerError
6162
import org.eclipse.apoapsis.ortserver.model.orchestrator.AdvisorWorkerResult
@@ -387,7 +388,7 @@ class OrchestratorTest : WordSpec() {
387388
id = withArg { it shouldBe configWorkerError.ortRunId },
388389
status = withArg { it.verifyOptionalValue(OrtRunStatus.FAILED) },
389390
issues = withArg {
390-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", ConfigEndpoint.configPrefix) }
391+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, ConfigEndpoint.configPrefix) }
391392
}
392393
)
393394
}
@@ -686,7 +687,7 @@ class OrchestratorTest : WordSpec() {
686687
ortRunRepository.update(
687688
id = withArg { it shouldBe analyzerJob.ortRunId },
688689
issues = withArg {
689-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", AnalyzerEndpoint.configPrefix) }
690+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, AnalyzerEndpoint.configPrefix) }
690691
}
691692
)
692693
}
@@ -892,7 +893,7 @@ class OrchestratorTest : WordSpec() {
892893
ortRunRepository.update(
893894
id = withArg { it shouldBe advisorJob.ortRunId },
894895
issues = withArg {
895-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", AdvisorEndpoint.configPrefix) }
896+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, AdvisorEndpoint.configPrefix) }
896897
}
897898
)
898899
}
@@ -983,7 +984,7 @@ class OrchestratorTest : WordSpec() {
983984
ortRunRepository.update(
984985
id = withArg { it shouldBe evaluatorJob.ortRunId },
985986
issues = withArg {
986-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", EvaluatorEndpoint.configPrefix) }
987+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, EvaluatorEndpoint.configPrefix) }
987988
}
988989
)
989990
}
@@ -1136,7 +1137,7 @@ class OrchestratorTest : WordSpec() {
11361137
ortRunRepository.update(
11371138
id = withArg { it shouldBe reporterJob.ortRunId },
11381139
issues = withArg {
1139-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", ReporterEndpoint.configPrefix) }
1140+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, ReporterEndpoint.configPrefix) }
11401141
}
11411142
)
11421143
}
@@ -1272,7 +1273,7 @@ class OrchestratorTest : WordSpec() {
12721273
ortRunRepository.update(
12731274
id = withArg { it shouldBe notifierJob.ortRunId },
12741275
issues = withArg {
1275-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", NotifierEndpoint.configPrefix) }
1276+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, NotifierEndpoint.configPrefix) }
12761277
}
12771278
)
12781279
}
@@ -1331,7 +1332,7 @@ class OrchestratorTest : WordSpec() {
13311332
ortRunRepository.update(
13321333
id = withArg { it shouldBe notifierJob.ortRunId },
13331334
issues = withArg {
1334-
verifyIssues(it) { verifyWorkerErrorIssues("ERROR", NotifierEndpoint.configPrefix) }
1335+
verifyIssues(it) { verifyWorkerErrorIssues(Severity.ERROR, NotifierEndpoint.configPrefix) }
13351336
}
13361337
)
13371338
}
@@ -1837,7 +1838,7 @@ fun verifyIssues(
18371838
issuesList.validate()
18381839
}
18391840

1840-
fun Collection<Issue>.verifyWorkerErrorIssues(severity: String, source: String) {
1841+
fun Collection<Issue>.verifyWorkerErrorIssues(severity: Severity, source: String) {
18411842
shouldBeSingleton { issue ->
18421843
issue.severity shouldBe severity
18431844
issue.source shouldBe source

workers/common/src/main/kotlin/common/OrtMappings.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlinx.datetime.toKotlinInstant
2525

2626
import org.eclipse.apoapsis.ortserver.model.PluginConfiguration
2727
import org.eclipse.apoapsis.ortserver.model.RepositoryType
28+
import org.eclipse.apoapsis.ortserver.model.Severity
2829
import org.eclipse.apoapsis.ortserver.model.resolvedconfiguration.PackageCurationProviderConfig
2930
import org.eclipse.apoapsis.ortserver.model.resolvedconfiguration.ResolvedPackageCurations
3031
import org.eclipse.apoapsis.ortserver.model.runs.AnalyzerConfiguration
@@ -120,6 +121,7 @@ import org.ossreviewtoolkit.model.ScanResult as OrtScanResult
120121
import org.ossreviewtoolkit.model.ScanSummary as OrtScanSummary
121122
import org.ossreviewtoolkit.model.ScannerDetails as OrtScannerDetails
122123
import org.ossreviewtoolkit.model.ScannerRun as OrtScannerRun
124+
import org.ossreviewtoolkit.model.Severity as OrtSeverity
123125
import org.ossreviewtoolkit.model.Snippet as OrtSnippet
124126
import org.ossreviewtoolkit.model.SnippetFinding as OrtSnippetFinding
125127
import org.ossreviewtoolkit.model.TextLocation as OrtTextLocation
@@ -308,7 +310,7 @@ fun OrtIssue.mapToModel() =
308310
timestamp = timestamp.toKotlinInstant(),
309311
source = source,
310312
message = message,
311-
severity = severity.name
313+
severity = severity.mapToModel()
312314
)
313315

314316
fun OrtPackage.mapToModel() =
@@ -521,6 +523,12 @@ fun OrtScanSummary.mapToModel() =
521523

522524
fun OrtScopeExclude.mapToModel() = ScopeExclude(pattern, reason.name, comment)
523525

526+
fun OrtSeverity.mapToModel() = when (this) {
527+
OrtSeverity.ERROR -> Severity.ERROR
528+
OrtSeverity.WARNING -> Severity.WARNING
529+
OrtSeverity.HINT -> Severity.HINT
530+
}
531+
524532
fun OrtSnippet.mapToModel() = Snippet(
525533
purl = purl,
526534
provenance = provenance.mapToModel(),

0 commit comments

Comments
 (0)