Skip to content
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

MTG-8 Apply new detekt configuration #1180

Merged
merged 15 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ analysis:android-lint:
script:
- GRADLE_OPTS="-Xmx2560m" ./gradlew :lintCheckAll --stacktrace --no-daemon

analysis:detekt:
tags: [ "runner:main" ]
image: $CI_IMAGE_DOCKER
stage: analysis
timeout: 30m
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- cache/caches/
- cache/notifications/
script:
- GRADLE_OPTS="-Xmx2560m" ./gradlew :detektAll --stacktrace --no-daemon --build-cache --gradle-user-home cache/

analysis:licenses:
tags: [ "runner:main" ]
image: $CI_IMAGE_DOCKER
Expand All @@ -89,7 +76,7 @@ static-analysis:
variables:
GRADLE_OPTS: "-Xmx2560m -Dorg.gradle.daemon=false -Dorg.gradle.logging.stacktrace=all"
trigger:
include: "https://gitlab-templates.ddbuild.io/mobile/release/static-analysis.yml"
include: "https://gitlab-templates.ddbuild.io/mobile/v11527537-9cf2bf2b/static-analysis.yml"
strategy: depend

analysis:nightly-tests-coverage:
Expand Down
18 changes: 15 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ The whole project is covered by a set of static analysis tools, linters and test
./gradlew instrumentTestAll

# launches the detekt static analysis for all modules
./gradlew detektAll

# launches the ktlint check and formatter for all Kotlin files (the ktlint client needs to be installed on your machine)
# the detekt client needs to be installed on your machine as stated in the official documentation
# https://detekt.dev/docs/gettingstarted/cli
# the configuration files are stored in Datadog's dd-source repository
detekt --config {dd-source}/domains/mobile/config/android/gitlab/detekt/detekt-common.yml
xgouchet marked this conversation as resolved.
Show resolved Hide resolved
detekt --config {dd-source}/domains/mobile/config/android/gitlab/detekt/detekt-public-api.yml

# launches the ktlint check and formatter for all Kotlin files
# the ktlint client needs to be installed on your machine
ktlint -F "**/*.kt" "**/*.kts" '!**/build/generated/**' '!**/build/kspCaches/**'

# launches the Android linter for all modules
Expand Down Expand Up @@ -143,6 +148,13 @@ any change you introduce are still compatible with Java. If you want to add
Kotlin specific features (DSL, lambdas, …), make sure there is a way to get the
same feature from a Java source code.

### Code qualituy

