diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 21759852a4e..aaae3a2e6ac 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -374,7 +374,7 @@ config-inversion-linter: needs: [] script: - ./gradlew --version - - ./gradlew logEnvVarUsages checkEnvironmentVariablesUsage checkConfigStrings + - ./gradlew logEnvVarUsages checkEnvironmentVariablesUsage checkConfigStrings verifyAliasKeysAreSupported test_published_artifacts: extends: .gradle_build diff --git a/buildSrc/src/main/kotlin/datadog/gradle/plugin/config/ConfigInversionLinter.kt b/buildSrc/src/main/kotlin/datadog/gradle/plugin/config/ConfigInversionLinter.kt index 47d1be33951..f1f346d12cb 100644 --- a/buildSrc/src/main/kotlin/datadog/gradle/plugin/config/ConfigInversionLinter.kt +++ b/buildSrc/src/main/kotlin/datadog/gradle/plugin/config/ConfigInversionLinter.kt @@ -23,12 +23,14 @@ class ConfigInversionLinter : Plugin { registerLogEnvVarUsages(target, extension) registerCheckEnvironmentVariablesUsage(target) registerCheckConfigStringsTask(target, extension) + verifyAliasKeysAreSupported(target, extension) } } // Data class for fields from generated class private data class LoadedConfigFields( val supported: Set, + val aliases: Map> = emptyMap(), val aliasMapping: Map = emptyMap() ) @@ -53,9 +55,13 @@ private fun loadConfigFields( else -> throw IllegalStateException("SUPPORTED field must be either Set or Map, but was ${supportedField?.javaClass}") } + @Suppress("UNCHECKED_CAST") + val aliases = clazz.getField("ALIASES").get(null) as Map> + @Suppress("UNCHECKED_CAST") val aliasMappingMap = clazz.getField("ALIAS_MAPPING").get(null) as Map - LoadedConfigFields(supportedSet, aliasMappingMap) + + LoadedConfigFields(supportedSet, aliases, aliasMappingMap) }.also { cachedConfigFields = it } } } @@ -246,3 +252,44 @@ private fun registerCheckConfigStringsTask(project: Project, extension: Supporte } } } + + +/** Registers `verifyAliasKeysAreSupported` to ensure all alias keys are documented as supported configurations. */ +private fun verifyAliasKeysAreSupported(project: Project, extension: SupportedTracerConfigurations) { + val ownerPath = extension.configOwnerPath + val generatedFile = extension.className + + project.tasks.register("verifyAliasKeysAreSupported") { + group = "verification" + description = + "Verifies that all alias keys in `metadata/supported-configurations.json` are also documented as supported configurations." + + val mainSourceSetOutput = ownerPath.map { + project.project(it) + .extensions.getByType() + .named(SourceSet.MAIN_SOURCE_SET_NAME) + .map { main -> main.output } + } + inputs.files(mainSourceSetOutput) + + doLast { + val configFields = loadConfigFields(mainSourceSetOutput.get().get(), generatedFile.get()) + val supported = configFields.supported + val aliases = configFields.aliases.keys + + val unsupportedAliasKeys = aliases - supported + val violations = buildList { + unsupportedAliasKeys.forEach { key -> + add("$key is listed as an alias key but is not documented as a supported configuration in the `supportedConfigurations` key") + } + } + if (violations.isNotEmpty()) { + logger.error("\nFound alias keys not documented as supported configurations:") + violations.forEach { logger.lifecycle(it) } + throw GradleException("Undocumented alias keys found. Please add the above keys to the `supportedConfigurations` in '${extension.jsonFile.get()}'.") + } else { + logger.info("All alias keys are documented as supported configurations.") + } + } + } +}