Skip to content

Commit

Permalink
feat: Update config format and add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jul 8, 2024
1 parent d6abd2b commit 3957686
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 16 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation("com.felipebz.zpa:zpa-core:3.6.0-SNAPSHOT")
implementation("com.felipebz.zpa:zpa-checks:3.6.0-SNAPSHOT")
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.1")
implementation("org.pf4j:pf4j:3.11.1")
implementation("org.slf4j:slf4j-jdk14:2.0.13")
implementation("me.lucko:jar-relocator:1.7")
Expand Down
27 changes: 20 additions & 7 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import br.com.felipezorzo.zpa.cli.exporters.GenericIssueFormatExporter
import br.com.felipezorzo.zpa.cli.plugin.PluginManager
import com.beust.jcommander.JCommander
import com.beust.jcommander.ParameterException
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import me.lucko.jarrelocator.JarRelocator
import me.lucko.jarrelocator.Relocation
import org.sonar.plsqlopen.CustomAnnotationBasedRulesDefinition
import org.sonar.plsqlopen.metadata.FormsMetadata
import org.sonar.plsqlopen.rules.ActiveRules
import org.sonar.plsqlopen.rules.Repository
import org.sonar.plsqlopen.rules.RuleMetadataLoader
import org.sonar.plsqlopen.rules.ZpaChecks
import org.sonar.plsqlopen.rules.*
import org.sonar.plsqlopen.squid.AstScanner
import org.sonar.plsqlopen.squid.ProgressReport
import org.sonar.plsqlopen.utils.log.Loggers
Expand Down Expand Up @@ -95,12 +92,28 @@ class Main(private val args: Arguments) {
val baseDir = File(args.sources).absoluteFile
val baseDirPath = baseDir.toPath()

val mapper = jacksonObjectMapper()

val activeRules = ActiveRules()
if (args.configFile.isNotEmpty()) {
val configFile = File(args.configFile)
val mapper = ObjectMapper()
val config = mapper.readValue(configFile, ConfigFile::class.java)
activeRules.configureRules(config.rules.map { it.toActiveRuleConfiguration() })
activeRules.configureRules(config.rules.map {
var repositoryKey = "zpa"
var ruleKey = it.key
if (it.key.contains(':')) {
val keys = it.key.split(':')
repositoryKey = keys[0]
ruleKey = keys[1]
}

ActiveRuleConfiguration(
repositoryKey,
ruleKey,
it.value.options.level,
it.value.options.parameters
)
})
}

val ruleMetadataLoader = RuleMetadataLoader()
Expand Down
54 changes: 45 additions & 9 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/config/ConfigFile.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
package br.com.felipezorzo.zpa.cli.config

import org.sonar.plsqlopen.rules.ActiveRuleConfiguration
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

class ConfigFile {
val rules: List<RuleConfiguration> = listOf()
}
data class ConfigFile(
val rules: Map<String, RuleConfiguration>
)

@JsonDeserialize(using = RuleCategoryDeserializer::class)
@JsonSerialize(using = RuleCategorySerializer::class)
class RuleConfiguration {
var repositoryKey: String = "zpa"
var key: String = ""
var severity: String? = null
var options: RuleOptions = RuleOptions()
}

class RuleOptions {
var level: String? = null
set(value) {
field = value?.lowercase()
}
var parameters: Map<String, String> = emptyMap()
}

class RuleCategoryDeserializer : JsonDeserializer<RuleConfiguration>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): RuleConfiguration {
val node: JsonNode = p.codec.readTree(p)
val ruleConfiguration = RuleConfiguration()
val mapper = jacksonObjectMapper()

if (node.isTextual) {
ruleConfiguration.options.level = node.asText().uppercase()
} else if (node.isObject) {
ruleConfiguration.options = mapper.treeToValue(node, RuleOptions::class.java)
}

return ruleConfiguration
}
}

class RuleCategorySerializer : JsonSerializer<RuleConfiguration>() {
override fun serialize(value: RuleConfiguration, gen: JsonGenerator, serializers: SerializerProvider) {
val mapper = jacksonObjectMapper()

fun toActiveRuleConfiguration(): ActiveRuleConfiguration {
return ActiveRuleConfiguration(repositoryKey, key, severity, parameters)
if (value.options.parameters.isEmpty()) {
gen.writeString(value.options.level.toString())
} else {
gen.writeTree(mapper.valueToTree(value.options))
}
}
}
84 changes: 84 additions & 0 deletions zpa-config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"rules": {
"EmptyBlock": "minor",
"ParsingError": "info",
"CollapsibleIfStatements": "major",
"InequalityUsage": "major",
"ComparisonWithNull": "blocker",
"TooManyRowsHandler": "critical",
"InsertWithoutColumns": "critical",
"DeclareSectionWithoutDeclarations": "info",
"NvlWithNullParameter": "blocker",
"ComparisonWithBoolean": "minor",
"CharacterDatatypeUsage": "minor",
"SelectAllColumns": "major",
"ColumnsShouldHaveTableName": "major",
"SelectWithRownumAndOrderBy": "blocker",
"ToDateWithoutFormat": "major",
"ExplicitInParameter": "minor",
"VariableInitializationWithNull": "minor",
"UselessParenthesis": "minor",
"IdenticalExpression": "blocker",
"EmptyStringAssignment": "minor",
"DuplicatedValueInIn": "blocker",
"VariableInitializationWithFunctionCall": "major",
"IfWithExit": "minor",
"FunctionWithOutParameter": "major",
"SameCondition": "blocker",
"AddParenthesesInNestedExpression": "major",
"RaiseStandardException": "major",
"NotFound": "minor",
"QueryWithoutExceptionHandling": {
"level": "critical",
"parameters": {
"strict": "true"
}
},
"UnusedVariable": "major",
"VariableHiding": "major",
"DbmsOutputPut": "minor",
"ReturnOfBooleanExpression": "minor",
"UnnecessaryElse": "minor",
"DeadCode": "major",
"ConcatenationWithNull": "minor",
"SameBranch": "major",
"UnusedParameter": {
"level": "major",
"parameters": {
"ignoreMethods": ""
}
},
"CommitRollback": "major",
"UnnecessaryNullStatement": "minor",
"DuplicateConditionIfElsif": "blocker",
"UnnecessaryAliasInQuery": {
"level": "minor",
"parameters": {
"acceptedLength": "3"
}
},
"VariableInCount": "blocker",
"UnhandledUserDefinedException": "critical",
"UnusedCursor": "major",
"NotASelectedExpression": "critical",
"InvalidReferenceToObject": "major",
"CursorBodyInPackageSpec": "major",
"XPath": {
"level": "major",
"parameters": {
"xpathQuery": "",
"message": "The XPath expression matches this piece of code"
}
},
"VariableName": {
"level": "minor",
"parameters": {
"regexp": "[a-zA-Z]([a-zA-Z0-9_]*[a-zA-Z0-9])?"
}
},
"ToCharInOrderBy": "major",
"DisabledTest": "major",
"RedundantExpectation": "major",
"UnnecessaryLike": "minor"
}
}

0 comments on commit 3957686

Please sign in to comment.