Skip to content

Commit

Permalink
feat(reporter): Add id of current ORT run as label to ORT results
Browse files Browse the repository at this point in the history
Add the id of the current ORT run as label to the ORT result
in order to have it available during report and notification
generation.

Signed-off-by: Wolfgang Klenk <[email protected]>
  • Loading branch information
wkl3nk committed May 8, 2024
1 parent 251aa40 commit b4cd334
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
63 changes: 63 additions & 0 deletions workers/common/src/main/kotlin/common/OrtBaseResultGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/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.eclipse.apoapsis.ortserver.workers.common

import org.eclipse.apoapsis.ortserver.model.OrtRun

import org.ossreviewtoolkit.model.OrtResult

/**
* An internally used helper class to generate an [OrtResult] from the current [OrtRun].
*
* This class loads the results of the previous worker steps and generates an [OrtResult] from them. In addition, it
* adds a number of common labels that can be evaluated by different types of workers to obtain further information
* about the run.
*
* The following labels are added:
* - runId: The id of the [OrtRun].
*/
object OrtBaseResultGenerator {
private const val RUN_ID_LABEL: String = "runId"

fun generate(ortRunService: OrtRunService, ortRun: OrtRun, failIfRepoInfoMissing: Boolean = true): OrtResult {
val repository = ortRunService.getOrtRepositoryInformation(ortRun, failIfMissing = failIfRepoInfoMissing)
val resolvedConfiguration = ortRunService.getResolvedConfiguration(ortRun)
val analyzerRun = ortRunService.getAnalyzerRunForOrtRun(ortRun.id)
val advisorRun = ortRunService.getAdvisorRunForOrtRun(ortRun.id)
val scannerRun = ortRunService.getScannerRunForOrtRun(ortRun.id)
val evaluatorRun = ortRunService.getEvaluatorRunForOrtRun(ortRun.id)

val baseResult = ortRun.mapToOrt(
repository = repository,
analyzerRun = analyzerRun?.mapToOrt(),
advisorRun = advisorRun?.mapToOrt(),
scannerRun = scannerRun?.mapToOrt(),
evaluatorRun = evaluatorRun?.mapToOrt(),
resolvedConfiguration = resolvedConfiguration.mapToOrt()
)

return baseResult.copy(
// Add common labels for all types of workers
labels = baseResult.labels + mapOf(
RUN_ID_LABEL to ortRun.id.toString()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import org.eclipse.apoapsis.ortserver.transport.Endpoint
import org.eclipse.apoapsis.ortserver.transport.EvaluatorEndpoint
import org.eclipse.apoapsis.ortserver.transport.ReporterEndpoint
import org.eclipse.apoapsis.ortserver.transport.ScannerEndpoint
import org.eclipse.apoapsis.ortserver.workers.common.OrtBaseResultGenerator
import org.eclipse.apoapsis.ortserver.workers.common.OrtRunService
import org.eclipse.apoapsis.ortserver.workers.common.mapToOrt

import org.ossreviewtoolkit.model.OrtResult

Expand All @@ -47,6 +47,7 @@ import org.ossreviewtoolkit.model.OrtResult
* - &lt;worker&gt;JobStatus: Labels of this category allow finding out which worker jobs have been executed and their
* status. The values correspond to the names of the constants from the [JobStatus] enum. For instance, a label
* _analyzerJobStatus_ with the value _FINISHED_ means that the Analyzer job has been executed successfully.
* - runId: The id of the current [OrtRun].
*/
internal class NotifierOrtResultGenerator(
/** Reference to the service to access the current ORT run. */
Expand Down Expand Up @@ -79,24 +80,12 @@ internal class NotifierOrtResultGenerator(
* Generate an [OrtResult] from the given [ortRun] object based on the given [notifierJob].
*/
fun generateOrtResult(ortRun: OrtRun, notifierJob: NotifierJob): OrtResult {
val repository = ortRunService.getOrtRepositoryInformation(ortRun, failIfMissing = false)
val resolvedConfiguration = ortRunService.getResolvedConfiguration(ortRun)
val analyzerRun = ortRunService.getAnalyzerRunForOrtRun(ortRun.id)
val advisorRun = ortRunService.getAdvisorRunForOrtRun(ortRun.id)
val scannerRun = ortRunService.getScannerRunForOrtRun(ortRun.id)
val evaluatorRun = ortRunService.getEvaluatorRunForOrtRun(ortRun.id)

val baseResult = ortRun.mapToOrt(
repository = repository,
analyzerRun = analyzerRun?.mapToOrt(),
advisorRun = advisorRun?.mapToOrt(),
scannerRun = scannerRun?.mapToOrt(),
evaluatorRun = evaluatorRun?.mapToOrt(),
resolvedConfiguration = resolvedConfiguration.mapToOrt()
)

val labelsToAdd = getLabelsForReportDownloadLinks(ortRun) + getMailRecipientsLabels(notifierJob) +
val baseResult = OrtBaseResultGenerator.generate(ortRunService, ortRun, failIfRepoInfoMissing = false)

val labelsToAdd = getLabelsForReportDownloadLinks(ortRun) +
getMailRecipientsLabels(notifierJob) +
getJobStatusLabels(ortRun)

return baseResult.takeIf { labelsToAdd.isEmpty() }
?: baseResult.copy(labels = baseResult.labels + labelsToAdd)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package org.eclipse.apoapsis.ortserver.workers.notifier

import io.kotest.core.spec.style.StringSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.maps.shouldContain
import io.kotest.matchers.maps.shouldContainAll
import io.kotest.matchers.shouldBe

import io.mockk.every
Expand Down Expand Up @@ -67,7 +69,8 @@ class NotifierOrtResultGeneratorTest : StringSpec({

val result = helper.runResultGeneratorTest()

result.labels shouldBe originalLabels
result.labels shouldContainAll originalLabels
result.labels shouldContain ("runId" to ORT_RUN_ID.toString())
}

"Labels for the generated reports should be added" {
Expand Down
21 changes: 3 additions & 18 deletions workers/reporter/src/main/kotlin/reporter/ReporterWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import org.eclipse.apoapsis.ortserver.model.ReporterJob
import org.eclipse.apoapsis.ortserver.model.runs.reporter.Report
import org.eclipse.apoapsis.ortserver.model.runs.reporter.ReporterRun
import org.eclipse.apoapsis.ortserver.workers.common.JobIgnoredException
import org.eclipse.apoapsis.ortserver.workers.common.OrtBaseResultGenerator
import org.eclipse.apoapsis.ortserver.workers.common.OrtRunService
import org.eclipse.apoapsis.ortserver.workers.common.RunResult
import org.eclipse.apoapsis.ortserver.workers.common.context.WorkerContextFactory
import org.eclipse.apoapsis.ortserver.workers.common.env.EnvironmentService
import org.eclipse.apoapsis.ortserver.workers.common.mapToOrt

import org.jetbrains.exposed.sql.Database

Expand Down Expand Up @@ -68,30 +68,15 @@ internal class ReporterWorker(
val context = contextFactory.createContext(job.ortRunId)
runBlocking { environmentService.generateNetRcFileForCurrentRun(context) }

val repository = ortRunService.getOrtRepositoryInformation(ortRun)
val resolvedConfiguration = ortRunService.getResolvedConfiguration(ortRun)
val analyzerRun = ortRunService.getAnalyzerRunForOrtRun(ortRun.id)
val advisorRun = ortRunService.getAdvisorRunForOrtRun(ortRun.id)
val evaluatorJob = ortRunService.getEvaluatorJobForOrtRun(ortRun.id)
val evaluatorRun = ortRunService.getEvaluatorRunForOrtRun(ortRun.id)
val scannerRun = ortRunService.getScannerRunForOrtRun(ortRun.id)

val ortResult = ortRun.mapToOrt(
repository = repository,
analyzerRun = analyzerRun?.mapToOrt(),
advisorRun = advisorRun?.mapToOrt(),
evaluatorRun = evaluatorRun?.mapToOrt(),
scannerRun = scannerRun?.mapToOrt(),
resolvedConfiguration = resolvedConfiguration.mapToOrt()
)
val ortResult = OrtBaseResultGenerator.generate(ortRunService, ortRun, failIfRepoInfoMissing = true)

val startTime = Clock.System.now()

val reporterRunnerResult = runner.run(
job.ortRunId,
ortResult,
job.configuration,
evaluatorJob?.configuration
ortRunService.getEvaluatorJobForOrtRun(ortRun.id)?.configuration
)

val endTime = Clock.System.now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ class ReporterWorkerTest : StringSpec({
every { id } returns ORT_RUN_ID
every { repositoryId } returns REPOSITORY_ID
every { revision } returns "main"
every { labels } returns mapOf("projectName" to "Test project")
}

val ortResult = mockk<OrtResult> {
every { copy(any(), any(), any(), any(), any(), any(), any()) } returns this
every { labels } returns mapOf("projectName" to "Test project")
}

val ortResult = mockk<OrtResult>()
every { analyzerRun.mapToOrt() } returns mockk()
every { advisorRun.mapToOrt() } returns mockk()
every { evaluatorRun.mapToOrt() } returns mockk()
Expand Down

0 comments on commit b4cd334

Please sign in to comment.