Our code uses [Detekt](https://detekt.dev/) static analysis with a shared configuration, slightly
stricter than the default one. A Detekt check is ran on every on every PR to ensure that all new code
follow this rule.
Current Detekt version: 1.22.0

### Code style

Our coding style is ensured by [KtLint](https://ktlint.github.io/), with the
Expand Down
22 changes: 0 additions & 22 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ task<Delete>("clean") {

tasks.register("checkAll") {
dependsOn(
"detektAll",
"lintCheckAll",
"unitTestAll",
"koverReportAll",
Expand Down Expand Up @@ -121,7 +120,6 @@ tasks.register("unitTestDebug") {
tasks.register("unitTestTools") {
dependsOn(
":sample:kotlin:assembleUs1Release",
":tools:detekt:test",
":tools:unit:testReleaseUnitTest"
)
}
Expand Down Expand Up @@ -189,26 +187,6 @@ tasks.register("checkGeneratedFiles") {
dependsOn("checkApiSurfaceChangesAll")
}

tasks.register("detektAll") {
dependsOn(
":dd-sdk-android:detektMain",
":dd-sdk-android-coil:detektMain",
":dd-sdk-android-compose:detektMain",
":dd-sdk-android-fresco:detektMain",
":dd-sdk-android-glide:detektMain",
":dd-sdk-android-ktx:detektMain",
":dd-sdk-android-ndk:detektMain",
":dd-sdk-android-rx:detektMain",
":dd-sdk-android-sqldelight:detektMain",
":dd-sdk-android-timber:detektMain",
":dd-sdk-android-tv:detektMain",
":instrumented:integration:detekt",
":instrumented:nightly-tests:detekt",
":library:dd-sdk-android-session-replay:detektMain",
":tools:unit:detekt"
)
}

tasks.register("koverReportAll") {
dependsOn(
":dd-sdk-android:koverXmlReport",
Expand Down
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies {

// Dependencies used to configure the gradle plugins
implementation(libs.kotlinGradlePlugin)
implementation(libs.detektGradlePlugin)
implementation(libs.androidToolsGradlePlugin)
implementation(libs.versionsGradlePlugin)
implementation(libs.fuzzyWuzzy)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class CheckApiSurfaceTask : DefaultTask() {
val removals = lines.filter { it.matches(Regex("^-[^-].*$")) }

if (additions.isNotEmpty() || removals.isNotEmpty()) {
throw IllegalStateException(
error(
"Make sure you run the ${ApiSurfacePlugin.TASK_GEN_API_SURFACE} task" +
" before you push your PR.\n" +
additions.joinToString("\n") + removals.joinToString("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class KotlinFileVisitor {

// region KotlinFileVisitor

@Suppress("TooGenericExceptionCaught")
fun visitFile(file: File, printAst: Boolean = false) {
val code = file.readText()
val source = AstSource.String(description = "Content of file ${file.path}", content = code)
Expand All @@ -44,7 +45,7 @@ class KotlinFileVisitor {
when (ast) {
is AstNode -> visitAstNode(ast, level)
is AstTerminal -> ignoreNode()
else -> throw IllegalStateException("Unable to handle $ast")
else -> error("Unable to handle $ast")
}
}

Expand Down Expand Up @@ -411,12 +412,6 @@ class KotlinFileVisitor {
return this is AstTerminal && this.description == description
}

private fun AstNode.firstChildTerminal(description: String): AstTerminal {
val first = firstChildTerminalOrNull(description)
checkNotNull(first) { "Unable to find a child with description $description in \n$this" }
return first
}

private fun AstNode.firstChildTerminalOrNull(description: String): AstTerminal? {
return children.firstOrNull { it.isTerminal(description) } as? AstTerminal
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ open class CheckThirdPartyLicensesTask : DefaultTask() {

// region Internal

@Suppress("DestructuringDeclarationWithTooManyEntries")
private fun parseCsvFile(): List<ThirdPartyDependency> {
val result = mutableListOf<ThirdPartyDependency>()
var firstLineRead = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DependenciesLicenseProvider {

// region Internal

@Suppress("FunctionMaxLength")
private fun getConfigurationDependenciesMap(
project: Project,
transitive: Boolean
Expand Down Expand Up @@ -105,6 +106,7 @@ class DependenciesLicenseProvider {
}
}

@Suppress("FunctionMaxLength")
private fun listThirdPartyLicensesInConfiguration(
configuration: String,
dependencies: List<ComponentIdentifier>,
Expand Down Expand Up @@ -145,15 +147,15 @@ class DependenciesLicenseProvider {
}

private fun configurationToComponent(configuration: String): ThirdPartyDependency.Component {
if (configuration in knownImportConfiguration) {
return ThirdPartyDependency.Component.IMPORT
return if (configuration in knownImportConfiguration) {
ThirdPartyDependency.Component.IMPORT
} else if (configuration in knownImportTestConfiguration) {
return ThirdPartyDependency.Component.IMPORT_TEST
ThirdPartyDependency.Component.IMPORT_TEST
} else if (configuration in knownBuildConfiguration) {
return ThirdPartyDependency.Component.BUILD
ThirdPartyDependency.Component.BUILD
} else {
logger.info("Unknown configuration $configuration")
return ThirdPartyDependency.Component.UNKNOWN
ThirdPartyDependency.Component.UNKNOWN
}
}

Expand Down Expand Up @@ -194,7 +196,6 @@ class DependenciesLicenseProvider {
private const val TAG_LICENSES = "licenses"
private const val TAG_LICENSE = "license"
private const val TAG_NAME = "name"
private const val TAG_URL = "url"

private val knownImportConfiguration = setOf(
"archives",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object SPDXLicenceConverter {
}
}

@Suppress("ReturnCount")
private fun convertLicense(license: String): SPDXLicense? {
if (license.isBlank()) return null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.datadog.gradle.plugin.checklicenses

@Suppress("ktlint:enum-entry-name-case")
@Suppress("ktlint:enum-entry-name-case", "EnumNaming")
enum class SPDXLicense(val csvName: String) {
_0BSD("0BSD"),
AAL("AAL"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ open class GitCloneDependenciesExtension : Serializable {
var excludedPrefixes: List<String>,
var originRef: String,
var destinationFolder: String
) : Serializable
) : Serializable {
companion object {
private const val serialVersionUID: Long = 1L
}
}

internal val dependencies: MutableList<Dependency> = mutableListOf()

Expand All @@ -31,4 +35,8 @@ open class GitCloneDependenciesExtension : Serializable {
Dependency(repo, subFolder, excludedPrefixes, ref, destinationFolder)
)
}

companion object {
private const val serialVersionUID: Long = 1L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,12 @@ open class GitCloneDependenciesTask : DefaultTask() {
}

private fun keepLine(line: String): Boolean {
if (LOG_LINE_REGEX.matches(line)) {
return false
return when {
LOG_LINE_REGEX.matches(line) -> false
SLF4J_REGEX.matches(line) -> false
LOMBOK_IMPORTS_REGEX.matches(line) -> false
else -> true
}
if (SLF4J_REGEX.matches(line)) {
return false
}
if (LOMBOK_IMPORTS_REGEX.matches(line)) {
return false
}
return true
}

private fun deleteClone(target: File) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class JsonSchemaReader(
* @param ref the reference used to resolve the target definition
* @return the found Definition reference or null
*/
@Suppress("ReturnCount")
private fun findDefinitionReference(
ref: String,
fromFile: File
Expand Down Expand Up @@ -148,6 +149,7 @@ class JsonSchemaReader(

// region Internal

@Suppress("FunctionMaxLength")
private fun extractAdditionalPropertiesType(
definition: JsonDefinition,
fromFile: File
Expand All @@ -157,7 +159,7 @@ class JsonSchemaReader(
is Map<*, *> -> {
val value = additional["type"]?.toString()
if (value == null) {
throw IllegalStateException("additionalProperties object is missing a `type`")
error("additionalProperties object is missing a `type`")
} else {
val type = JsonType.values().firstOrNull { it.name.equals(value, true) }
transform(JsonDefinition.ANY.copy(type = type), "?", fromFile)
Expand All @@ -171,7 +173,7 @@ class JsonSchemaReader(
}
}
else -> {
throw IllegalStateException("additionalProperties uses an unknown format")
error("additionalProperties uses an unknown format")
}
}
}
Expand Down Expand Up @@ -277,7 +279,7 @@ class JsonSchemaReader(
if (refDefinition != null) {
transform(refDefinition.definition, refDefinition.typeName, refDefinition.fromFile)
} else {
throw IllegalStateException(
error(
"Definition reference not found: ${definition.ref}."
)
}
Expand All @@ -294,8 +296,8 @@ class JsonSchemaReader(
description: String?,
fromFile: File
): TypeDefinition {
val options = oneOf.mapIndexed { i, it ->
transform(it, it.title ?: "${typeName}_$i", fromFile)
val options = oneOf.mapIndexed { i, type ->
transform(type, type.title ?: "${typeName}_$i", fromFile)
}

val asArray = options.filterIsInstance<TypeDefinition.Array>().firstOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.squareup.kotlinpoet.TypeName

internal val NOTHING_NULLABLE = NOTHING.copy(nullable = true)

@Suppress("ReturnCount")
internal fun String.variableName(): String {
val split = this.split("_").filter { it.isNotBlank() }
if (split.isEmpty()) return ""
Expand Down
Loading