Skip to content
Snippets Groups Projects
Verified Commit 73a20234 authored by Jan-Niclas Strüwer's avatar Jan-Niclas Strüwer
Browse files

code cleanup db storage

parent 836971dc
No related branches found
No related tags found
Loading
......@@ -2,6 +2,7 @@ package de.fraunhofer.iem.dataprovider.toolResult
import de.fraunhofer.iem.dataprovider.gitlab.RepositoryRepository
import de.fraunhofer.iem.dataprovider.sarif.Sarif
import de.fraunhofer.iem.dataprovider.sarif.Tool
import de.fraunhofer.iem.dataprovider.sarif.asDbObject
import org.springframework.stereotype.Service
import java.util.*
......@@ -21,30 +22,16 @@ class ToolRunService(
val repo = repositoryRepository.findById(repoId)
sarif.runs.forEach { run ->
val fullName = run.tool.driver.fullName
val name = run.tool.driver.name
val version = run.tool.driver.version
val tool =
toolRepository.findByFullNameIgnoreCaseAndNameIgnoreCaseAndVersionIgnoreCase(fullName, name, version)
?: toolRepository.save(run.tool.asDbObject())
val rules = if (tool.id != null) {
ruleRepository.findByTool_Id(tool.id!!).toMutableList()
} else {
mutableListOf()
}
run.tool.driver.rules.forEach { sarifRule ->
if (rules.find { it.sarifRuleId == sarifRule.id } == null) {
val rule = ruleRepository.save(sarifRule.asDbObject())
rules.add(rule)
tool.addRule(rule)
}
}
val tool = findOrCreateTool(run.tool)
val rules = findRulesForTool(tool.id)
createNewRulesAndSaveToTool(run.tool.driver.rules, rules, tool)
val toolResults = run.results.map { sarifResult ->
val tr = sarifResult.asDbObject()
// link rules to results
rules.filter { it.sarifRuleId == sarifResult.ruleId }
.forEach { it.addResultLocation(tr) }
tr
......@@ -58,10 +45,42 @@ class ToolRunService(
val result = toolRunRepository.save(toolRun)
toolRepository.save(tool)
repo.ifPresent {
it.addToolResult(result)
repositoryRepository.save(it)
}
}
}
private fun createNewRulesAndSaveToTool(
sarifRules: List<de.fraunhofer.iem.dataprovider.sarif.Rule>,
rules: MutableList<Rule>,
tool: de.fraunhofer.iem.dataprovider.toolResult.Tool
) {
sarifRules.forEach { sarifRule ->
if (rules.find { it.sarifRuleId == sarifRule.id } == null) {
val rule = ruleRepository.save(sarifRule.asDbObject())
rules.add(rule)
tool.addRule(rule)
}
}
}
private fun findOrCreateTool(tool: Tool): de.fraunhofer.iem.dataprovider.toolResult.Tool {
val fullName = tool.driver.fullName
val name = tool.driver.name
val version = tool.driver.version
return toolRepository.findByFullNameIgnoreCaseAndNameIgnoreCaseAndVersionIgnoreCase(fullName, name, version)
?: toolRepository.save(tool.asDbObject())
}
private fun findRulesForTool(id: UUID?): MutableList<Rule> {
return if (id != null) {
ruleRepository.findByTool_Id(id).toMutableList()
} else {
mutableListOf()
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.