-
Notifications
You must be signed in to change notification settings - Fork 324
Add Config Inversion Linter for Config Definitions #9849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1397f6
adding gradle task to verify configs defined are documented
mhlidd 3b4dbb8
updating task to account for default values and multi-line config def…
mhlidd ca2dc8e
adding missing config
mhlidd af20a7c
Merge branch 'master' into mhlidd/update_config_invaersion_gradle
mhlidd 99bc69a
PR comments without javaparser
mhlidd e5c78de
replacing with JavaParser
mhlidd d10c94e
Merge branch 'master' into mhlidd/update_config_invaersion_gradle
mhlidd 7120c9f
remove unused imports
mhlidd c16bf89
lint
mhlidd 73f2246
pr comments
mhlidd 586c53e
Merge branch 'master' into mhlidd/update_config_invaersion_gradle
mhlidd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,13 @@ | ||||||||
| package datadog.gradle.plugin.config | ||||||||
|
|
||||||||
| import com.github.javaparser.ParserConfiguration | ||||||||
| import com.github.javaparser.StaticJavaParser | ||||||||
| import com.github.javaparser.ast.CompilationUnit | ||||||||
| import com.github.javaparser.ast.expr.StringLiteralExpr | ||||||||
| import com.github.javaparser.ast.nodeTypes.NodeWithModifiers | ||||||||
| import com.github.javaparser.ast.Modifier | ||||||||
| import com.github.javaparser.ast.body.FieldDeclaration | ||||||||
| import com.github.javaparser.ast.body.VariableDeclarator | ||||||||
| import org.gradle.api.Plugin | ||||||||
| import org.gradle.api.Project | ||||||||
| import org.gradle.api.GradleException | ||||||||
|
|
@@ -14,6 +22,7 @@ class ConfigInversionLinter : Plugin<Project> { | |||||||
| val extension = target.extensions.create("supportedTracerConfigurations", SupportedTracerConfigurations::class.java) | ||||||||
| registerLogEnvVarUsages(target, extension) | ||||||||
| registerCheckEnvironmentVariablesUsage(target) | ||||||||
| registerCheckConfigStringsTask(target, extension) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -124,3 +133,91 @@ private fun registerCheckEnvironmentVariablesUsage(project: Project) { | |||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| /** Registers `checkConfigStrings` to validate config definitions against documented supported configurations. */ | ||||||||
| private fun registerCheckConfigStringsTask(project: Project, extension: SupportedTracerConfigurations) { | ||||||||
| val ownerPath = extension.configOwnerPath | ||||||||
| val generatedFile = extension.className | ||||||||
|
|
||||||||
| project.tasks.register("checkConfigStrings") { | ||||||||
| group = "verification" | ||||||||
| description = "Validates that all config definitions in `dd-trace-api/src/main/java/datadog/trace/api/config` exist in `metadata/supported-configurations.json`" | ||||||||
|
|
||||||||
| val mainSourceSetOutput = ownerPath.map { | ||||||||
| project.project(it) | ||||||||
| .extensions.getByType<SourceSetContainer>() | ||||||||
| .named(SourceSet.MAIN_SOURCE_SET_NAME) | ||||||||
| .map { main -> main.output } | ||||||||
| } | ||||||||
| inputs.files(mainSourceSetOutput) | ||||||||
|
|
||||||||
| doLast { | ||||||||
| val repoRoot: Path = project.rootProject.projectDir.toPath() | ||||||||
| val configDir = repoRoot.resolve("dd-trace-api/src/main/java/datadog/trace/api/config").toFile() | ||||||||
|
|
||||||||
| if (!configDir.exists()) { | ||||||||
| throw GradleException("Config directory not found: ${configDir.absolutePath}") | ||||||||
| } | ||||||||
|
|
||||||||
| val urls = mainSourceSetOutput.get().get().files.map { it.toURI().toURL() }.toTypedArray() | ||||||||
| val (supported, aliasMapping) = URLClassLoader(urls, javaClass.classLoader).use { cl -> | ||||||||
| val clazz = Class.forName(generatedFile.get(), true, cl) | ||||||||
| @Suppress("UNCHECKED_CAST") | ||||||||
| val supportedSet = clazz.getField("SUPPORTED").get(null) as Set<String> | ||||||||
| @Suppress("UNCHECKED_CAST") | ||||||||
| val aliasMappingMap = clazz.getField("ALIAS_MAPPING").get(null) as Map<String, String> | ||||||||
| Pair(supportedSet, aliasMappingMap) | ||||||||
| } | ||||||||
|
|
||||||||
| StaticJavaParser.setConfiguration(ParserConfiguration()) | ||||||||
|
|
||||||||
| // Checking "public" "static" "final" | ||||||||
| fun NodeWithModifiers<*>.hasModifiers(vararg mods: Modifier.Keyword) = | ||||||||
| mods.all { hasModifier(it) } | ||||||||
|
|
||||||||
| fun normalize(configValue: String) = | ||||||||
| "DD_" + configValue.uppercase().replace("-", "_").replace(".", "_") | ||||||||
mhlidd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| val violations = buildList { | ||||||||
| configDir.listFiles()?.forEach { file -> | ||||||||
| val fileName = file.name | ||||||||
| val cu: CompilationUnit = StaticJavaParser.parse(file) | ||||||||
|
|
||||||||
| cu.findAll(VariableDeclarator::class.java).forEach { varDecl -> | ||||||||
| val field = varDecl.parentNode | ||||||||
| .filter { it is FieldDeclaration } | ||||||||
| .map { it as FieldDeclaration } | ||||||||
|
||||||||
| .filter { it is FieldDeclaration } | |
| .map { it as FieldDeclaration } | |
| .map { it as? FieldDeclaration } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I wonder if we should set explicitly the language level 🤔 to 1.8, when looking at the code of java parser, I see that the default is set to the
POPULARconstant, howeverPOPULARappear to change regularly in minor versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to currently be set as Java 11, and was changed from Java 8 four years ago. But I agree that we can manually set it for consistency